Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node{
- int data;
- node *next;
- };
- class stack{
- node *top = new node;
- public:
- stack(){
- top = NULL;
- }
- void push(int v){
- node *nn = new node;
- nn -> data = v;
- nn -> next = top;
- top = nn;
- }
- int pop(){
- if(top == NULL){
- cout << "Stack is empty " << endl;
- return 0;
- }
- else {
- node *t = top;
- int val = top-> data;
- top = top -> next;
- free(t);
- return val;
- }
- }
- void display(){
- node *t=top;
- while(t!=NULL){
- cout << t->data <<" -> ";
- t=t->next;
- }
- cout<<"NULL\n";
- }
- };
- int main(){
- stack s;
- int ch = 10;
- while(ch != 0){
- cout << "Enter your choice" << endl;
- cout << "1: Push an element " << endl;
- cout << "2: Pop an element " << endl;
- cout << "0: Exit " << endl;
- cin >> ch;
- switch(ch){
- case 1:
- int value;
- cout << "Enter value to push" << endl;
- cin >> value;
- s.push(value);
- s.display();
- break;
- case 2:
- cout << "Popped element: " << s.pop() << endl;
- s.display();
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement