Module 3
Stacks
and
Queues
Presented By:
Rajesh S M, Assistant Professor, Dept. of CSE,
GITAM School of Technology.
Contents
Stacks
➜ Definition
➜ Operations on Stacks.
➜ Array implementation.
➜ Linked list implementation.
➜ Applications.
2
Queues
➜ Definition
➜ Operations on Queues.
➜ Array implementation.
➜ Linked list implementation.
➜ Applications.
➜ Types of Queues.
Queues
➜ Linear Data Structure.
➜ A Queue is a collection of objects that are inserted
and removed according to the First in, First out
(FIFO) principle.
➜ Insertion and Deletion of an Element is done at
two different ends. (Rear & Front)
➜ Example : Call center phone system, Ticket
Counter, Bill Counter.
3
Real Time Examples
4
Basic Operations on Queue
enqueue()
− Inserting (storing) a element into queue from rear.
dequeue()
− Removing (deleting) a element from front of queue.
peek()
− Get the first data element of the queue, without
removing it.
isFull()
− Check if queue is full.
isEmpty()
− Check if queue is empty.
display()
− Prints all the elements of the Queue.
Understanding Queue
6
Understanding Queue
7
Understanding Queue
8
Analyse and Answer
9
➜ Element at rear and front.
➜ Queue Follows - Principle.
➜ Element first inserted.
➜ Element first deleted out.
➜ Can wedeleteany element.
➜ Which is are the two
referencesfor Queues.
Queues implementation
10
Array / List Based
➜ Lists / Array’s are used to
implement the queues
operations.
➜ Various ways to Implement:
- RegularImplementation.
- Listmethods.
- PythonLibraryModules.
Linked List Based
➜ Can be implement on
Singly Linked List.
➜ Methods:
- Insert_rear()
- Del_front()
- Display()
Queues Implementation
➜ enqueue() & dequeue() operations make
use of both the ends of the List.
➜ Ends : front and rear.
➜ For each insert / enqueue operation queue
rear is incremented (by one)
➜ For each delete / dequeue operation
queue front is incremented (by one)
11
Queues Implementation : Regular Implementation
12
➜ Let’s implement the following methods
using list:
➜ enqueue()
➜ dequeue()
➜ display()
13
Basic Setup
➜ Empty list: [ ]
➜ Maximum Queue size.
➜ rear = front = -1
Queues Implementation : Regular Implementation
14
Enqueue
Inserting an Element to the queue from
the rear end.
Step1: Check if the queue is full.
Step2: If the queue is full, produce
overflow error and exit.
Step3: If the queue is not full, increment
"rear" to point to next space.
Step4: Add data element to the queue
location, where the rear is pointing.
Step5: return success.
15
Dequeue
Removing an Element from the queue
from the front end.
Step1: Check if the queue is empty.
Step2: If the queue is empty, produce
underflow error and exit.
Step3: If the queue is not empty, access
the data where front is pointing.
Step4: Increment "front" to point to the
next available data element.
Step5: return success.
16
Display
Printing the contents of
the Queue.
Display’s all the
elements.
Queues Implementation : Regular Implementation
17
Driver Code
List based Queue Implementation : Inbuilt Methods
18
Queue
Operation
Realization with Python list
(Inbuilt Methods)
Enqueue list.append()
Dequeue list.pop(0)
Top (peek) list[-1]
Queue Empty len(list)==0
Queue Size len(List)
19
Implementation
Basic Setup
Isfull ()
Isempty ()
20
Implementation
Enqueue()
Dequeue()
21
Implementation
Display()
22
Driver Code
List based Queue Implementation : Dequeue Module
23
Queue
Operation
Realization with Python list
(Inbuilt Methods)
Enqueue list.append()
Dequeue list.popleft()
Top (peek) list[-1]
Queue Empty len(list)==0
Queue Size len(List)
24
Implementation
25
Implementation
List based Queue Implementation : queue.Queue
26
Inbuilt Method Description
maxsize Number of items allowed in the queue.
empty() Return True if the queue is empty, False
otherwise.
full() Return True if there are maxsize items in
the queue.
If the queue was initialized with
maxsize=0 (the default), then full() never
returns True.
List based Queue Implementation : queue.Queue
27
Inbuilt Method Description
get() Remove and return an item from the
queue. If queue is empty, wait until an
item is available.
get_nowait() Return an item if one is immediately
available, else raise QueueEmpty.
put(item) Put an item into the queue. If the queue is
full, wait until a free slot is available
before adding the item.
List based Queue Implementation : queue.Queue
28
Inbuilt Method Description
put_nowait
(item)
Put an item into the queue without
blocking. If no free slot is immediately
available, raise QueueFull.
qsize() Return the number of items in the queue.
29
Implementation
30
Implementation
Queue’s Implementation using Linked List
31
Enqueue: Implement Insert_last()
Dequeue: Implement Delete_front()
Display: Implement Display with first Reference.
20 30 40
None
First
Queue’s Implementation using Linked List : Basic
32
Queue’s Implementation using Linked List : Enqueue
33
Queue’s Implementation using Linked List : Dequeue
34
Queue’s Implementation using Linked List : Display
35
Queue’s Implementation using Linked List : Driver Code
36
Or
Use Menu Driven
Interface.
Applications of Queue’s
37
1. When a resource is shared among multiple consumers.
Examples include CPU scheduling, Disk Scheduling.
2. When data is transferred asynchronously (data not
necessarily received at same rate as sent) between two
processes.
Examples include IO Buffers, pipes, file IO, etc.
Applications of Queue’s
38
3. In Operating systems:
a) Semaphores
b) FCFS ( first come first serve) scheduling, example: FIFO queue
c) Spooling in printers
d) Buffer for devices like keyboard
4. In Networks:
Queues in routers/ switches
Mail Queues
Exercise on Queues
39
1. String Reversal.
2. Balanced Parenthesis.
3. String Palindrome
Types of Queues
40
Queue
Queue
Linear Queue
Linear Queue
Double
Ended Queue
Double
Ended Queue
Priority
Queue
Priority
Queue
Circular
Queue
Circular
Queue
Linear Queue
41
❖ It is a linear data structure that serves the request
first, which has been arrived first.
❖ Data elements arranged in a linear fashion.
❖ It has two references, i.e., front and rear.
❖ Insertion takes place from the rear end, and deletion
occurs from the front end.
Double Ended Queue (Deque)
42
❖ Double Ended Queue is a generalized version of
Queue data structure that allows insert and delete at
both ends.
❖ it does not follow FIFO rule (First In First Out).
Operations on Double Ended Queue
43
enqueue_front(): Insert the element from the front end.
enqueue_rear(): Insert the element from the rear end.
dequeue_front(): Delete the element from the front end.
dequeue_rear(): Delete the element from the rear end.
getfront(): Return the front element of the deque.
getrear(): Return the rear element of the deque.
display(): Prints all the elements of the deque.
Deque implementation
44
List Based
➜ Lists are used to implement
the deque operations.
➜ Various ways to Implement:
- Listmethods.
- PythonLibraryModules.
- RegularImplementation.
Linked List Based
➜ Can be implement on
Singly Linked List.
➜ Methods:
- Insert_front()
- Insert_rear()
- Del_front()
- Del_rear()
- Display()
List based Deque Implementation : Inbuilt Methods
45
Queue
Operation
Realization with Python list
(Inbuilt Methods)
Enqueue_front list.insert(index,element)
Enqueue_rear list.append(element)
Dequeue_front list.pop(0)
Dequeue_rear list.pop()
46
Implementation
Basic Setup
Isfull ()
Isempty ()
47
Implementation
Enqueue_front()
Enqueue_rear()
48
Implementation
Dequeue_front()
Dequeue_rear()
49
Implementation
Display()
50
Driver Code
51
Driver Code
List based Deque Implementation : Deque Module
52
Queue
Operation
Realization with Python list
(Inbuilt Methods)
Enqueue_front list.appendleft()
Enqueue_rear list.append()
Dequeue_front list.popleft()
Dequeue_rear len.pop()
Queue Size len(List)
53
Implementation
54
Implementation
55
Implementation
56
Implementation
Deque Implementation : Regular Implementation
57
➜ Let’s implement the following methods using
list:
➜ Isfull()
➜ Isempty()
➜ Enqueue_front()
➜ Enqueue_rear()
➜ Dequeue_front()
➜ Dequeue_rear()
➜ display()
58
Basic Setup
➜ Empty list: [ ]
➜ Maximum Queue size.
➜ rear = 0
➜ front = -1
Deque Implementation : Regular Implementation
59
Implementation
60
Implementation
61
Implementation
62
Implementation
63
Implementation
64
Display Method
65
Driver Code
Priority Queue
66
❖ Priority Queues are Linear data structures where each
data/value in the queue has a certain priority.
❖ For example, In airlines, baggage with the title
“Business” or “First-class” arrives earlier than the rest.
Applications Priority queue:
Job Scheduling algorithms, CPU and Disk Scheduling,
managing resources that are shared among different
processes, etc.
Priority Queue properties
67
❖ Priority Queue is an extension of the queue .
❖ Element with high priority is dequeued before an
element with low priority.
❖ If two elements have the same priority, they are
served according to their order in the queue.
Key differences between Priority Queue and Queue:
68
❖ In Queue, the oldest element is dequeued first.
While, in Priority Queue, element based on highest
priority is dequeued.
❖ When elements are popped out of a priority queue
then result obtained in either sorted in Increasing
order or in Decreasing Order. While, when elements
are popped from a simple queue, a FIFO order of data
is obtained in the result.
Priority Queue
69
70
Implementation
List Methods
71
Implementation
List Methods
72
Implementation
List Methods
73
Implementation using Priority Queue Class
74
Implementation using Priority Queue Class
Circular Queue
75
❖ Circular Queue is a linear data structure.
❖ Operations are performed based on FIFO.
❖ The last position is connected back to the first
position to make a circle.
‘Ring Buffer’
Representation:
76
Understanding Circular Queue
77
Implementation of Circular Queue:
78
Implementation of Circular Queue:
79
Implementation of Circular Queue:
80
Implementation of Circular Queue:
81
Implementation of Circular Queue:
“
Thank You
82

Queues - Introduction, Operations and Implementation