Skip to content

Commit

Permalink
Merge pull request #1 from snm2824/snm2824-patch-1
Browse files Browse the repository at this point in the history
Stack as singly Linked List in C++
  • Loading branch information
snm2824 authored Oct 2, 2020
2 parents bb8150c + be63942 commit d1653c8
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Stack as Linked list
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Stack as Linked list in C++

#include <bits/stdc++.h>
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"<<ptr->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<<ptr->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;
}

0 comments on commit d1653c8

Please sign in to comment.