• No results found

QUEUE

N/A
N/A
Protected

Academic year: 2020

Share "QUEUE"

Copied!
36
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

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 end FRONT • The element added first is also removed first

(3)

• 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

(4)

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

(5)

Enqueue (ItemType newItem)

Function: Adds newItem to the rear of the

queue.

Preconditions: Queue has been initialized and is

not full.

(6)

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

(7)

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.

(8)

• Auxiliary queue operations:

▫ object front(): returns the element at the front without removing it

▫ integer size(): returns the number of elements stored

(9)

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)

(10)

Applications of Queues

• Direct applications

▫ Waiting lists, bureaucracy

▫ Access to shared resources (e.g., printer) ▫ Multiprogramming

• Indirect applications

(11)

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.

(12)

• 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:

(13)

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.

(14)

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”.

(15)

• 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

(16)

• 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=REAR≠NULL

• And if that only element is deleted, then

(17)

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.

(18)

3. Set QUEUE[REAR]:=ITEM [inserts new element]

(19)

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.

(20)
(21)
(22)
(23)

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.

(24)

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

(25)

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

(26)

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]

(27)

5. LINK[TEMP]=AVAIL and AVAIL=TEMP

(28)

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

(29)

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.

(30)
(31)

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

(32)

[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.

(33)

[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.

(34)

Array representation of a priority

queue

(35)

[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

(36)

[Algorithm adds an ITEM with priority number M to a priority queue]

1. Insert ITEM as the rear element in row M of Queue.

References

Related documents

4.1 The Select Committee is asked to consider the proposed development of the Customer Service Function, the recommended service delivery option and the investment required8. It

innovation in payment systems, in particular the infrastructure used to operate payment systems, in the interests of service-users 3.. to ensure that payment systems

• Follow up with your employer each reporting period to ensure your hours are reported on a regular basis?. • Discuss your progress with

However, obtaining bacterial genomic information is not always trivial: the target bacteria may be difficult-to-culture or uncultured, and may be found within samples containing

Marie Laure Suites (Self Catering) Self Catering 14 Mr. Richard Naya Mahe Belombre 2516591 [email protected] 61 Metcalfe Villas Self Catering 6 Ms Loulou Metcalfe

The corona radiata consists of one or more layers of follicular cells that surround the zona pellucida, the polar body, and the secondary oocyte.. The corona radiata is dispersed

○ If BP elevated, think primary aldosteronism, Cushing’s, renal artery stenosis, ○ If BP normal, think hypomagnesemia, severe hypoK, Bartter’s, NaHCO3,

Despite the fact that the rise in net saving rate, capital stock and total consumption are the same in these two economies, the permanent increase in consumption that an