You are on page 1of 10

DATA STRUCTURE

QUEUE
UNIT 1-PART 3
BCA
Queues in a Computer System
• When a process (program) requires a certain
resource
– printer
– disk access on a network
– characters in a keyboard buffer
• Queue Manipulation Operations
– isEmpty(): returns true or false
– first(): returns copy of value at front
– add(v): adds a new value at rear of queue
– remove(): removes, returns value at front
Queue

• Queue are first in first out type of data


structure (i.e. FIFO)
• In a queue new elements are added to the
queue from one end called REAR end and the
element are always removed from other end
called the FRONT end.
• The people standing in a railway reservation
row are an example of queue.
Queue

• Each new person comes and stands at the


end of the row and person getting their
reservation confirmed get out of the row
from the front end.
• The bellow show figure how the
operations take place on a stack:
10 20 30 40 50

front rear
Queue
• The queue can be implemented into two
ways:
– Using arrays (Static implementation)
– Using pointer (Dynamic implementation)
Implementing a Queue Class
• Implement as a LinkedList attribute value
– insertions and deletions from either end are
efficient, occur in constant O(1) time
– good choice
• Implement as an ArrayList attribute
– poor choice
– adding values at one end, removing at other end
require multiple shifts
Implementing a Queue Class
• Build a Queue from scratch
– build a linked structure to store the queue
elements
• Attributes required
– handle for the head node
– handle for tail node
– integer to store number of values in the queue
– use SinglyLinkedNode class.
Queue Structure

aQueue myHead mySize myTail

...

value0 value1
... valuen-1
Queue Class Methods
• Constructor
– set myHead, myTail to null
– set mySize to zero
• isEmpty()
– return results of comparison mySize == 0
• front()
– return myHead.getValue()
// unless empty
Queue Class Methods
• add()
– create new node, update attribute variables
– if queue is empty, must also update myHead
• remove()
– must check if class not empty
otherwise …
– save handle to first object
– adjust head to refer to node
– update mySize

You might also like