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

5 Queue

Uploaded by

Aya LOTFI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views14 pages

5 Queue

Uploaded by

Aya LOTFI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Queue

Med RHAZZAF
Content
• What is a Queue
• FIFO (Fist-In, First-Out) idea
• Real-world applications
• Queue Operations
• Basic
• complex
• Implementation of Queues
• Array implementation
• Singly linked list implementation
What is a Queue
• A queue is a linear data structure that is open at
both ends and the operations are performed in First
In First Out (FIFO) order.
• We define a queue to be a list in which all additions
to the list are made at one end, and all deletions
from the list are made at the other end.
FIFO (Fist-In, First-Out) idea
Real-world applications
• Print Job Management: In a computer system
with multiple users, print jobs are typically
processed in the order they are received. A
queue data structure can be used to manage
and prioritize print jobs.
Real-world applications
• Task Scheduling: Operating systems use
queues to manage tasks and processes. The
scheduler assigns CPU time to processes in the
order they are added to the queue, ensuring
fairness.
Real-world applications
• Call Center Systems: Incoming customer
service requests or support tickets can be
placed in a queue, ensuring that they are
handled in the order they are received.
Queue Characteristics
Queue operations
• Simple operations:
– Enqueue (push)
– Dequeue (pop
Queue operations
• Complex operations:
– Peek
– Is empty
– Is full
Implementation of Queue
Array implementation
• class QueueArray: • def dequeue(self):
def __init__(self, capacity): if self.is_empty():
self.front = self.size = 0 print("the queue is empty")
return None
self.rear = capacity - 1
result = self.Q[self.front]
self.Q = [None] * capacity
self.front = (self.front + 1) % self.capacity
self.capacity = capacity
self.size = self.size - 1
def is_full(self):
return result
return self.size == self.capacity def peek(self):
def is_empty(self): if self.is_empty():
return self.size == 0 print("Queue is empty")
def enqueue(self, item): return None
if self.is_full(): return self.Q[self.front]
print("the queue is full") def view_rear(self):
return False if self.is_empty():
self.rear = (self.rear + 1) % self.capacity print("Queue is empty")
self.Q[self.rear] = item return None
self.size = self.size + 1 return self.Q[self.rear]
return True
Implementation of Queue
Singly Linked list implementation
• • def dequeue(self):
class Node: class QueueLinkedList:
if self.is_empty():
def __init__(self, def __init__(self): return None
data): self.front = self.rear = None temp = self.front
self.data = data self.front = temp.next
if self.front is None:
self.next = None def is_empty(self):
self.rear = None
return self.front is None return temp
def enqueue(self, item): def peek(self):
temp = Node(item) if self.is_empty():
print("Queue is empty")
if self.rear is None:
return None
self.front = self.rear = temp return self.front.data
return def view_rear(self):
self.rear.next = temp if self.is_empty():
print("Queue is empty")
self.rear = temp
return None
return self.rear.data
Keep in mind
• FIFO Principle: Fist-In, First-Out - The first item added is the
first one removed.
• Operations: Basic operations are enqueue (push) and
dequeue (pop). Use peek to view the first element to serve.
• Applications: Queue are used generally in order to be fair in
distributions management for rare or limited resources.
Quiz
• Quiz 5

You might also like