Lecture 14: Simple Data Structure - : Summary of Lecture: - What Is A Queue? - What Operations Can Be Performed On It?
Lecture 14: Simple Data Structure - : Summary of Lecture: - What Is A Queue? - What Operations Can Be Performed On It?
Summary of Lecture:
• What is a Queue?
• What operations can be performed on it?
A queue is a linear list in which items may be added only at one end and items may be
removed-only at the other end. The name "queue" likely comes from the everyday use of
the term. Consider: queue of people waiting at a bus stop, as pictured in fig. below. Each
new person who comes takes his or her place at the end of the line, and when the bus
comes, the people at the front of the line board first Clearly, the first person in the line is
the first person to leave. Thus queues are also called first-in first-out (FIFO) lists.
Another example of a queue is a batch of jobs waiting to be processed, assuming no job
has higher priority than the others. .
76
A queue is a linear list of elements in which deletions can take place only at one end,
called the front, and insertions can take place only at the other end, called the rear. The
terms "front" and "rear” are used in describing a linear list only when it is implemented
as, a queue.
Queues are also called first-in first-out (FIFO) lists, since the first element in a queue
will be the first element out of the queue. In other words, the order in which elements
enter a queue is the order in which they leave. This contrasts with stacks, which are last-
in first-out (LIFO) lists.
Queues abound in everyday life. The automobiles waiting to pass through an
intersection form a queue, in which the first car in line is the first car through; the people
waiting in line at a bank form a queue, where the first person in line is the first person to
be waited on; and so on. An important example of a queue in computer science occurs in
a timesharing system,)n which programs with the same priority form a queue while
waiting to be executed. (Another structure, called a priority queue, is discussed later on.)
Representation of Queues
Queues may be represented in the computer in various ways, usually by means of one-
way lists or linear arrays. Unless otherwise stated or implied, each of our queues will be
maintained by a linear array QUEUE and two pointer variables: FRONT, containing the
location of the front element of the queue; and REAR, containing the location of the rear
element of the queue. The condition FRONT = NULL will indicate that the queue is
empty.
Figure below indicates the way elements will be deleted from the queue and the way
new elements will be added to the queue. Observe that whenever an element is deleted
from the queue, the value of FRONT is increased by 1; this can be implemented by the
77
assignment
FRONT: = FRONT + 1
Similarly, 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
This means that after N insertions, the rear element of the queue will occupy QUEUE [N]
or, in other words, eventually the queue will occupy the last part of the array. This occurs
even through the queue itself may not contain many elements.
Suppose we want to insert an element ITEM into a queue at the time the queue does
occupy the last part of the array, i.e., when REAR = N. One way to do this is to simply
move the entire queue to the beginning of the array, changing FRONT and REAR
accordingly, and then inserting ITEM as above. This procedure may be very expensive.
The procedure we adopt is to assume that the array
78
Figure: Array representation of a queue
QUEUE is circular, that is, that QUEUE [l] comes after QUEUE [N] in the array. With this
assumption, we insert ITEM into the queue by assigning ITEM to QUEUE [l]. Specifically,
instead of increasing REAR to N + 1, we reset REAR = 1 and then assign
79
Suppose that our queue contains only one element, i.e., suppose that
80
Procedure : QDELETE (QUEUE, N, FRONT, REAR, ITEM)
This procedure deletes an element from a queue and assigns it to
The variable ITEM.
81