/* Program on stack using linked list*/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include <process.h>
struct node
{
int info;
struct node *link;
};
struct node *top,*tmp,*q;
class rkslstack
{
public:
void push(void);
void pop(void);
void display(void);
};
void rkslstack::push()
{
int pushed_item;
cout<<"Input the new value to be pushed on to the stack : ";
cin>>pushed_item;
struct node* newnode = new node;
tmp=newnode;
tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}/*End of push()*/
void rkslstack::pop()
{
if(top == NULL)
cout<<"Stack underflow\n";
else
{
tmp=top;
cout<<"Deleted item is:"<<tmp->info;
top=top->link;
delete(tmp);
}
}/*End of pop()*/
void rkslstack::display()
{
if(top == NULL)
{
cout<<"\nStack is empty\n";
}
q=top;
cout<<"\nSingly linked List is: \n";
cout<<q->info;
while(q->link!=NULL)
{
q=q->link;
cout<<"||"<<q<<"----->"<<q->info;
}
cout<<"|| "<<NULL ;
}/*End of display() */
void main()
{
int choice;
rkslstack sl;
clrscr();
while(1)
{ cout<<"\n MENU \n";
cout<<"1.Push\n";
cout<<"2.Pop\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : " ;
cin>>choice;
switch(choice)
{
case 1:
sl.push();
break;
case 2:
sl.pop();
break;
case 3:
sl.display();
break;
case 4:
exit(1);
default :
cout<<"Wrong choice\n";
}/*End of switch */
}/*End of while */
}/*End of main() */
Output:
No comments:
Post a Comment