Professional Documents
Culture Documents
Lecture Queues
Lecture Queues
QUEUE
A Queue is a special kind of list, where items are
inserted at one end (the rear) And deleted at the
other end (the front)
Other Name:
First In First Out (FIFO)
Difference from Stack:
Insertion go at the end of the list, rather
than the beginning of the list.
03/20/15
Queue
Stores a set of elements in a particular
order
Stack principle: FIRST IN FIRST
OUT = FIFO
It means: the first element inserted is
the first one to be removed
Example
The first one in line is the first one to be
served
03/20/15
st In First Out
rear
front
B
A
C
rear B
front A
D
rear C
B
front A
rear
D
C
front B
rear
front
03/20/15
Queue Applications
Real life examples
Waiting in line
Waiting on hold for tech support
03/20/15
Applications of Queues
Operating system
multi-user/multitasking environments, where several
users or task may be requesting the same resource
simultaneously.
Communication Software
queues to hold information received over networks
and dial up connections. (Information can be
transmitted faster than it can be processed, so is
placed in a queue waiting to be processed)
Some other?
03/20/15
Static
Queue is implemented by an array, and size of queue
remains fix
Dynamic
A queue can be implemented as a linked list, and
expand or shrink with each enqueue or dequeue
operation.
03/20/15
7
7
1.
2.
3.
4.
5.
Example:
Line of customers in a bank
03/20/15
8
8
03/20/15
Implementation
Static
Queue is implemented by an array, and size of queue
remains fix
Dynamic
A queue can be implemented as a linked list, and it
can expand or shrink with each enqueue or dequeue
operation.
03/20/15
10
Implementation 2
03/20/15
11
Array Implementation
Front
First Element
Second
Element
.
Why it is inefficient?
Rear
Last Element
maxlength
03/20/15
12
03/20/15
13
Implementation 1: enqueue
void enqueue(int rear, int item)
{
//add an item to the queue
if (rear == MAX_QUEUE_SIZE)
{
queue_full( );
return;
}
queue [++rear] = item;
}
03/20/15
14
Implementation 1: dequeue
int dequeue(int front, int rear)
{
// remove element at the front of the queue
if ( *front == rear)
return queue_empty( );
return queue [++ front];
}
03/20/15
15
Array Implementation
5
7
5
6
6
12
67
Front=0
Rear=6
8
0
7
5
6
6
Front=4
Rear=6
7
0
Front=5
Rear=8
03/20/15
6
6
03/20/15
18
7
0
6
6
12
67
Front=5
Rear=8
Enqueue 39
39
0
7
1
6
6
12
67
Front=5
Rear=0
03/20/15
19
[3]
[2]
[2]
[3]
J2
[1]
[4]
[0]
J3
[1] J1
[5]
front = 0
rear = 0
[4]
[0]
[5]
front = 0
rear = 3
20
FULL QUEUE
[2]
[3]
J2
[1]
FULL QUEUE
[2]
J8
J3
J1
J4
J5
[0]
front =0
rear = 5
[5]
[3]
J9
[4][1] J7
[4]
J6
[0]
J5
[5]
front =4
rear =3
21
03/20/15
22