From be639427299ce4309b7d3bca60ba0ab3ec917abe Mon Sep 17 00:00:00 2001 From: snm2824 <66302258+snm2824@users.noreply.github.com> Date: Fri, 2 Oct 2020 18:09:29 +0530 Subject: [PATCH] Stack as singly Linked List in C++ Implementing a linked list based data structure :Stack ,in C++ --- Stack as Linked list | 83 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Stack as Linked list diff --git a/Stack as Linked list b/Stack as Linked list new file mode 100644 index 0000000..29ba25d --- /dev/null +++ b/Stack as Linked list @@ -0,0 +1,83 @@ +// Stack as Linked list in C++ + +#include +using namespace std; + + +struct node { + + int data; + node* next; +}*ptr,*top=NULL; +void insertion(){ + ptr=new node; + cout<<"\nEnter data:"; + cin>>ptr->data; + ptr->next=NULL; + if(top==NULL){ + top=ptr; + } + else{ + cout<<"\nPopped item is"<data<<; + ptr->next=top; + top=ptr; + } +} +void deletion(){ + if(top==NULL){ + cout<<"\nUnderflow condition"; + } + else{ + ptr=top; + top=top->next; + delete ptr; + } + +} +void display(){ + if(top==NULL){ + cout<<"\nStack is empty"; + } + else{ cout<<"\nStack:"; + ptr=top; + while(ptr!=NULL){ + cout<data<<"\n"; + ptr=ptr->next; + } + } +} + +int main() { + char h; + + int n; + + do{ + + cout<<"\n --Menu-- \n"<<"1.Push into stack\n"<<"2.Pop from stack\n"<<"3.Display values\n"<<"Enter option:"; + cin>>n; + switch(n){ + case 1:{ + insertion(); + break; + } + case 2:{ + deletion(); + break; + } + case 3:{ + display(); + break; + } + default:{ + cout<<"\nYou have entered wrong choice"; + break; + } + } + cout<<"\nDo you want to continue(y or n):"; + cin>>h; +}while(h=='y'); + + + return 0; +}