0% found this document useful (0 votes)
39 views

Stacks and Queues

This document discusses stacks, which are linear data structures that follow the LIFO (last in, first out) principle. Stacks allow insertion and removal of elements from one end only, called the top. Common stack operations are push, which adds an element to the top, and pop, which removes an element from the top. The document provides examples of stacks and explains overflow and underflow conditions.

Uploaded by

Karthik Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Stacks and Queues

This document discusses stacks, which are linear data structures that follow the LIFO (last in, first out) principle. Stacks allow insertion and removal of elements from one end only, called the top. Common stack operations are push, which adds an element to the top, and pop, which removes an element from the top. The document provides examples of stacks and explains overflow and underflow conditions.

Uploaded by

Karthik Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Data Structures and Algorithms

Vellore-632014, Tamil Nadu, India


Outline

Stacks

Queues

CSE2003 Data Structures and Algorithms 12:42 AM Slide. 2


Stacks
 Stacks
• Stack is a linear data structure
• Every data structure is capable of storing the data
• Properties of stack:
– First and Last Out (FILO)
– Last in First Out (LIFO)

CSE2003 Data Structures and Algorithms 12:42 AM Slide. 3


Operations
 Stacks
 Operations • Stack is an ordered list of similar data type.
• Stack is a LIFO(Last in First out) structure or we
can say FILO(First in Last out).
• push()
– Adds an element at the top of the stack.
• pop()
– Deletes an element from the top of the stack.
• peek()
– Displays the topmost element in the stack.

CSE2003 Data Structures and Algorithms 12:42 AM Slide. 4


Operations
 Stacks
 Operations • is_empty()
– It will return True if the stack is Empty.
• is_full()
– It will return True if the stack is Full.
• Both insertion and removal are allowed at only one
end of Stack called Top.
• Stack is said to be in Overflow state when it is
completely full and is said to be in Underflow state
if it is completely empty.

CSE2003 Data Structures and Algorithms 12:42 AM Slide. 5


Operations
 Stacks
 Operations • Stack - It is having a one open end
• In stack Insertion operation is called push() And
Deletion is called pop()
pop push

4
3
2
1
0
Top= -1 Stack

CSE2003 Data Structures and Algorithms 12:42 AM Slide. 6


Performing Push() operation in stack
 Stacks
 Operations
 Push operation – 2 5 7 9 11
performance

Push(2) Push(5)

4 4
3 3
2 2
1 Top= 1 5
Top= 0 2 0 2
Stack Stack

CSE2003 Data Structures and Algorithms 12:42 AM Slide. 7


Push() operation
 Stacks
 Operations
 Push operation –
performance Push(11)
Push(7) Push(9)

11
4 4 Top= 4
3 Top=3 9 3 9
Top= 2 7 2 7 2 7
1 5 1 5 1 5
0 2 0 2 0 2
Stack Stack Stack

CSE2003 Data Structures and Algorithms 12:42 AM Slide. 8


Push() operation
 Stacks
 Operations • If the stack is_full and we call the push operation,
 Push operation – this condition is called Overflow condition.
performance

Overflow push

Top= 11
4
3 9
2 7
1 5
0 2
Stack

CSE2003 Data Structures and Algorithms 12:42 AM Slide. 9


Performing Pop() operation in stack
 Stacks
 Operations
 Push operation –
performance
 Pop operation – Pop() Pop()
performance
11
Top= 4 Top=4 4
3 9 3 9 Top= 3
2 7 2 7 2 7
1 5 1 5 1 5
0 2 0 2 0 2
Stack Stack Stack

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Pop() operation
 Stacks
 Operations
 Push operation –
performance
 Pop operation – Pop() Pop()
performance

4 4 4
3 3 3
Top= 2 2 2
1 5 Top= 1 1
0 2 0 2 Top= 0
Stack Stack Stack

CSE2003 Data Structures and Algorithms 12:42 AM Slide. 11


Pop() operation
 Stacks
 Operations • If the stack is_empty and we call the pop operation,
 Push operation – this condition is called Underflow condition.
performance
 Pop operation –
Underflow State
performance pop

4
3
2
1
0
Top= -1 Stack

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Real Time Examples
 Stacks
 Operations • A Stack of Plates
 Push operation – – A stack is the pile of dinner plates that you encounter
performance
when you eat at the local cafeteria: When you remove a
 Pop operation –
performance plate from the pile, you take the plate on the top of the
 Real time examples pile.
– But this is exactly the plate that was added ("inserted'')
most recently to the pile by the dishwasher.
– If you want the plate at the bottom of the pile, you must
remove all the plates on top of it to reach it.

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Real Time Examples
 Stacks
 Operations • In browsers
 Push operation – – The back button in a browser saves all the URLs you
performance
have visited previously in a stack.
 Pop operation –
performance – Each time you visit a new page, it is added on top of the
 Real time examples stack.
– When you press the back button, the current URL is
removed from the stack, and the previous URL is
accessed.

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Real Time Examples
 Stacks
 Operations • To reverse a word - Put all the letters in a stack and
 Push operation – pop them out. Because of the LIFO order of stack,
performance
 Pop operation –
you will get the letters in reverse order.
performance
 Real time examples
• A Stack of Books

• A Stack of CD’s

• Tower of Hanoi

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Towers of Hanoi
 Stacks
 Operations
• Only one disk can be moved among the towers at any given
 Push operation – time.
performance
• Only the "top" disk can be removed.
 Pop operation –
performance • No large disk can be placed over a small disk.
 Real time examples
 Towers of Hanoi

Start Temp Destination

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Towers of Hanoi
 Stacks
 Operations • Step 1:
 Push operation – – Move disk 1 from A to C
performance
 Pop operation –
performance
 Real time examples
 Towers of Hanoi

• Step 2:
– Move disk from tower A to B

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Towers of Hanoi
 Stacks
 Operations • Step 3
 Push operation – – Move disk from tower C to tower B
performance
 Pop operation –
performance
 Real time examples
 Towers of Hanoi • Step 4
– Move disk from tower A to tower C

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Towers of Hanoi
 Stacks
 Operations
• Step 5
 Push operation – – Move disk 1 from tower B to tower A.
performance
 Pop operation –
performance
 Applications
 Towers of Hanoi
• Step 6
– Move disk from tower B to tower C.

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Towers of Hanoi
 Stacks
 Operations • Step7
 Push operation – – Move disk 1 from tower A to tower C.
performance
 Pop operation –
performance
 Real time examples
 Towers of Hanoi

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Algorithm for Towers of Hanoi
 Stacks
 Operations • Step1: START
 Push operation – • Step2: if disks == 1
performance
 Pop operation – • print “move disk” from start to destination
performance
 Real time examples • Step3: return
 Algorithm
• Step4: set Hanoi to disk -1, start, destination, temp
 Towers of Hanoi
• print “move disk” from start to destination
• Set Hanoi to disks-1,temp,start, destination
• Step5: Stop

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Applications
 Stacks
 Operations • A Stack can be used for evaluating expressions
 Push operation – consisting of operands and operators.
performance
 Pop operation – • Stacks can be used for Backtracking, i.e., to check
performance parenthesis matching in an expression.
 Real time examples
 Algorithm • It can also be used to convert one form of
 Towers of Hanoi expression to another form.
 Applications of stacks
• It can be used for systematic Memory Management.

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Queues
 Stacks
 Queues • Queue is a linear data structure.
• Queue is a First In First Out (FIFO)

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Queues
 Stacks
 Queues • Enqueue() - Adds an element at rare / tail end of the
queue. If the queue is full, then it is an overflow
condition.
• Dequeue()- Deletes an element from the front / head
end of the queue. If the queue is empty, then it is an
underflow condition.
• peek()- Displays the front element in the queue.
• is_empty()- It will return True if the queue is
Empty.

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Queues
 Stacks
 Queues • is_full()- It will return True if the queue is Full.
• Insertion will be done on rare end and deletion will
be done on front end.
• Queue is said to be in Overflow state when it is
completely full and is said to be in Underflow state
if it is completely empty.

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Operations
 Stacks
 Queues • Queue - It is having a two open ends
• In Queue
– Insertion operation is called Enqueue()
– Deletion is called Dequeue()

0 1 2 3 4

Dequeue Enqueue

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Performing Enqueue() operation in Queue
 Stacks
 Queues 2 5 7 9 11
 Enqueue operation –
performance Enqueue(2)
0 1 2 3 4
2

Front = 0
Rare = 0

Enqueue(2)
0 1 2 3 4

2 5

Front = 0 Rare = 1

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Enqueue() operation
 Stacks
Enqueue(7)
 Queues
 Enqueue operation – 0 1 2 3 4
performance 2 5 7

Front = 0 Rare = 2
Enqueue(9)
0 1 2 3 4
2 5 7 9

Front = 0 Rare = 3

Enqueue(11)
0 1 2 3 4
2 5 7 9 11

Front = 0 Rare = 4

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Enqueue() operation
 Stacks
 Queues • If the Queue is_full and we call the enqueue
 Enqueue operation – operation,
performance
• This condition is called Overflow condition.

Enqueue
0 1 2 3 4

2 5 7 9 11

Front = 0 Rare = 4

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Performing Dequeue() operation in Queue:
 Stacks
 Queues
 Enqueue operation – Dequeue()
performance
 Dequeue operation – 0 1 2 3 4
performance 5 7 9 11

Front = 0 Rare = 4

Dequeue()

0 1 2 3 4
7 9 11

Front = 1 Rare = 4

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Dequeue() operation
 Stacks
Dequeue()
 Queues
 Enqueue operation – 0 1 2 3 4
performance 9 11
 Dequeue operation – Front = 2 Rare = 4
performance
Dequeue()

0 1 2 3 4
11

Front = 3 Rare = 4

Dequeue()
0 1 2 3 4

Rare = 4
Front = 4
CSE2003 Data Structures and Algorithms 12:42 AM Slide.
Dequeue() operation
 Stacks
 Queues • If the Queue is_empty and we call the dequeue
 Enqueue operation – operation,
performance
 Dequeue operation – • This condition is called Underflow condition.
performance

Dequeue()

0 1 2 3 4

Rare = 4
Front = 4

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Real time examples
 Stacks
 Queues
 Enqueue operation –
performance
 Dequeue operation –
performance
 Real time examples

Collecting Tickets in a Queue

Vehicles Collecting Tickets following


Queue rule

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


 Stacks
 Queues Collecting books in library
 Enqueue operation –
performance
 Dequeue operation –
performance
 Real time examples

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Applications
 Stacks
 Queues • Memory Management
 Enqueue operation – – The unused memory locations in the case of ordinary
performance
queues can be utilized in circular queues.
 Dequeue operation –
performance • Traffic system
 Real time examples
 Applications
– In computer controlled traffic system, circular queues are
used to switch on the traffic lights one by one repeatedly
as per the time set.
• CPU Scheduling
– Operating systems often maintain a queue of processes
that are ready to execute or that are waiting for a
particular event to occur

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Circular Queue

Conditions:

If front == -1 and rare == -1


queue is empty
6 7
11 If rare== size-1
9 queue is full
12
8
14 21 When the index 0 is empty then
16 18 If rare == size -1
Front rear = 0
rare If front ==rare+1
queue is full

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


Programs
 Programs
• Program on Stacks using Arrays
• Program on Towers of Hanoi
• Program on Queues using Arrays

CSE2003 Data Structures and Algorithms 12:42 AM Slide.


CSE2003 Data Structures and Algorithms 12:42 AM Slide.

You might also like