Professional Documents
Culture Documents
Queue
Priority Queue
Deque
Module 2
● Stacks
○ Implementation of stacks using arrays and lists
○ Conversion of infix to postfix
○ Evaluation of postfix expression
● Linked lists
○ Singly, doubly and circular lists
○ Application of linked lists
○ Polynomial manipulation
● Queues & Deques
○ Implementation
○ Priority queues
queue
Queue
● Queue is a kind of abstract data type where items are
inserted one end and removed from the other end.
● This makes the queue a First-In-First-Out (FIFO) data
structure.
● In a FIFO data structure, the first element added to the
queue will be the first one to be removed.
● Application
○ Transport system
○ Used as Buffer
Queue
● Insertion is done at the rear end. The operation is known
as enqueue operation.
● Deletion is done at the front end. The operation is known
as dequeue operation.
● Time complexity of all queue operations is O(1).
Front and Rear of a Queue
Array Implementation
Enqueue increments Rear.
● Each time the printer is free, the job with the highest
priority would be removed from the print queue, and
printed.
Applications of Priority Queue
1. Dijkstra's algorithm
2. Huffman coding
3. Best-first search algorithms
4. Prim's algorithm for minimum spanning tree
Implementation of Priority Queues
1. Linked list
a. Insert at the beginning - O(1)
b. Find the minimum - O(N)
2. Binary search tree (BST)
a. Insert O(logN)
b. Find minimum - O(logN)
3. Binary heap
a. Better than BST because it does not support links.
b. Insert O(logN)
c. Find minimum O(logN)
d. Deleting the minimal element O(1)
e. Adjusting heap structure during deletion takes O(logN) time.
Questions?
1. Compare and contrast array representation and linked list
representation of linear lists.
2. Explain stack data structure and its basic operations.
3. Write a general algorithm for conversion of infix
expression to postfix expression and explain with example.
4. Implement a linked list with following operations.
a. Add a node with a given value to the front of the list.
b. Traverse a list.
c. Add a node with a given value after a specified node.
d. Delete a node with a given value.
5. Explain priority queue.
6. Explain dequeue.
this is for later reference
Depth-First Search with a Stack
In depth-first search we go down a path until we get to a dead end; then we backtrack or back up (by popping a stack) to get an
alternative path.
● Create a stack
● Create a new choice point
● Push the choice point onto the stack
● while (not found and stack is not empty)
○ Pop the stack
○ Find all possible choices after the last one tried
○ Push these choices onto the stack
● Return
this is for later reference
Breadth-First Search with a Queue
In breadth-first search we explore all the nearest possibilities by finding all possible successors and enqueue them to a queue.
● Create a queue
● Create a new choice point
● Enqueue the choice point onto the queue
● while (not found and queue is not empty)
○ Dequeue the queue
○ Find all possible choices after the last one tried
○ Enqueue these choices onto the queue
● Return