Lecture - Queues Data Structures & Operations
Lecture - Queues Data Structures & Operations
Lecture - Queues Data Structures & Operations
1
Introduction to Queues
• A queue is a linear data structure that follows
the First-In-First-Out (FIFO) principle.
• This means that the first element added to the
queue will be the first one to be removed.
Characteristics:
• FIFO Principle: The first element added is the
first one removed.
• Linear Structure: Elements are stored in a
sequential manner.
2
Queue
• Queue is a list with the restriction that insertions are done at
one end and deletions are done at the other
– First-In, First-Out ("FIFO”)
– Elements are stored in order of
insertion but don't have indexes.
– Client can only add to the end of the
queue, and can only examine/remove
the front of the queue.
3
Basic queue operations
4
Cont.. Basic Operations
8
Queues in computer science
• Operating systems:
– queue of print jobs to send to the printer
– queue of programs / processes to be run
– queue of network data packets to send
• Programming:
– modeling a line of customers or clients
– storing a queue of computations to be performed in order
10
Using Queues
add(value places given value at back of queue
)
remove() removes value from front of queue and returns it;
throws a NoSuchElementException if queue is empty
peek() returns front value from queue without removing it;
returns null if queue is empty
size() returns number of elements in queue
isEmpty() returns true if queue has no elements
Queue<Integer> q = new LinkedList<Integer>();
q.add(42);
q.add(-3);
q.add(17); // front [42, -3, 17] back
System.out.println(q.remove()); // 42
11
Algorithm for Enqueue
Procedure Enqueue(queue, element)
if queue is full
print "Queue Overflow"
else
rear = rear + 1
queue[rear] = element
end if
End Procedure
12
Dequeue Operation
• Check for Underflow:
– If the queue is empty (i.e., no elements to
remove), report an underflow condition.
• Remove Element:
– Retrieve the element from the position indicated
by the front pointer.
• Update Front Pointer:
– Increment the front pointer to the next position.
13
Algorithm for Dequeue:
Procedure Dequeue(queue)
if queue is empty
print "Queue Underflow"
else
element = queue[front]
front = front + 1
return element
end if
End Procedure
14
Implementing Queue ADT: Array Queue
• Keep track of the number of elements in the
queue, size.
• Enqueue at the back of the array (size).
• Dequeue at the front of the array (index 0).
15
Implementing Queue ADT:
Circular Array Queue
• Neat trick: use a circular array 7 0
front back
18
Exercise: Linked List Queue Implementation
front back
19
Implementing Queues in Python
class Queue: self.rear += 1
def __init__(self, max_size): print(f"Enqueued: {item}")
self.queue = []
self.max_size = max_size def dequeue(self):
self.front = 0 if self.is_empty():
self.rear = -1 print("Queue Underflow")
else:
def is_full(self): item = self.queue[self.front]
return len(self.queue) == self.max_size self.queue = self.queue[1:]
self.rear -= 1
def is_empty(self): print(f"Dequeued: {item}")
return len(self.queue) == 0 return item
21
Example
Queue: [10, 20, 30], Front = 0
Dequeue:
Check if queue is empty: No.
Remove element at position 0: 10.
Update front pointer: Front = 1.
Queue: [20, 30]
22
Visualization
Before Dequeue: After Dequeue:
Front → [10] [20] [30] Front → [20] [30]
^front = 0 ^front = 1
23
Types of Queues
• Simple Queue:
– Also known as a linear queue.
– Follows the basic FIFO principle.
• Circular Queue:
– Overcomes the limitations of the simple queue by
connecting the rear end back to the front, forming
a circle.
– Useful for fixed-size buffer management.
24
Types of Queues
• Priority Queue:
– Elements are processed based on priority rather
than the order they arrive.
– Higher priority elements are dequeued before
lower priority ones.
• Double-Ended Queue (Deque):
– Allows insertion and deletion from both ends
(front and rear).
– Can be used to implement both stacks and queues.
25
Applications of Queues
• Scheduling:
– CPU scheduling and disk scheduling.
– Task scheduling in operating systems.
• Buffer Management:
– Managing data packets in network communications.
– Printer job management.
• Breadth-First Search (BFS):
– A graph traversal algorithm that uses queues.
• Simulation and Modeling:
– Managing customers in a service system (e.g., ticket
queues, call centers).
26
Summary
• Queues follow the FIFO principle.
• Enqueue adds elements to the rear.
• Dequeue removes elements from the front.
• Essential for various applications in computing
and real-world scenarios.
27
Case Study: Simulation
Types of Arrays:
• One-Dimensional Arrays
• Multi-Dimensional Arrays (including two-
dimensional and higher-dimensional arrays)
31
One-Dimensional Arrays
Declaration:
• Syntax (C/C++): type arrayName[size];
• Example: int numbers[5];
• At Declaration: int numbers[5] = {1, 2, 3, 4, 5};
32
Accessing Elements
• Syntax: arrayName[index];
• Example: int firstNumber = numbers[0];
33
Example in C++
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
return 0;
}
34
Multi-Dimensional Arrays
36
Example
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
37
Accessing Elements
Syntax: arrayName[rowIndex][columnIndex];
Example: int firstElement = matrix[0][0];
38
Example in C++
#include <iostream> for (int i = 0; i < 3; i++) {
using namespace std; for (int j = 0; j < 4; j++) {
cout << matrix[i][j] <<
int main() { " ";
int matrix[3][4] = { }
{1, 2, 3, 4}, cout << endl;
{5, 6, 7, 8}, }
{9, 10, 11, 12}
}; return 0;
}
cout << "Elements of the
2D array: " << endl;
39
Higher-Dimensional Arrays
• Three-Dimensional Arrays: Can be thought of
as a cube of elements.
• Declaration (C/C++): type arrayName[size1]
[size2][size3];
• Example: int threeD[2][3][4];
40
Applications of Arrays
• One-Dimensional Arrays:
– Storing a list of items like numbers, names, etc.
– Implementing other data structures like stacks and
queues.
• Two-Dimensional Arrays:
– Representing matrices in mathematics.
– Storing tabular data, such as a spreadsheet or grid.
– Game boards, like chess or tic-tac-toe.
41
Summary
42