Professional Documents
Culture Documents
Sttacks v2 14062021
Sttacks v2 14062021
Definition
A linear data structure that
follows the LIFO rule for data
storage and retrieval.
LIFO: Last in First out implies
that the element added last would
be the first to be retrieved i.e. the
order of retrieval is opposite to the
order of storage.
Elements are removed from a stack
in the reverse order of that in which
they were inserted into the stack.
List of elements in which an
element may be inserted or deleted
only at one end,called the top of
the stack.
Basic Operations on a Stack
Static:
Use an array with restricted operation set
Advantage: Time saved
Disadvantage: Wastage of memory for unused
locations.
Static Implementation
In case of static implementation
an array is used as a stack
by restricting the locations at which store /
retrieve operations can be carried out.
Valid set of operations are:
Add at end: Push
Remove from end: Pop
Retrieve the topmost element without
removing: Peek
Check if stack is empty: IsEmpty
Check if stack is full: IsFull
Data Components: Static
Implementation
The data components required for static
implementation of stacks are:
<datatype> arr [MAX];
int top;
The component arr is the actual data storage
area.
The component top is an integer that is used
in order to maintain the location where the
next Push / Pop operation would place.
(initialized to -------)
Exceptional Conditions
While Adding (Push Operation)
If the stack is full
Adding more elements would lead to a condition
called
Stack Overflow
Must be taken care of while adding elements
While Removing (Pop Operation)
If the stack is empty
Trying to remove an element would lead to a
condition called
Stack Underflow
Must be taken care of while removing elements
Memory Map
PUSH Operation
Step1−Checks if the stack is full.
Step2−If the stack is full,produce an error and exit.
Step3−If the stack is not full,increment top to point next empty
space.
Step4−Add data element to the stack location,where top is
pointing.
Step5−Return success.
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
getch();
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
POP operation
Step1−Check if the stack is empty.
Step2−If the stack is empty,produce an error and exit.
Step3−If the stack is not empty,access the data element
at which top is pointing.
Step4 -decrement the value of top by one.
Step5−Return success.
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
Lab Exercise
Create a Menu driven program to perform
Push and Pop operations in a Stack where
stack is implemented as an array.
Implementation
//stack using array
#include<stdio.h>
#include<conio.h>
int stack[100],choice,n,top,x,i;
void push();
void pop();
void display();
void main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
getch();
}
}
while(choice!=4);
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
getch();
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}