SlideShare a Scribd company logo
2
Most read
3
Most read
Stack
Stack is a linear list where any element is added at the top of the list and any element is deleted
(accessed) from the top of the list. So, for stack an indicator or pointer must be used to indicate or
point the top element of the stack.
Add operation for a stack is called “push” operation and deletion operation is called “pop”
operation. Stack is a LIFO (Last In First Out) structure. That means the element which was added
last will be deleted or accessed first.
The elements of a stack are added from bottom to top, means push operation is performed from
bottom to top. The elements are deleted from top to bottom, which means pop operation is
performed from top to bottom.
Stack can be implemented using two ways; using array and using linked list.
8
7
6
5 1923
4 1452
3 2315
2 1245
1 1025
(a) Array based stack
40 48 59 67 63
(b) A link based stack
Fig. 1: Graphical representation of stack
Array Based Stack
The stack, which is implemented using array is called array based stack. To create an array based
stack, at first we have to declare an array with required size.
Push Operation
Push operation means to add an element to a stack.
Here, we shall use array based stack. So, an array will be treated as a stack. We need an indicator
or index identifier to add element to the stack and this indicator will mark the top of the stack. To
add an element we have to check whether the array is already full or not. If the array is already full
then we cannot add any element, otherwise we can.
Here, top is an indicator indicates the top element of the stack and item is an element to be added
to the stack, M is the size of the stack (array). Overflow occurs when we try to insert an element
into the stack, which is already full.
top
(top=5
)
6
5
4 D
3 Q
2 F
1 A
(a) Before push
(b) After push
Fig. 2: Pictorial view of push operation
Algorithm to add an element to a stack
1. Declare the stack and top:
stack[[1…..M], top;
2, Add an item in the stack:
if(top<M)
{
top=top+1;
stack[top]=item;
}
else print “Over Flow”;
3. Output will be the updated stack.
Pop Operation
Pop operation means to delete (access) an element from a stack. Here, top is an indicator indicates
the top element of the stack, M is the size of the array and x is a variable where we access top
element of the stack.
6
5
4 D
3 Q
2 F
1 A
(a) Before pop (b) After pop
Fig. 3: Pictorial view of pop operation
6
5 H
4 D
3 Q
2 F
1 A
6
5 H
4 D
3 Q
2 F
1 A
top
(top=4
)
top
(top=5
)
top
(top=5
)
top
(top=4
)
Algorithm to delete an element from a stack
1. Declare the stack and top:
stack[1…..M], top;
2. Access the top element:
if (top==0) print “stack is empty”;
else {
x=stack[top];
top=top-1;
}
3. Output: updated list.
Sample Code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void show(int stack[],int p)
{
printf("nThe stack is:n");
for(int j=1;j<=p;j++)
printf("%d ",stack[j]);
printf("n");
}
int main()
{
int i=0,n,a[50];
printf("Enter maximum size of stack:n");
scanf("%d",&n);
printf("n1.Pushn2.Popn3.Exitn");
while(1)
{
printf("Enter your choice: ");
switch(getche())
{
case '1':if(i<n)
{
printf("nEnter element:");
i++;
scanf("%d",&a[i]);
show(a,i);
}
else
printf("nThe stack is
overflow.n");
break;
case '2':if(i>0)
{i--;
show(a,i);}
else
printf("nThe stack is
underflow.n");
break;
case '3': exit(0);
default: printf("nInvalid Selection.n") ;
break;
}
}
}
Sample Output
Case 1
Enter maximum size of stack:
3
1.Push
2.Pop
3.Exit
Enter your choice: 1
Enter element:5
The stack is:
5
Enter your choice: 1
Enter element:6
The stack is:
5 6
Enter your choice: 1
Enter element:7
The stack is:
5 6 7
Enter your choice: 1
The stack is overflow.
Enter your choice: 3
Case 2:
Enter maximum size of stack:
3
1.Push
2.Pop
3.Exit
Enter your choice: 2
The stack is underflow.
Enter your choice: 1
Enter element:2
The stack is:
2
Enter your choice: 1
Enter element:5
The stack is:
2 5
Enter your choice: 1
Enter element:8
The stack is:
2 5 8
Enter your choice: 1
The stack is overflow.
Enter your choice: 2
The stack is:
2 5
Enter your choice: 2
The stack is:
2
Enter your choice: 2
The stack is:
Enter your choice: 2
The stack is underflow.
Enter your choice: 3

More Related Content

PPTX
Stack and its operations
PPT
Stacks
PPTX
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
PDF
PPTX
Different types of Linked list.
PPTX
My lectures circular queue
PPTX
Quick sort
PPT
Queue Data Structure
Stack and its operations
Stacks
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Different types of Linked list.
My lectures circular queue
Quick sort
Queue Data Structure

What's hot (20)

PPSX
PPSX
Data Structure (Stack)
PPT
Queue implementation
PPTX
Binary Tree in Data Structure
PPTX
Data structure and its types
PPTX
Doubly Linked List
PPTX
Queue Implementation Using Array & Linked List
PPT
PPTX
Heap Sort in Design and Analysis of algorithms
PPSX
Data structure stack&queue basics
PPTX
Stack using Linked List
PPTX
Queue - Data Structure - Notes
PDF
Data Structures & Algorithm design using C
PPTX
File in C language
PPTX
Bubble sort
PPTX
Queue and its operations
PPTX
PPTX
Threaded Binary Tree.pptx
PPTX
Java Stack Data Structure.pptx
Data Structure (Stack)
Queue implementation
Binary Tree in Data Structure
Data structure and its types
Doubly Linked List
Queue Implementation Using Array & Linked List
Heap Sort in Design and Analysis of algorithms
Data structure stack&queue basics
Stack using Linked List
Queue - Data Structure - Notes
Data Structures & Algorithm design using C
File in C language
Bubble sort
Queue and its operations
Threaded Binary Tree.pptx
Java Stack Data Structure.pptx
Ad

Similar to Stack push pop (20)

PPTX
Stacks in Data Structure
PDF
STACK ( LIFO STRUCTURE) - Data Structure
PPT
Lect 15-16 Zaheer Abbas
DOCX
DSA- Unit III- STACK AND QUEUE
PDF
Chapter 5 Stack and Queue.pdf
PPTX
STACK.pptx
PDF
The Stack (Data Structccccccccccccccccccc
PDF
PDF
LEC3-DS ALGO(updated).pdf
PDF
04 stacks
PPTX
The presentation on stack data structure
PPTX
5.-Stacks.pptx
PPTX
6 - STACKS in Data Structure and Algorithm.pptx
PDF
Stack concepts by Divya
PPTX
Stack and its operation implemented with array new - Copy.pptx
PPTX
Data Structure ARRAY REPRESENTATION OF STACKS
DOCX
Stacks in data structure
PPTX
STACK.pptx
DOCX
Stack operations using array
Stacks in Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
Lect 15-16 Zaheer Abbas
DSA- Unit III- STACK AND QUEUE
Chapter 5 Stack and Queue.pdf
STACK.pptx
The Stack (Data Structccccccccccccccccccc
LEC3-DS ALGO(updated).pdf
04 stacks
The presentation on stack data structure
5.-Stacks.pptx
6 - STACKS in Data Structure and Algorithm.pptx
Stack concepts by Divya
Stack and its operation implemented with array new - Copy.pptx
Data Structure ARRAY REPRESENTATION OF STACKS
Stacks in data structure
STACK.pptx
Stack operations using array
Ad

More from A. S. M. Shafi (20)

DOCX
Data Warehouse Schema (Star, Snowflake).docx
PDF
Correlation Analysis in Machine Learning.pdf
PDF
Naive Bayes and Decision Tree Algorithm.pdf
PDF
Frequent Pattern Growth Mining Algorithm.pdf
PDF
Direct Hashing and Pruning Algorithm in Data MIning.pdf
PDF
Association Rule Mining with Apriori Algorithm.pdf
PDF
HITS Algorithm in Data and Web MIning.pdf
PDF
Page Rank Algorithm in Data Mining and Web Application.pdf
PDF
K Nearest Neighbor Classifier in Machine Learning.pdf
PDF
K Means Clustering Algorithm in Machine Learning.pdf
PDF
2D Transformation in Computer Graphics
PDF
3D Transformation in Computer Graphics
PDF
Projection
PDF
2D Transformation
PDF
Line drawing algorithm
PDF
Fragmentation
PDF
File organization
PDF
Bankers algorithm
PDF
RR and priority scheduling
PDF
Fcfs and sjf
Data Warehouse Schema (Star, Snowflake).docx
Correlation Analysis in Machine Learning.pdf
Naive Bayes and Decision Tree Algorithm.pdf
Frequent Pattern Growth Mining Algorithm.pdf
Direct Hashing and Pruning Algorithm in Data MIning.pdf
Association Rule Mining with Apriori Algorithm.pdf
HITS Algorithm in Data and Web MIning.pdf
Page Rank Algorithm in Data Mining and Web Application.pdf
K Nearest Neighbor Classifier in Machine Learning.pdf
K Means Clustering Algorithm in Machine Learning.pdf
2D Transformation in Computer Graphics
3D Transformation in Computer Graphics
Projection
2D Transformation
Line drawing algorithm
Fragmentation
File organization
Bankers algorithm
RR and priority scheduling
Fcfs and sjf

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
master seminar digital applications in india
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Business Ethics Teaching Materials for college
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
RMMM.pdf make it easy to upload and study
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
01-Introduction-to-Information-Management.pdf
PDF
Classroom Observation Tools for Teachers
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Pre independence Education in Inndia.pdf
O7-L3 Supply Chain Operations - ICLT Program
master seminar digital applications in india
Renaissance Architecture: A Journey from Faith to Humanism
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial disease of the cardiovascular and lymphatic systems
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Business Ethics Teaching Materials for college
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
RMMM.pdf make it easy to upload and study
human mycosis Human fungal infections are called human mycosis..pptx
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
01-Introduction-to-Information-Management.pdf
Classroom Observation Tools for Teachers

Stack push pop

  • 1. Stack Stack is a linear list where any element is added at the top of the list and any element is deleted (accessed) from the top of the list. So, for stack an indicator or pointer must be used to indicate or point the top element of the stack. Add operation for a stack is called “push” operation and deletion operation is called “pop” operation. Stack is a LIFO (Last In First Out) structure. That means the element which was added last will be deleted or accessed first. The elements of a stack are added from bottom to top, means push operation is performed from bottom to top. The elements are deleted from top to bottom, which means pop operation is performed from top to bottom. Stack can be implemented using two ways; using array and using linked list. 8 7 6 5 1923 4 1452 3 2315 2 1245 1 1025 (a) Array based stack 40 48 59 67 63 (b) A link based stack Fig. 1: Graphical representation of stack Array Based Stack The stack, which is implemented using array is called array based stack. To create an array based stack, at first we have to declare an array with required size. Push Operation Push operation means to add an element to a stack. Here, we shall use array based stack. So, an array will be treated as a stack. We need an indicator or index identifier to add element to the stack and this indicator will mark the top of the stack. To add an element we have to check whether the array is already full or not. If the array is already full then we cannot add any element, otherwise we can. Here, top is an indicator indicates the top element of the stack and item is an element to be added to the stack, M is the size of the stack (array). Overflow occurs when we try to insert an element into the stack, which is already full. top (top=5 )
  • 2. 6 5 4 D 3 Q 2 F 1 A (a) Before push (b) After push Fig. 2: Pictorial view of push operation Algorithm to add an element to a stack 1. Declare the stack and top: stack[[1…..M], top; 2, Add an item in the stack: if(top<M) { top=top+1; stack[top]=item; } else print “Over Flow”; 3. Output will be the updated stack. Pop Operation Pop operation means to delete (access) an element from a stack. Here, top is an indicator indicates the top element of the stack, M is the size of the array and x is a variable where we access top element of the stack. 6 5 4 D 3 Q 2 F 1 A (a) Before pop (b) After pop Fig. 3: Pictorial view of pop operation 6 5 H 4 D 3 Q 2 F 1 A 6 5 H 4 D 3 Q 2 F 1 A top (top=4 ) top (top=5 ) top (top=5 ) top (top=4 )
  • 3. Algorithm to delete an element from a stack 1. Declare the stack and top: stack[1…..M], top; 2. Access the top element: if (top==0) print “stack is empty”; else { x=stack[top]; top=top-1; } 3. Output: updated list. Sample Code #include<stdio.h> #include<conio.h> #include<stdlib.h> void show(int stack[],int p) { printf("nThe stack is:n"); for(int j=1;j<=p;j++) printf("%d ",stack[j]); printf("n"); } int main() { int i=0,n,a[50]; printf("Enter maximum size of stack:n"); scanf("%d",&n); printf("n1.Pushn2.Popn3.Exitn"); while(1)
  • 4. { printf("Enter your choice: "); switch(getche()) { case '1':if(i<n) { printf("nEnter element:"); i++; scanf("%d",&a[i]); show(a,i); } else printf("nThe stack is overflow.n"); break; case '2':if(i>0) {i--; show(a,i);} else printf("nThe stack is underflow.n"); break; case '3': exit(0); default: printf("nInvalid Selection.n") ; break; } } }
  • 5. Sample Output Case 1 Enter maximum size of stack: 3 1.Push 2.Pop 3.Exit Enter your choice: 1 Enter element:5 The stack is: 5 Enter your choice: 1 Enter element:6 The stack is: 5 6 Enter your choice: 1 Enter element:7 The stack is: 5 6 7 Enter your choice: 1 The stack is overflow. Enter your choice: 3
  • 6. Case 2: Enter maximum size of stack: 3 1.Push 2.Pop 3.Exit Enter your choice: 2 The stack is underflow. Enter your choice: 1 Enter element:2 The stack is: 2 Enter your choice: 1 Enter element:5 The stack is: 2 5 Enter your choice: 1 Enter element:8 The stack is: 2 5 8 Enter your choice: 1 The stack is overflow. Enter your choice: 2 The stack is:
  • 7. 2 5 Enter your choice: 2 The stack is: 2 Enter your choice: 2 The stack is: Enter your choice: 2 The stack is underflow. Enter your choice: 3