irapilguy

Untitled

Nov 10th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 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.     El(int x) {
  8.         data = x;
  9.         next = NULL;
  10.     }
  11. };
  12. El *top = NULL;
  13. void push(int x) {
  14.     El *t = new El(x);
  15.     if (top == NULL) {
  16.         top = t;
  17.         return;
  18.     }
  19.     t->next = top;
  20.     top = t;
  21. }
  22. int pop() {
  23.     if (top == NULL) return 0;
  24.     int res = top->data;
  25.     top = top->next;
  26.     return res;
  27. }
  28. int back() {
  29.     if (top != NULL) return top->data;
  30. }
  31. int size() {
  32.     if (top == NULL) return 0;
  33.     El *t = top;
  34.     int res = 0;
  35.     while (t != NULL) {
  36.         t = t->next;
  37.         res++;
  38.     }
  39.     return res;
  40. }
  41. void cleanStack() {
  42.  if (top != NULL) {
  43.         pop();
  44.         cleanStack();
  45.     }
  46. }
Add Comment
Please, Sign In to add comment