Sie sind auf Seite 1von 36

QUEUE

What is a queue?
It is an ordered group of homogeneous items of
elements.
Queues have two ends:
Elements are added at one end REAR
Elements are removed from the other endFRONT
The element added first is also removed first
(FIFO: First In, First Out).
Insertions are at the rear of the queue and removals are at the front
of the queue
Main queue operations:
enqueue(object): inserts an element at the end of the queue
object dequeue(): removes and returns the element at the front of the
queue
Queue Specification
Definitions: (provided by the user)
MAX_ITEMS: Max number of items that might be on
the queue
ItemType: Data type of the items on the queue

Operations
MakeEmpty
Boolean IsEmpty
Boolean IsFull
Enqueue (ItemType newItem)
Dequeue (ItemType& item)
Enqueue (ItemType newItem)
Function: Adds newItem to the rear of the
queue.
Preconditions: Queue has been initialized and is
not full.
Postconditions: newItem is at rear of queue.
Dequeue (ItemType& item)
Function: Removes front item from queue and
returns it in item.
Preconditions: Queue has been initialized and is
not empty.
Post-conditions: Front element has been
removed from queue and item is a copy of
removed element.
Implementation issues
Implement the queue as a circular structure.
How do we know if a queue is full or empty?
Initialization of front and rear.
Testing for a full or empty queue.
Auxiliary queue operations:
object front(): returns the element at the front without removing
it
integer size(): returns the number of elements stored
boolean isEmpty(): indicates whether no elements are stored
Example
Operation Output Q
enqueue(5) (5)
enqueue(3) (5, 3)
dequeue() 5 (3)
enqueue(7) (3, 7)
dequeue() 3 (7)
front() 7 (7)
dequeue() 7 ()
dequeue() error ()
isEmpty() true ()
enqueue(9) (9)
enqueue(7) (9, 7)
size() 2 (9, 7)
enqueue(3) (9, 7, 3)
enqueue(5) (9, 7, 3, 5)
dequeue() 9 (7, 3, 5)
Applications of Queues
Direct applications
Waiting lists, bureaucracy
Access to shared resources (e.g., printer)
Multiprogramming
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
Representation of Queues
Represented in two ways:
One-way list
or Linear Arrays
Two pointers variables: FRONT, containing
the location of the front element of the queue
and REAR, containing the location of the rear
element of the queue.
FRONT=NULL means queue is empty.
Whenever an element is deleted from the queue,
the value of FRONT is increased by 1; this can be
implemented by the assignment
FRONT:=FRONT+1
Whenever an element is added to the queue, the
value of REAR is increased by 1; this can be
implemented by the assignment:
REAR:=REAR+1
Circular Queue
When a new item is inserted at the rear, the
pointer to rear moves upwards.
Similarly, when an item is deleted from the
queue the front arrow moves downwards.
After a few insert and delete operations the rear
might reach the end of the queue and no more
items can be inserted although the items from
the front of the queue have been deleted and
there is space in the queue.
Circular Queue
To solve this problem, queues implement
wrapping around. Such queues are called
Circular Queues.
Both the front and the rear pointers wrap around
to the beginning of the array.
It is also called as Ring buffer.
Items can inserted and deleted from a queue in
O(1) time.
Generally it is assumed that array QUEUE is
circular, i.e. QUEUE[1] comes after QUEUE[N].

With this assumption, we insert ITEM into the


queue by assigning ITEM to QUEUE[1].

Instead of increasing REAR to N+1, we reset


REAR to 1 and then assign
QUEUE[REAR]:=ITEM
Similarly if FRONT=N and an element of
QUEUE is deleted, we reset FRONT=1 instead of
increasing FRONT to N+1

If queue contains only one element, i.e.


FRONT=REARNULL
And if that only element is deleted, then
FRONT:=NULL and REAR:=NULL
QINSERT(QUEUE,N, FRONT,REAR,ITEM)
1. [Queue already filled?]
if FRONT=1 and REAR=N or if FRONT=REAR+1, then:
Write: Overflow and Return.
2. [Find new value of REAR]
If FRONT:=NULL, then : [Queue initially empty]
Set FRONT:=1 and REAR:=1
Else if REAR:=N, then
Set REAR:=1
Else: Set REAR:=REAR+1.
[End of if structure]
3. Set QUEUE[REAR]:=ITEM [inserts new
element]
4. Return
QDELETE(QUEUE,N, FRONT,REAR,ITEM)
1. [Queue already empty?]
if FRONT:=NULL, then:
Write: UNDERFLOW and Return.
2. Set ITEM:=QUEUE[FRONT]
3. [Find new value of FRONT]
If FRONT:=REAR, then : [Queue has only one element to
start]
Set FRONT:=NULL and REAR:=NULL
Else if FRONT:=N, then
Set FRONT:=1
Else: Set FRONT:=FRONT+1.
[End of if structure]
4. Return
Linked Representation of QUEUE
Two pointer variables: FRONT and REAR.
INFO fields of the list hold the elements of the
queue and LINK field holds pointer to the
neighboring elements in the queue.
In case of insertion, a node borrowed from the
AVAIL list and carrying the item to be inserted is
added as the last node of the linked list
representing the QUEUE.
LINKQ_INSERT(INFO,LINK,FRONT,REAR,AVAIL,ITEM)

1. [Available space?] if AVAIL=NULL, then write


OVERFLOW and exit.
2. [Remove first node from AVAIL list]
Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
3. Set INFO[NEW]:=ITEM and
LINK[NEW]:=NULL
[copies ITEM into new node]
4. If (FRONT=NULL) then FRONT=REAR=NEW
[if Q is empty then ITEM is the first element in
the Q]
Else set LINK[REAR]:=NEW and REAR=NEW
[Rear points to the new node appended to the
end of the list]
5. EXIT
LINKQ_DELETE(INFO,LINK,FRONT,REAR,AVAIL,ITEM)

1. [Linked queue empty?] if (FRONT=NULL)


then write: UNDERFLOW and EXIT.
2. Set TEMP:=FRONT [If linked queue is
nonempty, remember FRONT in a temporary
variable TEMP].
3. ITEM=INFO[TEMP]
4. FRONT=LINK[TEMP]
[Reset FRONT to point to the next element in the
queue]
5. LINK[TEMP]=AVAIL and AVAIL=TEMP
[return the deleted node TEMP to the AVAIL list]
6. EXIT
Deque
It is a double-ended queue.
Items can be inserted and deleted from either
ends.
More versatile data structure than stack or
queue.
There are two pointers: LEFT or RIGHT.
Two variations of a DEQUE: input restricted
Deque and output restricted Deque
Priority Queues
More specialized data structure.
Similar to Queue, having front and rear.
Items are removed from the front.
Items are ordered by key value or priority so
that the item with the lowest key (or highest) is
always at the front.
Items are inserted in proper position to maintain
the order.
Two types of representations:
One-way list representation
Array representation
One-way list representation of Priority
Queue
Each node contains three items of information:
INFO, PRN(priority number) and link number
(LINK)
A node X precedes Y in the list (1) when X has
higher priority than Y
(2) When both have same priority but X was
added to the list before Y.
[deletes and processes the first element
in a priority queue]
1. Set ITEM:=INFO[START]
[saves the data in the first node.]
2. Delete first node from the list
3. Process ITEM.
4. Exit
[Adds an ITEM with priority number N to a priority queue]

1. Traverse the one-way list until finding a node


X whose priority number exceeds N. Insert
ITEM in front of node X.
2. If no such node is found, insert ITEM as the
last element of the list.
Array representation of a priority queue
FRONT[K] and REAR[K] contain the front and
rear elements of row K of QUEUE.
[Algorithm deletes and processes the first element
in a priority queue]
1. [Find the first non-empty queue.]
Find the smallest K such that FRONT[K]NULL.
2. Delete and process the FRONT element in row
K of Queue.
3. Exit
[Algorithm adds an ITEM with priority number M
to a priority queue]
1. Insert ITEM as the rear element in row M of
Queue.
2. Exit

Das könnte Ihnen auch gefallen