Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

20CS2013

L-Data Structures and Algorithms Lab URK20CS1116

Ex. No. 3 Implementation of queue data structure using array


26/07/2021
Date of Exercise
https://youtu.be/kzeJUr0vEQ4
Youtube Link

Aim:
To Implementation of queue data structure using arrays.

Algorithm:
Step 1: Start the program.
Step 2: Declare front = 0, rear = 0, capacity = 5 and queue = [].
Step 3: Create while loop and its true then display '***** Implementation of queue *****' and
1.insert\n2.delete\n3.display\n4.exit'.
Step 4: Read input integer choice from user.
Step 5: If choice is equal to 1 then check if rear is equal to capacity then display 'queue is full !'.
Step 6: Else read input integer new_element and insert new_element to queue.
Step 7: Compute rear += 1and display 'New element is successfully inserted'.
Step 8: Else if choice is equal to 2 then check if front is equal to rear then display 'queue is
empty!'.
Step 9: Else delete front element and display deleted element.
Step 10: Else if choice is equal to 3 then check if front is equal to rear then display 'queue is
empty!'.
Step 11: Else display elements of the queue.
Step 12: Else if choice is equal to 4 then break the while loop.
Step 13: Else display 'Invalid choice !'.
Step 14: Stop the program.

Program:
front = 0
rear = 0
capacity = 5
queue = []
while True:
print('***** Implementation of queue *****')
print('1.insert\n2.delete\n3.display\n4.exit')
choice = int(input('Enter your choice : '))
if choice == 1:

10
Ex.no: 3 | Implementation of queue data structure using array
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116

if rear == capacity:
print('queue is full !')
else:
new_element = int(input('Enter the new element : '))
queue.append(new_element)
rear += 1
print('New element is successfully inserted')

elif choice == 2:
if front == rear:
print('queue is empty !')
else:
deleted_element = queue[front]
for i in range(rear-1):
queue[i] = queue[i + 1]
rear -= 1;
print('deleted element is ' + str(deleted_element))

elif choice == 3:
if front == rear:
print('queue is empty !')
else:
print('Elements of queue : ')
for i in range(rear):
print(queue[i])

elif choice == 4:
break

else:
print('Invalid choice !')

11
Ex.no: 3 | Implementation of queue data structure using array
20CS2013 L-Data Structures and Algorithms Lab URK20CS1116

Output:

Result:
The above program has been executed for sample input values and output is
verified.

12
Ex.no: 3 | Implementation of queue data structure using array

You might also like