fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6. int x;
  7. Node *next;
  8. };
  9.  
  10. Node *head=NULL,*tail=NULL;
  11. int Count=0;
  12.  
  13. void push(int a)
  14. {
  15. if (head==NULL)
  16. {
  17. Node *p = new Node;
  18. p->x=a;
  19. p->next=NULL;
  20. head=tail=p;
  21. }
  22. else
  23. {
  24. Node *p = new Node;
  25. p->x=a;
  26. p->next=NULL;
  27. tail->next=p;
  28. tail=p;
  29. }
  30. Count++;
  31. }
  32.  
  33. void pop()
  34. {
  35. if (Count)
  36. {
  37. Node *p = head;
  38. head = p->next;
  39. delete p;
  40. Count--;
  41. if (!Count) head=tail=NULL;
  42. }
  43. }
  44.  
  45. void Show()
  46. {
  47. Node *p = head;
  48. while (p)
  49. {
  50. cout<<p->x<<" ";
  51. p = p->next;
  52. }
  53. cout<<endl;
  54. }
  55.  
  56. int size() {return Count;}
  57.  
  58. int main()
  59. {
  60. for (int i=0;i<6;i++) push(i);
  61. Show();
  62. cout<<"size = "<<size()<<endl;
  63. for (int i=0;i<2;i++) pop();
  64. Show();
  65. cout<<"size = "<<size()<<endl;
  66. return 0;
  67. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
0 1 2 3 4 5 
size = 6
2 3 4 5 
size = 4