What is Stack ?

Stack is a linear data structure which follows a particular order in which the operations are performed.a Stack is open at one ends.

Sequence of Elements Processing : LIFO (Last-in-First-out)
Number of pointers used : 1 Pointer
Operations performed : Push and Pop
Representation : Vertical

Programming Source Code

/*

Stack is a linear data structure which follows a
particular order in which the operations are
performed.a Stack is open at one ends.

Sequence of Elements Processing : LIFO (Last-in-First-out)
Number of pointers used : 1 Pointer
Operations performed : Push and Pop
Representation : Vertical
*/

/* Include the library contents of the file for input stream to the compiler */
#include<iostream.h>
#include<conio.h>
#define size 10

/* stack structure*/
struct stack {
int stack_box[size];
int top;
}stack_call;

/* In case of Stack Full */
int stackFull()
{
if(stack_call.top>=size-1)
return 1;
else
return 0;
}

/* In case of Stack Empty */
int stackEmpty()
{
if(stack_call.top==-1)
return 1;
else
return 0;
}

/* Insert Data */
void push(int item)
{
stack_call.top++;
stack_call.stack_box[stack_call.top] =item;
}

/* Delete Data */
int pop()
{
int item;
item=stack_call.stack_box[stack_call.top];
stack_call.top --;
return(item);
}

/* Display data */
void display()
{
int i;
if(stackEmpty())
cout<<"\n Stack Is Empty!";
else
{
cout<<"\nElements of Stack : ";
for(i=stack_call.top;i>=0;i--)
cout<<" "<<stack_call.stack_box[i];
}
}

/*------Main Function-----*/

void main()
{
int item,ch;

stack_call.top=-1;

clrscr();

cout<<"\n Stack Implementation";
do
{
cout<<"\n Main Menu \n 1 : Push \n 2 : Pop \n 3 : Display \n 4 : Clear \n 5 : Exit";
cout<<"\n Select Operation : ";
cin>>ch;

switch(ch)
{
case 1:
cout<<"Enter the item to be pushed : ";
cin>>item;

if(stackFull())
cout<<"Stack is Full (Overflow)";
else
push(item);

break;

case 2:
if(stackEmpty())
{
cout<<"Stack is Empty (Underflow)";
}
else
{
item=pop();
cout<<"The popped element is "<<item;
}
break;

case 3: display();break;
case 4: clrscr(); break;
case 5:
cout<<"End of Program...";

}
}while(ch!=5);
getch();
}

Output 

 

(Visited 150 times, 1 visits today)
Share with Friends :
Written by:

Leave a Reply

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