#include <iostream>
using namespace std;
int flag=1;
struct STACK
{
int data;
struct STACK *next;
};
typedef struct STACK stack ;
stack *head=NULL;
int count=0;
void PUSH()
{
stack *temp=new stack();
cout<<"Data: ";
cin>>temp->data;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
stack *last=head;
while(last->next!=NULL)
{
last = last->next;
}
last->next=temp;
}
count++;
return;
}
void POP()
{
if(count<=0)
{
cout<<"Linked List is Empty";
return;
}
else
{
stack *temp = head;
int i=1;
if(count==1)
{
head=NULL;
delete(temp);
count--;
}
else{
while(i != count-1)
{
temp = temp->next;
i++;
}
delete(temp->next);
temp->next=NULL;
count--;
}
return;
}
}
void print()
{
stack *temp =head;
if(head==NULL)
{
cout<<"No Data Found";
}
else
{
cout<<"Your Linked List: ";
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
cout<<endl;
}
void menu()
{
int n;
cout<<"Press 1 to PushPress 2 to popPress 3 to PrintPress 0 to Exit";
cin>>n;
if(n==1) PUSH();
else if(n==2) POP();
else if(n==3) print();
else if(n==0) flag=0;
}
int main()
{
while(flag==1)
{
menu();
}
return 0;
}
Output
Press 1 to Push
Press 2 to pop
Press 3 to Print
Press 0 to Exit
1
Data: 1
Press 1 to Push
Press 2 to pop
Press 3 to Print
Press 0 to Exit
1
Data: 2
Press 1 to Push
Press 2 to pop
Press 3 to Print
Press 0 to Exit
3
Your Linked List: 1 2
Press 1 to Push
Press 2 to pop
Press 3 to Print
Press 0 to Exit
2
Press 1 to Push
Press 2 to pop
Press 3 to Print
Press 0 to Exit
3
Your Linked List: 1
Press 1 to Push
Press 2 to pop
Press 3 to Print
Press 0 to Exit
0
Add your comment