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(int x) {
- data = x;
- next = NULL;
- }
- };
- El *top = NULL;
- void push(int x) {
- El *t = new El(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() {
- if (top == NULL) return 0;
- El *t = top;
- int res = 0;
- while (t != NULL) {
- t = t->next;
- res++;
- }
- return res;
- }
- void cleanStack() {
- if (top != NULL) {
- pop();
- cleanStack();
- }
- }
Add Comment
Please, Sign In to add comment