0% found this document useful (0 votes)
11 views14 pages

Stack Operations in Data Structures

The document provides an overview of stacks, a Last In First Out (LIFO) data structure, detailing its operations including push, pop, peek, and display. It explains the conditions for overflow and underflow during these operations and includes code examples for implementing stack operations in C. Additionally, it mentions applications of stacks in various problems such as method tracking in processes and search functionalities.

Uploaded by

deepika4805
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)
11 views14 pages

Stack Operations in Data Structures

The document provides an overview of stacks, a Last In First Out (LIFO) data structure, detailing its operations including push, pop, peek, and display. It explains the conditions for overflow and underflow during these operations and includes code examples for implementing stack operations in C. Additionally, it mentions applications of stacks in various problems such as method tracking in processes and search functionalities.

Uploaded by

deepika4805
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

Stack

BCSE202L- Data Structures and Algorithm


Stack

BCSE202 – Data Structures and Algorithms 2


Introduction
• Access is allowed only at one point of the structure,
normally termed the top of the stack
• access to the most recently added item only
• Described as a "Last In First Out" (LIFO) data
structure

BCSE202 – Data Structures and Algorithms 3


Stack Operations
push

pop

create
STACK

peek

display
Push operation
• Before inserting an element in a stack, we check whether
the stack is full.
• If we try to insert the element in a stack, and the stack is
full, then the overflow condition occurs.
• When we initialize a stack, we set the value of top as -1 to
check that the stack is empty.
• When the new element is pushed in a stack, first, the value
of the top gets incremented, i.e., top=top+1, and the
element will be placed at the new position of the top.
• The elements will be inserted until we reach the max size of
the stack.
BCSE202 – Data Structures and Algorithms 5
Creating a Stack

int stack[5]; 4
int top = -1; 3

top -1 1

Empty Stack
Push operation of a stack
void push (int x)
{ push(10)
scanf(“%d”,&x) To check push(5)
whether stack 6 4
is full push(24)
if(top==N-1) 4 3
printf(“Overflow”) push(42)
2
push(6) 2 2
4
else To insert the 5 1
push(50)
{ element into
1 0
stack
top++; 0
stack[top]=x;
}
}
top -1
Pop Operation

• Before deleting the element from the stack, we check


whether the stack is empty.
• If we try to delete the element from the empty stack,
then the underflow condition occurs.
• If the stack is not empty, we first access the element
which is pointed by the top
• Once the pop operation is performed, the top is
decremented by 1, i.e., top=top-1.

BCSE202 – Data Structures and Algorithms 8


Pop operation of a stack
void pop () To check
{ pop()
whether stack
is empty pop()
if(top == -1)
4
printf(“Underflow”) pop()
To delete the 3
else element into pop()
{ stack top 2 2
4
printf(“%d”,stack[top]) 5 1
top--;
1 0
} 0
}

-1
Peek

void peek ()
{
if(top == -1)
printf(“Stack is Empty”) top 6 4
else
4 3
printf(“%d”,stack([top]) 2
} 2 2
4
5 1

1 0
Output: 6 0
display

void display ()
{
if(top == -1)
printf(“Stack is Empty”) top 6 4
else
4 3
{ 2
for (int i=top; i>=0; i--) 2 2
{ 4
5 1
printf(“%d”,stack([i])
} 1 0
0
}
}
Output: 6 42 24 5 10
Stack using Linked List

Template to create a Node

struct Node
{
int data;
struct Node *next;
} Struct node *top =
NULL;
Problems that Use Stacks
• The runtime stack used by a
process (running program) to
keep track of methods in
progress
• Search problems
• Undo, redo, back, forward

13
ThankQ

BCSE202 – Data Structures and Algorithms 14

You might also like