#include<iostream>
using namespace std;
void push();
void pop();
void display();
struct stack
{
int data;
struct stack *next;
}*top=NULL;
typedef struct stack node;
main()
{
int choice;
do
{
cout<<"\n 1. Push";
cout<<"\n 2. Pop ";
cout<<"\n 3.Display";
cout<<"\n Enter the choice :";
cin>>choice;
switch(choice)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
default : cout<<"\n Wrong choice! ";
break;
}
}while(choice>0&&choice<4);
}
void push()
{
int item;
node *temp;
// temp=new node;
cout<<"\n Enter the item to insert ";
cin>>temp->data;
temp->next=top;
top=temp;
}
void pop()
{
if(top==NULL)
cout<<"\n No item to pop ";
else
{
node *temp;
temp=top;
cout<<"\n item popped is "<<temp->data;
top=top->next;
delete temp;
}
}
void display()
{
node *temp;
if(top==NULL)
cout<<"\n No item to display ";
else
{
temp=top;
while(temp->next!=NULL)
{
cout<<"\n "<<temp->data;
temp=temp->next;
}
cout<<"\n "<<temp->data;
}
}
using namespace std;
void push();
void pop();
void display();
struct stack
{
int data;
struct stack *next;
}*top=NULL;
typedef struct stack node;
main()
{
int choice;
do
{
cout<<"\n 1. Push";
cout<<"\n 2. Pop ";
cout<<"\n 3.Display";
cout<<"\n Enter the choice :";
cin>>choice;
switch(choice)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
default : cout<<"\n Wrong choice! ";
break;
}
}while(choice>0&&choice<4);
}
void push()
{
int item;
node *temp;
// temp=new node;
cout<<"\n Enter the item to insert ";
cin>>temp->data;
temp->next=top;
top=temp;
}
void pop()
{
if(top==NULL)
cout<<"\n No item to pop ";
else
{
node *temp;
temp=top;
cout<<"\n item popped is "<<temp->data;
top=top->next;
delete temp;
}
}
void display()
{
node *temp;
if(top==NULL)
cout<<"\n No item to display ";
else
{
temp=top;
while(temp->next!=NULL)
{
cout<<"\n "<<temp->data;
temp=temp->next;
}
cout<<"\n "<<temp->data;
}
}
No comments:
Post a Comment