STACK representation of linked-list

/*
STACK representation of linkedlist.
*/
#include<iostream.h>
#include<conio.h>
#include<PROCESS.h>
struct node
{
int info;
node *next;
};

class stack
{
private:
node *top;
public:
stack();
void push(int);
int pop();
int peep();
};
stack::stack()
{
top=NULL;
}
void stack::push(int key)
{
node *nnode;
nnode=new node;

if(nnode==NULL)
{
cout<<“\nOverflow”;
}
else
{
nnode->info=key;
if(top==NULL)
{
nnode->next=NULL;
}
else
{
nnode->next=top;
}
top=nnode;
}
}

int stack::pop()
{
node *temp;
int key=-0;
if(top==NULL)
{
cout<<“\nUnderflow”;
}
else
{
key=top->info;
temp=top;
top=top->next;
delete(temp);
}
return(key);
}
int stack::peep()
{
int key=-0;
if(top==NULL)
{
cout<<“\nList is Empty.”;
}
else
{
key=top->info;
}
return(key);
}
int main()
{
stack s;
int n,ch;
char c;
clrscr();

do
{
cout<<“\nEnter=>”;
cout<<“\n1.Push”;
cout<<“\n2.Pop”;
cout<<“\n3.Peep”;
cout<<“\n4.Exit”;
cout<<“\nPlease Choose: “;
cin>>ch;
switch(ch)
{
case 1: cout<<“\nEnter Information to be Pushed: “;
cin>>n;
s.push(n);
break;
case 2: n=s.pop();
cout<<“\nPoped Information is “<<n;
break;
case 3: n=s.peep();
cout<<“\nTop most Information is “<<n;
break;
case 4: exit(1);
break;
case 5: cout<<“\nInvalid Entry.”;
};
cout<<“\nDo you wish to continue(Y/N): “;
cin>>c;

}while(c==’y’||c==’Y’);
exit(1);

getch();
return(0);
}
/*
OUTPUT:
——-
Enter=>
1.Push
2.Pop
3.Peep
4.Exit
Please Choose: 1

Enter Information to be Pushed: 5

Do you wish to continue(Y/N): y

Enter=>
1.Push
2.Pop
3.Peep
4.Exit
Please Choose: 1

Enter Information to be Pushed: 3

Do you wish to continue(Y/N): y

Enter=>
1.Push
2.Pop
3.Peep
4.Exit
Please Choose: 3

Top most Information is 3
Do you wish to continue(Y/N): n
*/

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.