#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
class stack
{
struct node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void show();
};
void stack::push()
{
int value;
struct node *ptr;
cout<<"Enter a number to be inserted: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";
}
void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nTHE STACK IS EMPTY !!!";
return;
}
temp=top;
top=top->next;
cout<<"\n\n The popped value is.. "<<temp->data;
}
void stack::show()
{
if(top == NULL ){
cout<<"\nSTACK IS EMPTY..!!";
return;
}
else{
struct node *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
}}
void main()
{
clrscr();
stack s;
int choice;
choice=0;
while(choice!=4){
cout<<"\n\nSTACK USING LINKED LIST\n";
cout<<"1:PUSH\n2:POP\n3:VIEW\n4:QUIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:s.push();break;
case 2:s.pop();break;
case 3:s.show();break;
case 4:choice=4;break;
default:cout<<"\nINVALID COICE !! [ 1-4 ]";break;
}
}
}
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
class stack
{
struct node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void show();
};
void stack::push()
{
int value;
struct node *ptr;
cout<<"Enter a number to be inserted: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";
}
void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nTHE STACK IS EMPTY !!!";
return;
}
temp=top;
top=top->next;
cout<<"\n\n The popped value is.. "<<temp->data;
}
void stack::show()
{
if(top == NULL ){
cout<<"\nSTACK IS EMPTY..!!";
return;
}
else{
struct node *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
}}
void main()
{
clrscr();
stack s;
int choice;
choice=0;
while(choice!=4){
cout<<"\n\nSTACK USING LINKED LIST\n";
cout<<"1:PUSH\n2:POP\n3:VIEW\n4:QUIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:s.push();break;
case 2:s.pop();break;
case 3:s.show();break;
case 4:choice=4;break;
default:cout<<"\nINVALID COICE !! [ 1-4 ]";break;
}
}
}