Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from the end of the container.
Operation On Stack:
- Create
- Insert
- Delete
- Display
Program to implement Stack using Linked List:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include<iostream> using namespace std; char ch; class node{ //Class Node Which Will Act as Node Of SLL public: int d; node *next; node(int data){ //Parametric Constructor next=NULL; d=data; } }; class stack{ node *top; //Create hn & cn objects of node class public: stack(){ top=NULL; //Make hn as Null } void push(int); //To Create SLL int pop(); void dis(); //To display SLL int emp(); }; int stack :: pop(){ if(emp()==1){ cout<<"nUnderflow"; return -1; } else{ node *p=top; top=top->next; int x=p->d; delete p; return x; } } void stack :: push(int d1){ node *p=new node(d1); p->next=top; top=p; } void stack :: dis(){ node *p=top; while(p!=NULL){ //It will move SLL until It doesn't found NULL cout<<p->d<<"n"; p=p->next; } } int stack :: emp(){ if(top==NULL){ return 1; } else{ return 0; } } int main(){ stack o; int d1,i,ch1; do{ cout<<"n1:Create Stack"; cout<<"n2:Empty"; cout<<"n3:Pop(Delete)"; cout<<"n4:Print"; cout<<"n5:Exit"; cout<<"nEnter U R Choice"; cin>>ch1; switch(ch1){ case 1: do{ cout<<"nEnter Data"; cin>>d1; o.push(d1); cout<<"nDO U WANT TO CONTINUE...Enter Your Choice y OR Y n"; cin>>ch; }while(ch=='y' || ch=='Y'); cout<<"n"; o.dis(); break; case 2: if(o.emp()==1){ cout<<"nEmpty Stack"; } else{ cout<<"nNot Empty"; } break; case 3: cout<<o.pop()<<" is popedn"; o.dis(); break; case 4: o.dis(); break; case 5: break; } }while(ch!=5); return(0); } |
Leave a Reply