L6
L6
Algorithms(CSE2001)
MODULE 1
Stack and Queues - Linear Data Structures
1-1
What’s this ?
2
Queue
3
Queue
• A stack is LIFO (Last-In First Out) structure. In
contrast, a queue is a FIFO (First-In First-Out )
structure.
• In a FIFO data structure, the first element
added to the queue will be the first one to be
removed.
• A queue is a linear structure for which items
can be only inserted at one end and removed at
another end.
4
Operations on Queue
• We will dedicate once again six operations on
the Queue.
• You can add a person to the queue (enque),
• Look who is the first person to be serviced (front)
• Rremove the first person (dequeue) and
• Check whether the queue is empty (isEmpty).
• Also there are two more operations to create and
to destroy the queue.
5
Operations
• Queue create()
• Creates an empty queue
• boolean isEmpty(Queue q)
• Tells whether the queue q is empty
• enqueue(Queue q, Item e)
• Place e at the rear of the queue q
• destroy(Queue q)
• destroys queue q
6
Operations
• Item front(Queue q)
• returns the front element in Queue q
without removing it
Precondition: q is not empty
• dequeue(Queue q)
• removes front element from the queue q
Precondition: q is not empty
7
Implementation Using Arrays
• If we use an array to hold queue elements,
dequeue operation at the front (start) of the array
is expensive.
• This is because we may have to shift up to “n”
elements.
• For the stack, we needed only one end; for queue
we need both.
• To get around this, we will not shift upon removal
of an element.
8
Snapshot of a Queue
9
enqueue()
10
enqueue() again
11
dequeue()
12
dequeue() again
13
Two more enqueue()
14
What’s Wrong ?
15
Queue array wrap-around
16
enqueue(element)
void enqueue(int x)
{
rear = (rear+1)%size;
array[rear] = x;
noElements = noElements+1;
}
17
dequeue()
int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}
18
isEmpty() and isFull()
int isFull()
{
return noElements == size;
}
int isEmpty()
{
return noElements == 0;
}
19
isEmpty() and isFull()
int isFull()
{
return noElements == size;
}
int isEmpty()
{
return noElements == 0;
}
20
Simple Queue or Linear Queue
• In Linear Queue, an insertion takes place from one end while
the deletion occurs from another end. The end at which the
insertion takes place is known as the rear end, and the end at
which the deletion takes place is known as front end.
Circular Queue
• In Circular Queue, all the nodes are represented as circular. It is
similar to the linear Queue except that the last element of the
queue is connected to the first element. It is also known as Ring
Buffer, as all the ends are connected to another end.
Priority Queue
• It is a special type of queue in which the elements are arranged
based on the priority. It is a special type of queue data structure
in which every element has a priority associated with it.
Insertion in priority queue takes place based on the arrival,
while deletion in the priority queue occurs based on the
priority. Priority queue is mainly used to implement the CPU
scheduling algorithms.
Deque (or, Double Ended Queue)
• In Deque or Double Ended Queue, insertion and
deletion can be done from both ends of the queue
either from the front or rear. It means that we can
insert and delete elements from both front and rear
ends of the queue.
Deque (or, Double Ended Queue)
• There are two types of deque:
• Input restricted deque