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

Lecture 04

The document discusses queues, which are first-in, first-out (FIFO) data structures where items are inserted at one end and removed from the other. It provides examples of implementing queues using linked lists and arrays, and describes operations like enqueue, dequeue, front, and isEmpty. Real-world uses of queues include I/O buffers, scheduling in operating systems, and priority queues where items are ordered by priority rather than time.

Uploaded by

Saad Zaheer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 04

The document discusses queues, which are first-in, first-out (FIFO) data structures where items are inserted at one end and removed from the other. It provides examples of implementing queues using linked lists and arrays, and describes operations like enqueue, dequeue, front, and isEmpty. Real-world uses of queues include I/O buffers, scheduling in operating systems, and priority queues where items are ordered by priority rather than time.

Uploaded by

Saad Zaheer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Queues

 A stack is LIFO (Last-In First Out)


structure.
 In contrast, a queue is a FIFO (First-In
First-Out ) structure.
 A queue is a linear structure for which
items can be only inserted at one end and
removed at another end.

1
Queues
• A real world example of queue can be
a single-lane one-way road, where the
vehicle enters first, exits first.
• More real-world example can be seen
as queues at ticket windows & bus-
stops.

2
Queue Operations
Enqueue(X) – place X at the rear of the
queue.
Dequeue() -- remove the front element and
return it.
Front() -- return front element without
removing it.
IsEmpty() -- return TRUE if queue is
empty, FALSE otherwise
3
Implementing Queue
 Using linked List:
 Insert works in constant time for either end
of a linked list.
 Remove works in constant time only.
 Seems best that head of the linked list be
the front of the queue so that all removes
will be from the front.
 Inserts will be at the end of the list.
4
Implementing Queue
 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

5
Implementing Queue
 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

dequeue()

front rear front rear

7 5 2 1 7 5 2

6
Implementing Queue
 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

enqueue(9)

front rear front rear

7 5 2 9 7 5 2 9

7
Implementing Queue
int dequeue()
{
int x = front->get();
Node* p = front;
front = front->getNext();
delete p;
return x;
}
void enqueue(int x)
{
Node* newNode = new Node();
newNode->set(x);
newNode->setNext(NULL);
rear->setNext(newNode);
rear = newNode;
} 8
Implementing Queue
int front()
{
return front->get();
}

int isEmpty()
{
return ( front == NULL );
}

9
Queue using Array
 If we use an array to hold queue elements,
both insertions and removal at the front
(start) of the array are 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.
10
Queue using Array

front rear
1 7 5 2
1 7 5 2
0 1 2 3 4 5 6 7
front rear
0 3

11
Queue using Array
enqueue(6)

front rear
1 7 5 2 6
1 7 5 2 6
0 1 2 3 4 5 6 7
front rear
0 4

12
Queue using Array
enqueue(8)

front rear
1 7 5 2 6 8
1 7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
0 5

13
Queue using Array
dequeue()

front rear
7 5 2 6 8
7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
1 5

14
Queue using Array
dequeue()

front rear
5 2 6 8
5 2 6 8
0 1 2 3 4 5 6 7
front rear
2 5

15
Queue using Array
enqueue(9)
enqueue(12)
front rear
5 2 6 8 9 12
5 2 6 8 9 12
0 1 2 3 4 5 6 7
front rear
2 7
enqueue(21) ??

16
Queue using Array
 We have inserts and removal running in
constant time but we created a new
problem.
 Cannot insert new elements even though
there are two places available at the start
of the array.
 Solution: allow the queue to “wrap
around”.
 Before Enqueue and Dequeue we have to
17
handle overflow and underflow problem
Queue using Array
 Before Enqueue and Dequeue we have to
handle overflow and underflow problem.
int isFull()
{
return noElements == size;
}

int isEmpty()
{
return noElements == 0;
}

18
Queue using Array
enqueue(21)
0 1
front rear 21 front size
7 2 2 8
12 5
5 2 6 8 9 12 21
6 9 2
8 6 3 rear noElements
0 7
5 4

Void enqueue(int x)
{
rear=(rear+1) % size;
array[rear] = x;
noElements=noElements+1;

}
19
Queue using Array
enqueue(7)
0 1
front rear 21 7 front size
7 2 2 8
12 5
5 2 6 8 9 12 21 7
6 9 2
8 6 3 rear noElements
1 8
5 4

Void enqueue(int x)
{
rear=(rear+1) % size;
array[rear] = x;
noElements=noElements+1;

}
20
Queue using Array
dequeue()
dequeue() 0 1

front rear 21 7 front size


7 2 4 8
12
6 8 9 12 21 7
6 9
8 6 3 rear noElements
1 6
5 4
int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}

21
Uses of Queues
• I/O buffers
Scheduling queues in a multi-user computer system
e.g. Printer queue:
When files are submitted to a printer, they are
placed in the printer queue. The printer software
executes an algorithm something like:
for (;;)
{
while (printerQueue.empty())
sleep 1;
printFile = printerQueue.removeQ();
Print(printFile);} 22
Uses of Queues
Resident queue: On disk, waiting for memory

Ready queue: In memory —needs only


CPU to run

Suspended queue: Waiting for I/O transfer


or to be reassigned the CPU

23
Priority Queues

• Priority queues are queues in which items


are ordered by priority rather than
temporally (i.e. order in which received).

• Any queue implementation then can be


taken and modified to acquire a priority
queue.

24
• The only needed modification is the
Enqueue() method.

• Instead of unconditionally adding to the


back, the queue must be scanned for the
correct insertion point.

25
Queues vs. Stacks
• Stacks are a LIFO container => store data in the reverse of
order received
• Queues are a FIFO container => store data in the order
received

• Stacks then suggest applications where some sort of


reversal or unwinding is desired.

• Queues suggest applications where service is to be


rendered relative to order received.

• Stacks and Queues can be used in conjunction to


26
compare different orderings of the same data set.

You might also like