fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct node{
  6. int data;
  7. struct node *next;
  8. };
  9.  
  10. typedef struct node node;
  11.  
  12. node *makeNode(int x){
  13. node *newNode = new node();
  14. newNode->data = x;
  15. newNode->next = NULL;
  16. return newNode;
  17. }
  18.  
  19. void push(node **top, int x){
  20. node *newNode = makeNode(x);
  21. newNode->next = *top;
  22. *top = newNode;
  23. }
  24.  
  25. void pop(node **top){
  26. if((*top) != NULL){
  27. node *tmp = *top;
  28. (*top) = tmp->next;
  29. delete tmp;
  30. }
  31. }
  32.  
  33. int Top(node *top){
  34. if(top != NULL)
  35. return top->data;
  36. }
  37.  
  38. int size(node *top){
  39. int ans = 0;
  40. while(top != NULL){
  41. ++ans;
  42. top = top->next;
  43. }
  44. return ans;
  45. }
  46.  
  47. int main(){
  48. node *st = NULL;
  49. while(1){
  50. cout << "-------------------------------\n";
  51. cout << "1. push\n";
  52. cout << "2. pop\n";
  53. cout << "3. top\n";
  54. cout << "4. size\n";
  55. cout << "0. Thoat\n";
  56. cout << "-------------------------------\n";
  57. int lc; cin >> lc;
  58. if(lc == 1){
  59. int x; cout << "Nhap x :" ; cin >> x;
  60. push(&st, x);
  61. }
  62. else if(lc == 2){
  63. pop(&st);
  64. }
  65. else if(lc == 3){
  66. if(st == NULL) cout << "EMPTY\n";
  67. else
  68. cout << Top(st) << endl;
  69. }
  70. else if(lc == 4){
  71. cout << size(st) << endl;
  72. }
  73. else{
  74. break;
  75. }
  76. }
  77. }
  78.  
  79.  
  80. //#include <bits/stdc++.h>
  81. //
  82. //using namespace std;
  83. //
  84. //int n = 0, st[10001];
  85. //
  86. //void push(int x){
  87. // st[n] = x;
  88. // ++n;
  89. //}
  90. //
  91. //void pop(){
  92. // if(n >= 1)
  93. // --n;
  94. //}
  95. //
  96. //int top(){
  97. // return st[n - 1];
  98. //}
  99. //
  100. //int size(){
  101. // return n;
  102. //}
  103. //
  104. //int main(){
  105. // while(1){
  106. // cout << "-------------------------------\n";
  107. // cout << "1. push\n";
  108. // cout << "2. pop\n";
  109. // cout << "3. top\n";
  110. // cout << "4. size\n";
  111. // cout << "0. Thoat\n";
  112. // cout << "-------------------------------\n";
  113. // int lc; cin >> lc;
  114. // if(lc == 1){
  115. // int x; cout << "Nhap x :" ; 1cin >> x;
  116. // push(x);
  117. // }
  118. // else if(lc == 2){
  119. // pop();
  120. // }
  121. // else if(lc == 3){
  122. // if(n == 0) cout << "EMPTY\n";
  123. // cout << top() << endl;
  124. // }
  125. // else if(lc == 4){
  126. // cout << size() << endl;
  127. // }
  128. // else{
  129. // break;
  130. // }
  131. // }
  132. // return 0;
  133. //}
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
-------------------------------
1. push
2. pop
3. top
4. size
0. Thoat
-------------------------------