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

Chapter Five: Stack and Queues

1. Stacks and queues are common data structures that operate on LIFO (last in first out) and FIFO (first in first out) principles respectively. 2. Stacks can be implemented using arrays or linked lists, with push and pop operations at one end. Queues support enqueue to insert at the rear and dequeue to remove from the front. 3. Priority queues store elements with associated priorities, and remove the highest priority element first or elements of same priority in order of insertion. They can be implemented using arrays or linked lists.

Uploaded by

Chala Geta
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)
73 views

Chapter Five: Stack and Queues

1. Stacks and queues are common data structures that operate on LIFO (last in first out) and FIFO (first in first out) principles respectively. 2. Stacks can be implemented using arrays or linked lists, with push and pop operations at one end. Queues support enqueue to insert at the rear and dequeue to remove from the front. 3. Priority queues store elements with associated priorities, and remove the highest priority element first or elements of same priority in order of insertion. They can be implemented using arrays or linked lists.

Uploaded by

Chala Geta
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/ 40

CHAPTER FIVE

STACK AND QUEUES


Stacks
A simple data structure, in which insertion and
deletion occur at the same end, is termed (called) a
stack. It is a LIFO (Last In First Out) structure.
The operations of insertion and deletion are called
PUSH and POP
Push - push (put) item onto stack
Pop - pop (get) item from stack

2 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Push - push (put) item onto stack
Stacks Pop - pop (get) item from stack
TOS=represent top of stack

Initial Stack Push(8) Pop

                 


 
TOS=> 8  
4
1 4 TOS=> 4
TOS=>

3 1 1
6 3 3
6 6
Cont..
Implementation: Stacks can be implemented as:
 an array (contiguous list) and
 a linked list.

4 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Stack operations

5 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Cont..

6 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Array Implementation of Stacks(PUSH)
If an array is given, which is supposed to act as a
STACK, it has to be a STATIC Stack; meaning, data
will overflow if you cross the upper limit of the array

7 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Cont..
Algorithm:
 Step-1: Increment the Stack TOP by 1. Check whether
it is always less than the Upper Limit of the stack. If it
is less than the Upper Limit go to step-2 else report
-"Stack Overflow"
 Step-2: Put the new element at the position pointed by
the TOP

8 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Implementation

9 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


 Note: In array implementation, we have taken TOP =
-1 to signify the empty stack, as this simplifies the
implementation.

10 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


POP operation

 POP is the synonym for delete when it comes to Stack.


 Return an error message, "Stack underflow", if an
attempt is made to Pop an item from an empty Stack

11 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


(……………………………………
………..)
Algorithm
 Step-1: If the Stack is empty then give the alert "Stack
underflow" and quit; or else go to step-2
 Step-2:
 a) Hold the value for the element pointed by the TOP

 b) Put a NULL value instead


 c) Decrement the TOP by 1

12 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Implementation

13 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Linked List Implementation of Stacks

 The PUSH operation


 The POP operation
 Evaluate the postfix expression using stack:
6523+8∗+3+∗
 Application of Stack and Queue
Assignment 10 ??

14 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Queue
 Queue is a data structure that has access to its data at the front
and rear.
 Operates on FIFO (First In First Out) basis.
 Uses two pointers/indices to keep track of information/data.
 Has two basic operations:
 enqueue - inserting data at the rear of the queue
 dequeue – removing data at the front of the queue

15 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


……………………………………
…………….

16 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Array Implementation Of enqueue and dequeue Operations

17 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


.

18 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Linked List Implementation Of Queue

 Enqueue and
 Dequeue Operations

Assignment 11

19 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Deque (Double Ended Queue)
Insertion and deletion can be done at both ends

20 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


(::::::::::::::::::::::::::::::::::::::::::::::::::::
:::)

21 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


(::::::::::::::::::::::::::::::::::::::::::::::::::::
:::)

22 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


(::::::::::::::::::::::::::::::::::::::::::::::::::::
:::)

23 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


(::::::::::::::::::::::::::::::::::::::::::::::::::::
:::)

24 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


(::::::::::::::::::::::::::::::::::::::::::::::::::::
:::)

25 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


(::::::::::::::::::::::::::::::::::::::::::::::::::::
:::)

26 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


(::::::::::::::::::::::::::::::::::::::::::::::::::::
:::)

27 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


(::::::::::::::::::::::::::::::::::::::::::::::::::::
:::)

28 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


(::::::::::::::::::::::::::::::::::::::::::::::::::::
:::)

29 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


30 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022
31 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022
Assignment 12
Deque implementation in array
 All four operations must be implemented

32 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Priority Queues
 In general, is a collection of prioritized elements
(every element has a priority associated with it), in
which the next element to be removed from the queue,
is the element that:-
 Has the highest priority of all the elements.
 Has been in the queue the longest among elements
with equal priority.

33 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


34 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022
35 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022
(::::::::::::::::::::::::::::::::::::::::::::::::::::
:::)

36 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


How to implement priority queue?
Using Array: A simple implementation is to use array
of following structure.
Struct Element
{
int item;
int priority;
}

37 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


 Using Linked List
Struct Node
{
int data;
int priority;
Node * next;
};

38 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


Assignment 13(Sub. Date: next week)
A typical priority queue supports following operations.
insert(item, priority): Inserts an item with given
priority.
getHighestPriority(): Returns the highest priority
item.
deleteHighestPriority(): Removes the highest priority
item.

39 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022


 Applications of Priority Queue:

 CPU Scheduling
 Graph algorithms like Dijkstra’s shortest path algorithm, 
Prim’s Minimum Spanning Tree, etc

40 INSY 2032: Data Structure and Algorithm Analysis 01/05/2022

You might also like