0% found this document useful (0 votes)
19 views9 pages

Stacks

Uploaded by

vedantb062
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

Stacks

Uploaded by

vedantb062
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Data structures

Stacks

Mrs. P. S. Mahajan
Lecturer in Computer Engineering
Government polytechnic Thane
Stack
*A Stack is a linear data structure that follows the LIFO
(Last-In-First-Out) principle.
*Stack has one end, whereas the Queue has two ends
(front and rear).
*It contains only one pointer top pointer pointing to the
topmost element of the stack.
*Whenever an element is added in the stack, it is added
on the top of the stack, and the element can be deleted
only from the stack.
*In other words, a stack can be defined as a container in
which insertion and deletion can be done from the one
end known as the top of the stack.
Stack
Primitive Operations on
Stack
*push(): When we insert an element in a stack then the
operation is known as a push. If the stack is full then the
overflow condition occurs.

*pop(): When we delete an element from the stack, the


operation is known as a pop. If the stack is empty means
that no element exists in the stack, this state is known as
an underflow state.

*isEmpty(): It determines whether the stack is empty or not.

*isFull(): It determines whether the stack is full or not.'

*Top(): It returns the element at the given position.


PUSH operation
• Before inserting an element in
• Function to Push an element into
a stack, we check whether the
the stack
stack is full.
• If we try to insert the element
in a stack, and the stack is full, Void PUSH(int item)
then the overflow condition {
occurs. if(top == Size -1)
• When we initialize a stack, we {
set the value of top as -1 to Printf(“ Stack is Full/ Overflow”);
check that the stack is empty. Exit(0);
• When the new element is }
pushed in a stack, first, the Else
value of the top gets {
incremented, i.e., top=top+1, Printf(“Enter element to be inserted”);
and the element will be placed
scanf(“%d”,&item);
at the new position of the top.
top++;
• The elements will be inserted
St[top]=item;
until we reach the max size of
the stack. }
}
POP operation
• Function to Pop an element
* Before deleting the from the stack
element from the stack,
we check whether the int POP( )
stack is empty. {
* If we try to delete the Int item;
element from the empty if(top == -1)
stack, then the underflow {
Printf(“ Stack is Empty / Underflow”);
condition occurs.
Exit(0);
* If the stack is not empty, }
we first access the Else
element which is pointed {
by the top Item=st[top];
Top--;
* Once the pop operation is }
performed, the top is return(item);
decremented by 1, i.e., }
top=top-1.
Program for implementation of stack
using array.
#include<stdio.h> Switch(ch)
int max=10; {
int st[max], top=-1; Case 1: PUSH( );
break;
Void main( ) Case 2: POP( );
{ break;
int ch, ans=1; Case 3: DISPLAY( );
Do break;
Printf(“Stack Operation”); Case 4: exit(0);
Printf(“1. Push/ Element Insertion”); break;
Printf(“2. POP/ Element Deletion”); default: Printf(“Wrong Choice”);
Printf(“3. Display”); }
Printf(“4. Exit”); Printf(“do you want to continue?[1/0]”);
Printf(“Enter your choice”); Scanf(“%d”,&ans);
Scanf(“%d”,&ch); }while(ans==1);
getch( );
}
Applications of the stack

*Reversing a List
*Conversion of Infix to Postfix expression
*Evaluation of Postfix Expression
*Conversion of Infix to Prefix expression
*Evaluation of Prefix Expression
*Recursion
*Tower of Hanoi
Thank You

You might also like