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

4 Chapter Queue

A queue is a linear data structure that follows the First In First Out (FIFO) principle, where elements are added at the rear and removed from the front. Key operations include enqueue, dequeue, isEmpty, peek, and isFull, with applications in resource management, call centers, and traffic management. A deque, or double-ended queue, allows insertion and deletion from both ends and does not adhere to FIFO.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views15 pages

4 Chapter Queue

A queue is a linear data structure that follows the First In First Out (FIFO) principle, where elements are added at the rear and removed from the front. Key operations include enqueue, dequeue, isEmpty, peek, and isFull, with applications in resource management, call centers, and traffic management. A deque, or double-ended queue, allows insertion and deletion from both ends and does not adhere to FIFO.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Computer Science – Queue

Chapter 4 – QUEUE
Introduction To Queue
 A queue is a non-primitive linear data structure, it is a order collection of elements in which
the elements are inserted at one end called REAR END and the removal of existing elements
takes place from the other end called as FRONT.
 It follows the FIFO approach i.e. First In First Out. In the queue, the order of insertion is the
same as the order of deletion of elements.

First In First Out (FIFO):


 Queue follows the principal of First In First Out (FIFO), since the element entering first in
the queue will be the first one to come out of it.
 It is also known as a First Come First Served (FCFS) approach.
 In queue new items are added at one end, usually called the REAR, and items removed from
the other end usually called the FRONT of the queue.
 REAR is also known as TAIL and FRONT as HEAD of a queue.

Applications of Queue
Following are some examples of application of queue in computer science:
1. Serving request on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life scenario, call center phone systems use Queues to hold people calling them in an
order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same order as
they arrive i.e First Come First Served.
4. Traffic Management: Circular queues are used to manage the traffic lights in a traffic
management system for switching the traffic lights one by one, in order.
5. Message Buffering: Message buffer stores the messages in the order they arrive and
processes them one by one.
6. It also helps in network monitoring.
7. Queues are used to manage network traffic.
1 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA
Computer Science – Queue

Operations on Queue
1. ENQUEUE: This operation is used to insert a new element to the queue at the rear end.
Inserting elements beyond capacity of the queue will result in an exception – known as
“Queue is Overflow”.
2. DEQUEUE: This operation is used to remove one element at a time from the front of the
queue. We can delete elements from a queue until it is empty, trying to delete an element
from an empty queue will result in exception – known as “Queue is Underflow”.
3. ISEMPTY: It is used to check whether the queue has any element or not, it returns True if
queue is empty otherwise it returns False.
4. PEEK: It used to view elements at the front of the queue, without removing it from the
queue.
5. ISFULL: It is used to check whether any more elements can be added to the queue or not, it
returns True if queue is full otherwise it returns False.

Implementation Of Queue Using Python


For creating a queue structure in the program, following functions need to be defined:
- To create a queue named myQueue. We can create it by assigning an empty list.
myQueue=list( )

2 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

- A function enqueue( ) to insert a new element at the end of the queue. The function has two
parameters – name of the queue and element which is to be inserted in the queue.
def enqueue( myQueue, element):
myQueue.append(element)
- A function isEmpty( ) to check, if the queue has an element or not? This can be done by
checking the length of the queue. The function has a parameter – name of the queue and
returns True if the queue is empty False otherwise.
def isEmpty(myQueue):
if len(myQueue)==0:
return True
else :
return False
- A function dequeue( ) to delete an element from the front of the queue. It has one parameter
– name of the queue and returns the deleted element. The function first checks if the queue is
empty or not, for successful deletion.
def dequeue(myQueue):
if isEmpty(myQueue):
print(“Queue is empty”)
else:
return myQueue.pop(0)
- A function size( ) to get the number of elements in the queue. The function has one parameter
– name of the queue and returns the number of elements in the queue.
def size(myQueue):
return len(myQueue)
- A function peek( ) to simply read, but not to delete, the element at the front end of the queue.
For this, we can read the element at index[0] of the queue. The function has one parameter-
name of the queue and returns the value of element at Front if the queue is not empty, None
otherwise.
def peek(myQueue):
if isEmpty(myQueue):
print(‘Queue is empty’)
return None
else:
3 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA
Computer Science – Queue

return myQueue[0]

4 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

5 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

Introduction To Deque:
 The deque stands for Double Ended Queue
 Deque is a linear data structure where the insertion and deletion operations are performed
from both ends.
 We can say that deque is a generalized version of the queue.
 It does not follow the FIFO (First In First Out) rule.

The operation of a deque is given as follows:

6 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

Applications of Deque

Operations On Deque

7 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

Implementation Of Deque Using Python


Deque is also an ordered linear list, hence we use list data type to create deque.

The program should have the following functions/ statements defined in it:
- A statement to create deque, with name myDeque.
myDeque = list( )

8 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

- A function insertFront( ), to insert an element at the front of deque. We will use insert( )
with index 0 for it.
def insertFront(myDeque, element):
myDeque.insert(0, element)

- A function insertRear( ), to insert an element at the rear of deque.


def insertRear(myQueue, element):
myQueue.append(element)

- A function isEmpty( ), to check the presence of elements in deque.


def isEmpty(myQueue):
if len(myQueue)==0:
return True
else:
return False

- A function deletionRear( ), to delete an element from the rear of the deque. We will use
pop( ) to delete the last element of the deque.

- A function getFront( ), to read value from the front of deque, without removing it from the
queue when the queue is not empty.

9 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

- A function getRear( ), to read value from the rear of the deque, without removing it from
thte deque.

10 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

11 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

12 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

13 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

14 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA


Computer Science – Queue

15 SOWMYA RANI G S, LECTURER, DEPT OF COMPUTER SCIENCE, CHITRADURGA

You might also like