Anda di halaman 1dari 6

QUEUES INTRODUCTION Another important subclass of lists permits deletions to be performed at one end of a list and insertions at the

other. The information in such a list is processed in the same order as it was received, that is, on a first-in, first-out (FIFO) or a firstcome, first-served (FCFS) basis. This type of lists is frequently referred to as a queue. Front Rear Deletion Insertion An insertion is made to the right of the rightmost element in the queue, and deletion is done by deleting the leftmost element in the queue. The updating operation may be restricted to the examination of the last or end element. If no such restriction is made, any element in the list can be selected. Examples - The checkout line at a supermarket cash register . The first person in line is the first to be checked out. - Another example of a queue can be found in a time-sharing computer system where many users share the system simultaneously. Such system has a single central processing unit and one main memory, and these resources must be shared by allowing one users program to execute for a short time, followed by the execution of another users program, until there is a return to the execution of the initial users program. The user programs that are waiting to be processed from a waiting queue. This queue may not operate on a strictly first-in, first-out basis, but on some complex priority scheme. The resulting queue is sometimes called a priority queue. - A final example of a queue is the line of cars waiting to proceed in some fixed direction at an intersection of streets. The deletion of a car corresponds to the first car in the line passing throu the intersection, while an insertion to the queue consists of a car joining the end of the line of existing car waiting to proceed throu the intersection. Algorithms to insert and delete an element from queue To formulate the algorithms the vector is assumed to consist of a large number of elements, enough to be sufficient to handle the variable-length property of a queue. The vector representation of a queue requires pointers f and r which denote the positions of its front and rear elements, respectively. Deletion f r 1 Insertion

SLICA

Procedure QINSERT (Q, F, R, N, Y) . Given F and R, pointers to the front and rear elements of a queue Q consisting of N elements, and an element Y, this procedure inserts Y at the rear of the queue. Prior to the first invocation of the procedure, F and R have been set to zero. Q The queue F Front pointer to the queue R Rear pointer of the queue N Size of the queue Y Element to be inserted 1) [Check for overflow] If R N then Write OVERFLOW Return 2) [Increment rear pointer] RR+1 3) [Insert the element] Q[R] Y 4) [Set front pointer] If F = 0 then F 1 Return Function QDELETE(Q, F, R). Given F and R, the pointers to the front and rear elements of a queue, respectively, and the queue Q to which they correspond, this function deletes and returns the last element of the queue. Y is a temporary variable. Q The queue F Front pointer of queue R Rear pointer of queue Y Element to be deleted(temp. variable) 1) [Check for underflow] If F = 0 then Write UNDERFLOW Return(0) (0 denotes an empty queue) 2) [Remove element] Y Q[F] 3) [Check for empty queue] If F = R then F R 0 else F F + 1 4) [Finished] Return(Y) Tracing: consider an example where the size of the queue is four elements. Initially, the queue is empty. It is required to insert symbols A, B, and C, delete A and B and insert D and E. 2 SLICA

Empty f r A Insert A f r A B Insert B f r A B Insert C f B Delete A f Delete B f r C Insert D f C Insert E f r (Overflow) Here, an overflow occurs on trying to insert symbol E, even though the first two locations are not being used. Actually an arbitrarily large amount of memory wld be required to accommodate the elements and the memory can be wasted. Circular Queue: A more suitable method of representing a queue, which prevents an excessive use of memory, is to arrange the elements Q[1], Q[2], .Q[n] in a circular fashion with Q[1] following Q[n]. r r D D C r C r C

.
Q[n-2] Q[3]

Q[4]

Q[n-1] Q[2]

f Q[n] Q[1] 3

SLICA

Algorithm to insert and delete an element in circular queue Procedure CQINSERT(F, R, Q, N, Y). Given pointers to the front and rear of a circular queue, F and R, a vector Q consisting of N elements, and an element Y, this procedure inserts Y at the rear of the queue. Initially F and R are set to zero. Q The queue F Front pointer to the queue R Rear pointer of the queue N Size of the queue Y Element to be inserted 1) [Reset rear pointer] If R = N then R 1 else R R + 1 2) [Check for overflow] If F =R then Write OVERFLOW Return 3) [Insert element] Q[R] Y 4) [Set rear pointer] If F = 0 then F F + 1 Return Function CQDELETE(F, R, Q, N). Given F and R, pointers to the front and rear of a circular queue, respectively, and a vector Q consisting of N elements, this function deletes and returns the last element of the queue. Y is a temporary variable. Q The queue F Front pointer of queue R Rear pointer of queue Y Element to be deleted 1) [Check for underflow] If F = 0 then Write UNDERFLOW Return(0) 2) [Remove element] Y Q[F] 3) [Is the queue empty?] If F = R

SLICA

RF0 Return(Y) 4) [Increment front pointer] If F = N then F 1 else F F + 1 Return(Y) Tracing: Perform various insertion and deletion operations on circular queue which contains maximum of four elements. Empty fr A f r A f A f A f B r B B B f E r B f C r C C r C D Insert E D r D Insert A Insert B Insert C Insert D Delete A f r E r E E E f F r F r F r F f r C f C f D D D f Delete B Insert F Delete C Delete D Delete E Delete F

then

Deque A deque (double-ended queue) is a linear list in which insertion and deletion are made to or from either end of the structure. It can be represented as: Front Rear Deletion Insertion Insertion

Deletion

Deque is a more general structure than a stack or a queue. Two variation of deque - The input-restricted deque that allows insertion only from one end and output-restricted deque that allows deletion only from one end. Priority Queues A queue in which we are able to insert items or remove items from any one position based on some property (such as the priority of the task to be processed) is often referred to as a priority queue.

SLICA

Task identification R1 R2 .. Ri-1 1 1 .. 1 Priority Ri

O1 2

O2 2

.. ..

Oj-1 2 Oj

B1 3

B2 3

.. ..

Bk-1 3 Bk

Figure represents the priority queue of jobs waiting to use a computer. Priorities of 1, 2, and 3 have been attached to jobs of type real-time, on-line, and batch, respectively. Therefore if a job is initiated with the priority i, it is inserted immediately at the end of the list of other jobs with priority i, for i = 1, 2, or 3. In this example jobs are always removed from the front of the queue, but in general this is not necessary. A priority queue can be conceptualized as a series of queues representing situation in which it is known a priori what priorities are associated with queue items. Priority 1 R1 R2 Ri-1 Priority 1 O1 O2 Oj-1 Priority 1 B1 B2 Bk-1 .. Bk .. Oj .. Ri

Figure shows how the single-priority queue can be visualized as three separate queues, each exhibiting a strictly FIFO behavior. Elements in the second queue are removed only when the first queue is empty. It also shows an efficient storage representation of a priority queue. When elements are inserted, they are always inserted at the end of one of the queues as determined by the priority. If the single seq. storage structure is used for the priority queue, then insertion may mean that the new element must be placed in the middle of the structure and it requires the movement of several items.

SLICA

Anda mungkin juga menyukai