You are on page 1of 6

Chapter 3 -Stack

Stack: Stack is a linear data structure that stores all elements in a specific sequence. Stack has only one
end (pointer) to perform operations on stack such as insertion, deletion and traversal. This end (pointer) is
called as stack top.
As stack has only one end, insertion and deletion performed only from one end i.e. stack top, so stack
behaves in LIFO manner. LIFO is Last In first Out that means last inserted element into a stack is removed
first from the stack.

Array representation:
In the below figure, stack is declared with maximum 3 elements. First inserted element is stored at the 0th
index position; second element is stored at second index position and so on.

Primitive operations on stack:


1. push() : This operation is used to insert an element into a stack.
2. pop() : This operation is used to delete an element from stack.
Stack top: It is a pointer used to insert and delete an element to and from stack. Initially, it is initialized
with -1 value when implemented with an array.

Push operation: It is used to insert an element into a stack. Inserting an element requires increment of
stack top pointer by one index position. Before inserting an element, memory space availability inside stack
is checked. If space is available then stack top pointer is a incremented by one and new element is inserted
at the position indicated by stack top pointer. If space is not available then push operation is discarded.

Stack full: When stack contains maximum number of elements and stack top is initialize to array size-1
(max-1) then stack is said to be full.

Stack overflow: When stack is full and push operation is called then stack is said to be in overflow state.
Algorithm for push( ) operation:
Step 1 : Start (call to push function with argument value to be stored inside the stack)
Step 2 : Check for stack overflow state
If stack top pointer is initialize to max-1 position then
Display a message as ‘stack is full’ and return to calling function
Otherwise
Go to step 3
Step 3 : Increment stack pointer by one index position
Step 4 : Store new element value at the index position indicated by stack top
Step 5 : Return to calling function.

Pop operation: It is used to delete an element from stack. When pop operation is called an element
stored at the stack top position is deleted. Deleting an element from stack, decrements stack top pointer by
one index position.

Stack empty: When stack does not contain any element and stack top is initialize to -1 value then stack is
said to be empty.

Stack underflow: When stack is empty and pop operation is called then stack is said to be in underflow
state.

Algorithm for pop operation:


Step 1 : Start (call to pop function)
Step 2 : Check for stack underflow state
If stack top is initialize to -1 value then
Display message as ‘stack is empty’ and return to calling function
Otherwise
Go to next step
Step 3 : Store value stored at stack top index position into a temporary variable.
Step 4 : Decrement stack top by one index position
Step 5 : Display value of temporary variable as deleted
Step 6 : Return to calling function
Program for implementation of stack (without function):
#include <stdio.h> Output:
#include<conio.h> Menu: 1. push 2. pop 3. display
#define max 5 Enter your choice1
void main() Enter number:10
{ Push operation is done successfully
int a[max],st,i,choice,no,temp; Do you want to continue (1 for yes/2 for no) : 1
st=-1;
do Menu: 1. push 2. pop 3. display
{ Enter your choice1
printf("Menu: 1. push \t 2. pop \t 3. display"); Enter number:20
printf("\n Enter your choice"); Push operation is done successfully
scanf("%d",&choice); Do you want to continue (1 for yes/2 for no) : 1
switch(choice)
{ Menu: 1. push 2. pop 3. display
case 1: Enter your choice1
printf("\n Enter number:"); Enter number:30
scanf("%d",&no); Push operation is done successfully
if(st==max-1) Do you want to continue (1 for yes/2 for no) : 1
{
printf("\nStack is full"); Menu: 1. push 2. pop 3. display
break; Enter your choice1
} Enter number:40
else Push operation is done successfully
{ Do you want to continue (1 for yes/2 for no) : 1
st=st+1;
a[st]=no; Menu: 1. push 2. pop 3. display
printf("\n Push operation is done successfully"); Enter your choice1
} Enter number:50
break; Push operation is done successfully
case 2: Do you want to continue (1 for yes/2 for no) : 1
if(st==-1)
{ Menu: 1. push 2. pop 3. display
printf("\nStack is empty"); Enter your choice1
break; Enter number:60
} Stack is full
else Do you want to continue (1 for yes/2 for no) : 1
{
temp=a[st]; Menu: 1. push 2. pop 3. display
st=st-1; Enter your choice2
printf("Element deleted from stack is %d",temp); Element deleted from stack is 50
} Do you want to continue (1 for yes/2 for no) : 1
break;
case 3: Menu: 1. push 2. pop 3. diaplay
if(st==-1) Enter your choice3
{ Stack elements are: 10 20 30 40
printf("\nStack is empty");
break;
}
else
{
printf("\n Stack elements are:");
for(i=0;i<=st;i++)
printf("\t%d",a[i]);
}
break;
default:
printf("\nInvalid choice");
}
printf("\n Do you want to continue (1 for yes/2 for
no) : ");
scanf("%d",&choice);
}while(choice==1);
getch();
}

Program using function: Output is same as above


#include <stdio.h>
#define max 5
int a[max],st;
void push();
void pop();
void display();
int main()
{
int choice;
st=-1;
do
{
printf("Menu: 1. push \t 2. pop \t 3. display");
printf("\n Enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
printf("\nInvalid choice");
}
printf("\n Do you want to continue (1 for yes/2 for no) : ");
scanf("%d",&choice);
}while(choice==1);
return 0;
}
void push()
{
int no;
printf("\n Enter number");
scanf("%d",&no);
if(st==max-1)
{
printf("\nStack is full");
}
else
{
st=st+1;
a[st]=no;
printf("\n Push operation is done successfully");
}
}
void pop()
{
int temp;
if(st==-1)
{
printf("\nStack is empty");
}
else
{
temp=a[st];
st=st-1;
printf("Element deleted from stack is %d",temp);
}
}
void display()
{
int i;
if(st==-1)
{
printf("\nStack is empty");
}
else
{
printf("\n Stack elements are:");
for(i=0;i<=st;i++)
printf("\t%d",a[i]);
}
}
Stack as an ADT:
ADT is defined as a mathematical model that describe components required to implement a data structure
and set of operations that can be performed on data.

1. Components: For stack implementation two components are required.


I) Array[size] - It is used to store stack elements.
II) Stack top - It is a pointer used to perform operations on stack.

Structure for stack:


struct stack
{
int a[5];
int st;
};

2. Operations on stack:
I) Initialize: This operation is used to initialize stack top to -1 index value.
II) Stack empty: This operation is used to check stack underflow.
III) Stack full: This operation is used to check stack overflow.
IV) Push: This operation is used to insert an element in a stack.
V) Pop: This operation is used to delete an element from stack.
VI) Display/traverse: This operation is used to visit each element from stack at least once.

Stack representation:

You might also like