Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct node
- {
- node * next;
- int val;
- };
- bool isEmpty(node *stack)
- {
- if (stack)
- {
- return false;
- }
- else return true;
- }
- void showTop(node *stack)
- {
- if (isEmpty(stack))
- {
- cout << "stack is empty!" << endl;
- }
- else cout << stack->val<<endl;
- }
- void push(node *&stack, int v)
- {
- node * e = new node;
- e->val = v;
- e->next = stack;
- stack = e;
- }
- void pop(node *&stack)
- {
- if (!isEmpty(stack))
- {
- node * e = stack;
- stack = stack->next;
- delete e;
- }
- }
- int main()
- {
- node * head = nullptr;
- node * tail = nullptr;
- node * stack = nullptr;
- pop(stack);
- showTop(stack);
- pop(stack);
- showTop(stack);
- push(stack, 5);
- showTop(stack);
- push(stack, 1);
- showTop(stack);
- push(stack, 3);
- showTop(stack);
- pop(stack);
- showTop(stack);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement