Queue
Queue
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 1 / 15
Create Queue
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 2 / 15
Algorithm 1: createQueue
Creates and initializes queue structure.
Pre queue is a metadata structure
Post metadata elements have been initialized
Return Queue head
1 allocate queue head
2 set queue front to null
3 set queue front to null
4 set queue count to 0
5 return queue head
end createQueue
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 3 / 15
Enqueue
The enqueue is a little more complex than inserting data into a stack.
To develop the insertion algorithm, we need to analyze two different
queue conditions: insertion into an empty queue and insertion into a
queue with data.
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 4 / 15
Algorithm 2: enqueue(queue, dataIn)
This algorithm inserts data into a queue.
Pre queue is a metadata structure
Post dataIn has been inserted
Return true if successful, false if overflow
if queue full then
return false
end
allocate (new node)
move dataIn to new node data
if empty queue then
set queue front to address of new data
else
set next pointer of rear node to address of new node
end
set new node next to null pointer
set queue rear to address of new node
increment queue count
return true
end enqueue
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 5 / 15
Dequeue
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 6 / 15
Algorithm 3: dequeue (queue, item)
This algorithm deletes a node from a queue.
Pre queue is a metadata structure
item is a reference to calling algorithm variable
Post data at queue front returned to user through item and front
element deleted
Return true if successful; false if underflow
if queue empty then
return false
end
move front data to item
if only 1 node in queue then
set queue rear to null
end
set queue front to queue front next
decrement queue count
return true
end dequeue
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 7 / 15
Queue Front
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 8 / 15
Queue Application : Categorizing Data
Categoring Data:
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 9 / 15
Example
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 10 / 15
Algorithm 5: categorize
Group a list of numbers into four groups using four queues.
1 createQueue (q0to9)
2 createQueue (q10to19)
3 createQueue (q20to29)
4 createQueue (qOver29)
5 fillQueues (q0to9, q10to19, q20to29, qOver29)
6 printQueues (q0to9, q10to19, q20to29, qOver29)
end categorize
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 11 / 15
Algorithm 6: fillQueues (q0to9, q10to19, q20to29, qOver29)
This algorithm reads data from the keyboard and places them in one of
four queues.
Pre all four queues have been created
Post queues filled with data
for not end of data do
read (number)
if number < 10 then
enqueue (q0to9, number)
else if number < 20 then
enqueue (q10to19, number)
else if number < 30 then
enqueue (q20to29, number)
else
enqueue (qOver29, number)
end
end
end fillQueues
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 12 / 15
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 13 / 15
Circularly Linked Lists
In a circularly linked list implementation, the last node’s link points to
the first node of the list.
Circularly linked lists are primarily used in lists that allow access to
nodes in the middle of the list without starting at the beginning.
Insertion into and deletion from a circularly linked list follow the same
logic patterns used in a singly linked list except that the last node
points to the first node.
Therefore, when inserting or deleting the last node, in addition to
updating the rear pointer in the header we must also point the link
field to the first node.
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 14 / 15
Doubly linked list
A doubly linked list is a linked list structure in which each node has a
pointer to both its successor and its predecessor.
Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 15 / 15