0% found this document useful (0 votes)
6 views15 pages

Queue

The document provides an overview of queue data structures, including algorithms for creating, enqueueing, dequeueing, and retrieving data from queues. It also discusses applications of queues in data categorization and introduces circularly linked lists and doubly linked lists as advanced implementations. The algorithms are designed to handle various conditions such as empty queues and underflow scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

Queue

The document provides an overview of queue data structures, including algorithms for creating, enqueueing, dequeueing, and retrieving data from queues. It also discusses applications of queues in data categorization and introduces circularly linked lists and doubly linked lists as advanced implementations. The algorithms are designed to handle various conditions such as empty queues and underflow scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Queue Data Structures and Complex Implementations

Dr. H. NARESH KUMAR

Assistant Professor - Mathematics


School of Arts Sciences Humanities & Education
SASTRA Deemed to be University
Thanjavur, India - 613 401

CSE103 - Data Structures

Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 1 / 15
Create Queue

The create queue operation is rather simple. All we have to do is set


the metadata pointers to null and the count to 0.

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

Although dequeue is also a little more complex than deleting data


from a stack, it starts out much the same.
We must first ensure that the queue contains data.
If the queue is empty, we have underflow and we return false,
indicating that the dequeue was not successful.

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

Algorithm 4: queueFront (queue, dataOut)


Retrieves data at the front of the queue without changing queue
contents.
Pre queue is a metadata structure
dataOut is a reference to calling algorithm variable
Post data passed back to caller
Return true if successful, false if underflow
if queue empty then
return false
end
move data at front of queue to dataOut
return true
end queueFront

Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 8 / 15
Queue Application : Categorizing Data

Queues are one of the more common data-processing structures.


They are found in virtually every operating system and every network
and in countless other areas.
For example, queues are used in business online applications such as
processing customer requests, jobs, and orders.
In a computer system, a queue is needed to process jobs and for
system services such as print spools.

Categoring Data:

To rearrange data without destroying their basic sequence.

Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 9 / 15
Example

Consider the following list of numbers

We want to categorize them into four different groups:


Group 1: less than 10
Group 2: between 10 and 19
Group 3: between 20 and 29
Group 4: 30 and greater
Output

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.

Each node contains two pointers: a backward pointer to its


predecessor and a forward pointer to its successor.

Dr. H. Naresh Kumar (SASHE, SASTRA) Queue Data Structures and Complex Implementations
CSE103 - Data Structures 15 / 15

You might also like