STACK using SLL

, by Prashant Gunjal


/*
Stack using link list

*/
#include<iostream.h>
#include<conio.h>

struct sll
{
int data;
struct sll * next;
};
typedef struct sll node;
node * s,*top;

class stack
{
public:
node * temp;
stack()
{
s=top=NULL;
}
void push();
void pop();
void display();
};
void stack::push()
{
cout<<"\nEnter data to be insert : ";
if(top==NULL)
{
temp=new node;
temp->next=NULL;
cin>>temp->data;
s=top=temp;
}
else
{
temp=new node;
temp->next=NULL;
cin>>temp->data;
temp->next=top;
top=temp;
}
}
void stack::display()
{
if(top==NULL)
{
cout<<"\nEmpty Stack ";
}
else
{
cout<<"\nData of Stack : ";
temp=top;
while(temp!=NULL)
{

cout<<"\t"<<temp->data;
temp=temp->next;
}
}
}
void stack::pop()
{

if(top==NULL)
{
cout<<"\nNot possible ";
}
else
{
cout<<"\ndata removed : "<<top->data;
top=top->next;
if(top==NULL)
s=NULL;

}
}
void main()
{
stack q;
int ch;
clrscr();
do
{
cout<<"\n1.push \n2.Display \n3.pop\n4.exit";
cout<<"\nEnter your choice no : ";
cin>>ch;
switch(ch)
{
case 1:
q.push();
break;
case 2:
q.display();
break;
case 3:
q.pop();
break;
}
}while(ch!=4);
getch();
}
/*

1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 1                                                      
                                                                               
Enter data to be insert : 12
                                                                               
1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 1                                                      
                                                                               
Enter data to be insert : 13                                                  
                                                                               
1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 1                                                      
                                                                               
Enter data to be insert : 14                                                  
                                                                               
1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 2                                                      
                                                                               
Data of Stack :         14      13      12                                    
1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 3                                                      
                                                                               
data removed : 14                                                              
1.push                                                                        
2.Display                                                                      
3.pop                                                                          
4.exit                                                                        
Enter your choice no : 4                                                      


*/

0 comments: