Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- struct El {
- int data;
- El *next;
- };
- El *top = NULL;
- El *createEl(int x) {
- El *t = new El();
- t->data = x;
- t->next = NULL;
- return t;
- }
- void push(int x) {
- El *t = createEl(x);
- if (top == NULL) {
- top = t;
- return;
- }
- t->next = top;
- top = t;
- }
- int pop() {
- if (top == NULL) return 0;
- int res = top->data;
- top = top->next;
- return res;
- }
- int back() {
- if (top != NULL) return top->data;
- }
- int size() {
- El *t = top;
- int res = 0;
- while(t != NULL) {
- t->next;
- res++;
- }
- return res;
- }
- void cleanStack() {
- if(top != NULL) {
- pop();
- cleanStack();
- }
- }
- int main() {
- int n;
- cin >> n;
- for (int i = 0; i < n; i++) {
- int x;
- cin >> x;
- push(x);
- cout << "SIZE" << size();
- }
- cout << "size " << size() << " \n";
- cout << "pop " << pop() << "\n";
- cout << "size " << size() << " \n";
- cleanStack();
- cout << "size after " << size() << " \n";
- return 0;
- }
Add Comment
Please, Sign In to add comment