/**********************************************************
NAME
:- JABIR DAUD PATHAN
PROGRAM
:- IMPLEMENTATION OF STACK USING
SLL
**********************************************************/
/*---------------<< Header File
>>----------------*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct
node
{
int data;
struct node*next;
};
/*-------<< Declaration of class stack >>--------*/
class
stack
{
private :
node*top;
public :
stack() // Default constructor
{
top=NULL;
}
int empty(); // [ Functions
void push(int); // declaraton ]
int pop();
void display();
};
/*-------<< Function defination for empty stack
>>-------*/
int
stack :: empty()
{
if(top==NULL)
return 1;
return 0;
}
/*-------<< Function defination for push element into
stack >>-------*/
void
stack :: push(int k)
{
node*p;
p=new node;
p->data=k;
p->next=NULL;
if(top==NULL)
top=p;
else
{
p->next=top;
top=p;
}
}
/*-------<< Function defination for pop element from
stack >>-------*/
int
stack::pop()
{
int j;
node*p;
p=top;
top=top->next;
j=p->data;
free(p);
return j;
}
/*------<< Function defination for display contents of
stack >>-----*/
void
stack :: display()
{
node*p;
p=top;
cout<<"\n\nThe contents of stacks
are :";
if(top==NULL)
{
cout<<"\n\nThe stack is
empty.";
}
else
{
cout<<"\n\n Top-->";
while(p!=NULL)
{
cout<<" |
"<<p->data<<" |\n\t";
p=p->next;
}
cout<<" ---";
}
}
/*-------------<< Main Function Starts >>------------*/
void
main()
{
stack s;
int ch,j;
char ans;
do
{
clrscr();
cout<<"\n\t\t\t---<<
OPERATIONS ON STACK >>---";
cout<<"\n\n\t\t\t\t1.PUSH
ELEMENT";
cout<<"\n\t\t\t\t2.POP
ELEMENT";
cout<<"\n\t\t\t\t3.EXIT";
cout<<"\n\n\t\t\t-----------------------------";
cout<<"\n\n\t\t\tEnter your choice
: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the element to
be pushed : ";
cin>>j;
s.push(j);
s.display();
break;
case 2:
if(s.empty())
{
cout<<"\nError :
Underflow.....";
}
else
{
cout<<"\nThe poped element is
:"<<s.pop();
s.display();
}
break;
case 3:
exit(0);
break;
}
cout<<"\n\n\n\t\tDo you want to
continue(Y/N)? :";
ans=getche();
}while(ans=='Y'||ans=='y');
getch();
}
/*-------------<< End Of Main Function >>---------------*/
/*-----------------<< OUTPUT SCREEN
>>-------------------*/
---<< OPERATIONS ON STACK
>>---
1.PUSH ELEMENT
2.POP ELEMENT
3.EXIT
-----------------------------
Enter your choice : 1
Enter
the element to be pushed : 1
The
contents of stacks are :
Top--> | 1 |
---
Do you want to continue(Y/N)?
:Y
---<< OPERATIONS
ON STACK >>---
1.PUSH ELEMENT
2.POP ELEMENT
3.EXIT
-----------------------------
Enter your choice : 1
Enter
the element to be pushed : 2
The
contents of stacks are :
Top--> | 2 |
|
1 |
---
Do you want to continue(Y/N)?
:Y
---<< OPERATIONS
ON STACK >>---
1.PUSH ELEMENT
2.POP ELEMENT
3.EXIT
-----------------------------
Enter your choice : 1
Enter
the element to be pushed : 3
The
contents of stacks are :
Top--> | 3 |
|
2 |
| 1 |
---
Do you want to continue(Y/N)?
:Y
---<< OPERATIONS
ON STACK >>---
1.PUSH ELEMENT
2.POP ELEMENT
3.EXIT
-----------------------------
Enter your choice : 2
The
poped element is :3
The
contents of stacks are :
Top--> | 2 |
| 1 |
---
Do you want to continue(Y/N)?
:Y
---<< OPERATIONS
ON STACK >>---
1.PUSH ELEMENT
2.POP ELEMENT
3.EXIT
-----------------------------
Enter your choice : 2
The
poped element is :2
The
contents of stacks are :
Top--> | 1 |
---
Do you want to continue(Y/N)?
:Y
---<< OPERATIONS
ON STACK >>---
1.PUSH ELEMENT
2.POP ELEMENT
3.EXIT
-----------------------------
Enter your choice : 2
The
poped element is :1
The
contents of stacks are :
The
stack is empty.
Do you want to continue(Y/N)?
:Y
---<< OPERATIONS
ON STACK >>---
1.PUSH ELEMENT
2.POP ELEMENT
3.EXIT
-----------------------------
Enter your choice : 2
Error
: Underflow.....
Do you want to continue(Y/N)?
:Y
---<< OPERATIONS
ON STACK >>---
1.PUSH ELEMENT
2.POP ELEMENT
3.EXIT
-----------------------------
Enter your choice : 3
/*********************************************************************/
No comments:
Post a Comment