0% found this document useful (0 votes)
10 views5 pages

Dsa 3

The document describes algorithms for implementing a queue and circular queue using an array. For a queue, elements are added to the rear and removed from the front, following FIFO order. A circular queue is similar but the last array position wraps around to the first position, allowing it to reuse the entire space. Pseudocode is provided for queue and circular queue classes with methods for insertion, removal, checking emptiness/fullness, and displaying elements. Sample code implements these algorithms to demonstrate the queue and circular queue data structures.
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)
10 views5 pages

Dsa 3

The document describes algorithms for implementing a queue and circular queue using an array. For a queue, elements are added to the rear and removed from the front, following FIFO order. A circular queue is similar but the last array position wraps around to the first position, allowing it to reuse the entire space. Pseudocode is provided for queue and circular queue classes with methods for insertion, removal, checking emptiness/fullness, and displaying elements. Sample code implements these algorithms to demonstrate the queue and circular queue data structures.
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/ 5

Ex.

No: 3 Array Implementation of Queue and


Circular Queue ADTs
08-08-23
Reg No: URK22CS1026

Aim :
To simulate the working of a queue and circular queue of integers using an array with the
following operations: (a) Insert (b) Delete (c) Display

Description:
The queue is also an abstract data type or a linear data structure, in which the first element is
inserted from one end called REAR(also called the tail), and the deletion of the existing
element takes place from the other end called as FRONT(also called the head). This makes
the queue a FIFO data structure, which means that the element inserted first will also be
removed first. The process to add an element into a queue is called Enqueue and the process
of removal of an element from a queue is called Dequeue.
Circular Queue:
A circular Queue is a linear data structure in which the operations are performed based on
FIFO (First In First Out) principle and the last position is connected back to the first position
to make a circle. It is also called ‘Ring Buffer’.

Algorithm: Queue
1. Define a class queue, with instance variables maxsize, queue, front, and rear.
2. The is_empty() method checks if the queue is empty, i.e., if front is equal to -1.
3. The is_full() method checks if the queue is full, i.e., if rear is equal to maxsize - 1.
4. The enqueue(data) method adds an element data to the queue if it is not full, updating
front and rear accordingly.
5. The dequeue() method removes and returns the front element from the queue if it is
not empty, updating front accordingly.
6. The display() method displays the elements of the queue.
7. The peek() method displays the front element of the queue without removing it.
8. An instance of the queue class, q, is created to access it’s methods.
Program: Queue
class queue:
def __init__(self):
self.maxsize = 5
self.queue = [None] * self.maxsize
self.front = -1
self.rear = -1
def is_empty(self):
return self.front==-1
def is_full(self):
return self.rear==self.maxsize-1
def enqueue(self, data):
if self.is_full():
print("queue is full")
else:
if self.front == -1:
self.front = 0
self.rear = self.rear + 1
self.queue[self.rear] = data
def dequeue(self):
if self.is_empty():
print("queue is empty")
else:
if self.front >self.rear:
print("queue is empty")
self.front=-1
self.rear=-1
return
print("deleted item", self.queue[self.front])
self.queue[self.front] = None
self.front = self.front + 1
def display(self):
if self.is_empty():
print("queue is empty")
else:
print(self.queue)
def peek(self):
if self.is_empty():
print("queue is empty")
else:
print("Peak value:",self.queue[self.front])
q = queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)
q.display()
q.dequeue()
q.dequeue()
q.display()

Output:
Algorithm: Circular Queue
1. Define a class MyCircularQueue, with instance variables size, queue, rear, and front.
2. The is_empty() method checks if the circular queue is empty, if front is equal to -1.
3. The is_full() method checks if the circular queue is full, i.e., if the next position after
rear (considering circular wrap-around) is equal to front.
4. The enqueue() method adds an element value to the circular queue if it is not full,
updating rear accordingly. If the queue is initially empty, front and rear are set to 0.
5. The dequeue() method removes the front element from the circular queue if it is not
empty, updating front accordingly. If the queue becomes empty after dequeuing, front
and rear are reset to -1.
6. The display() method displays the elements of the circular queue.
7. An instance of the MyCircularQueue class, q, is created with queue size to access it’s
methods.

Program: Circular Queue


class MyCircularQueue:
def __init__(self, size):
self.size = size
self.queue = [None] * size
self.rear = -1
self.front = -1
def is_empty(self):
return self.front == -1
def is_full(self):
return (self.rear + 1) % self.size == self.front
def enqueue(self, value):
if (self.is_full() ):
print("Queue is full")
else:
self.rear = (self.rear + 1) % self.size
if (self.front == -1):
self.front = 0
self.rear = 0
self.queue[self.rear] = value
def dequeue(self):
if self.is_empty():
print("Queue is Empty")
else:
x=self.queue[self.front]
self.queue[self.front]=None
if (self.front == self.rear):
self.front = -1
self.rear = -1
else:
self.front = (self.front + 1) % self.size
def display(self):
print(self.queue)
q=MyCircularQueue(5)
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)
q.dequeue()
q.dequeue()
q.dequeue()
q.enqueue(4)
q.enqueue(5)
q.display()
Output:

Result:
These program successfully demonstrate the implementation of Queue and Circular queue
along with its enqueue, dequeue and display methods

You might also like