irapilguy

Untitled

Nov 9th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. using namespace std;
  4. struct El {
  5. int data;
  6. El *next;
  7. };
  8. El *top = NULL;
  9. El *createEl(int x) {
  10. El *t = new El();
  11. t->data = x;
  12. t->next = NULL;
  13. return t;
  14. }
  15. void push(int x) {
  16. El *t = createEl(x);
  17. if (top == NULL) {
  18. top = t;
  19. return;
  20. }
  21. t->next = top;
  22. top = t;
  23. }
  24. int pop() {
  25. if (top == NULL) return 0;
  26. int res = top->data;
  27. top = top->next;
  28. return res;
  29. }
  30. int back() {
  31. if (top != NULL) return top->data;
  32. }
  33. int size() {
  34. El *t = top;
  35. int res = 0;
  36. while(t != NULL) {
  37. t->next;
  38. res++;
  39. }
  40. return res;
  41. }
  42. void cleanStack() {
  43. if(top != NULL) {
  44. pop();
  45. cleanStack();
  46. }
  47. }
  48. int main() {
  49. int n;
  50. cin >> n;
  51. for (int i = 0; i < n; i++) {
  52. int x;
  53. cin >> x;
  54. push(x);
  55. cout << "SIZE" << size();
  56. }
  57. cout << "size " << size() << " \n";
  58. cout << "pop " << pop() << "\n";
  59. cout << "size " << size() << " \n";
  60. cleanStack();
  61. cout << "size after " << size() << " \n";
  62. return 0;
  63. }
Add Comment
Please, Sign In to add comment