You are on page 1of 30

Module 2

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.

Dequeue increments Front.


Front and Rear
of a Queue
Linked List
Implementation
Insert an element into a Queue
public void insert(int data)
{
Node nptr = new Node(data, null);
if (rear == null)
{
front = nptr;
rear = nptr;
}
else
{
rear.setLink(nptr);
rear = rear.getLink();
}
size++ ;
}
Delete an element from a Queue (always removed from front)
public int remove()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
Node ptr = front;
front = ptr.getLink();
if (front == null)
rear = null;
size-- ;
return ptr.getData();
}
Function to check front element of Queue (peek)
public int peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow
Exception");
return front.getData();
}
Displaying contents of queue
public void display()
{
System.out.print("\nQueue = ");
if (size == 0)
{
System.out.print("Empty\n");
return ;
}
Node ptr = front;
while (ptr != rear.getLink() )
{
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();
}
Deque
Double-ended Queue
● Double-ended queue
○ dequeue
○ abbreviated to deque, pronounced deck
● An abstract data type that generalizes a queue.
● Elements can be added to or removed from either the front
(head) or back (tail).
● Application
○ Job scheduling algorithm
● Time complexity is O(1).
● Random access is O(n) time.
Insert at front
public void insertFront(int x)
{
Node newNode=new Node(x);
newNode.next=null;
if(isEmpty())
last=newNode;
else
first.previous=newNode;
newNode.next=first;
first=newNode;
}
Insert at rear
public void insertRear(int x)
{
Node newNode=new Node(x);
newNode.next=null;
if(isEmpty())
first=newNode;
else
{
last.next=newNode;
newNode.previous=last;
}
last=newNode;
}
Delete from front
public int deleteFront()
{
int t=first.data;
if(first.next==null)
last=null;
else
first.next.previous=null;
first=first.next;
return t;
}
Delete from rear
public int deleteRear()
{
int t=last.data;
if(first.next==null)
first=null;
else
last.previous.next=null;
last=last.previous;
return t;
}
Display from Front
public void displayForward()
{
Node current=first;
while(current!=null)
{
current.displayNode();
current=current.next;
}
}
Display from Rear
public void displayBackward()
{
Node current=last;
while(current!=null)
{
current.displayNode();
current=current.previous;
}
}
Priority Queue
Priority Queue
● A priority queue is an abstract data
type is queue, where additionally
each element has a "priority"
associated with it.
● An element with high priority is
served before an element with low
priority.
● If two elements have the same
priority, they are served according
to their order in the queue.
● Priority queues are often implemented
with heaps.
Priority Queue
A priority queue is a queue where:

● Requests are inserted in the order of arrival


● The request with highest priority is processed first
(deleted from the queue)
● The priority is indicated by a number, the lower the
number - the higher the priority.
Priority Queue Representation
Priority Queue Example
● A priority queue is different from a "normal" queue,
because instead of being a "first-in-first-out" data
structure, values come out in order by priority.

● A priority queue might be used, for example, to handle


the jobs sent to the Computer Science Department's
printer: Jobs sent by the department chair should be
printed first, then jobs sent by professors, then those
sent by graduate students, and finally those sent by
undergraduates.
Priority Queue Example
● The values put into the priority queue would be the
priority of the sender (e.g., using 4 for the chair, 3
for professors, 2 for grad students, and 1 for
undergrads), and the associated information would be the
document to print.

● 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

You might also like