0% found this document useful (0 votes)
2 views12 pages

Waheed Lab Task(s) 7

The document outlines the implementation of a queue using an array, including functions for enqueueing, dequeueing, displaying elements, and a menu-driven interface. It also describes a test procedure for both a standard queue and a circular queue with specific operations and expected outcomes. The maximum size of the queue is set to 5, but can be adjusted as needed.

Uploaded by

sajidahmed332a
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)
2 views12 pages

Waheed Lab Task(s) 7

The document outlines the implementation of a queue using an array, including functions for enqueueing, dequeueing, displaying elements, and a menu-driven interface. It also describes a test procedure for both a standard queue and a circular queue with specific operations and expected outcomes. The maximum size of the queue is set to 5, but can be adjusted as needed.

Uploaded by

sajidahmed332a
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/ 12

Lab Task(s): 7

Write a program as follows for QUEUE

----------------------Array based implementation of QUEUE----------------------------------- 1.

Enqueue an element to queue

2. Dequeue an element from queue

3. Display all

4. Exit

-------------------------------------------------------------------------------------------------------------

MAX_SIZE = 5 # You can increase this size as needed

queue = [None] * MAX_SIZE

front = -1

rear = -1

def enqueue():

global rear, front

if rear == MAX_SIZE - 1:

print("Queue is Full (Overflow)!")

else:

element = input("Enter the element to

enqueue: ") if front == -1:

front = 0

rear += 1

queue[rear] = element

print(f"{element} enqueued successfully.")


def dequeue():

global front, rear

if front == -1 or front > rear:

print("Queue is Empty (Underflow)!")

else:

removed_element = queue[front]

print(f"Dequeued element:

{removed_element}") front += 1

if front > rear:

# Reset queue after last element is

dequeued front = rear = -1

def display():

if front == -1 or front >

rear:

print("Queue is Empty!")

else:

print("Queue elements:")

for i in range(front, rear +

1):

print(queue[i])

# Menu
while True:
print("\n----------------------Array based implementation of
QUEUE-----------------------------------")
print("1. Enqueue an element to queue")
print("2. Dequeue an element from queue")
print("3. Display all")
print("4. Exit")

print("-------------------------------------------------------------------------------------------------------------")

choice = input("Please Enter Your Choice: ")

if choice == '1':

enqueue()

elif choice == '2':

dequeue()

elif choice == '3':

display()

elif choice == '4':

print("Exiting program. Goodbye!")

break

else:

print("Invalid choice! Please enter 1, 2, 3, or

4.")
Q2:Test the program using the following procedure: QUEUE of
size N=6 Call enQueue(5)
Call enQueue(2)
Call enQueue(3)
Call deQueue ()
call deQueue()
Call enQueue(6)
Call enQueue(3)
Call Display()

OPERATIONS STEP BY STEP:

1.enQueue(5) → Queue: [5]


2.enQueue(2) → Queue: [5, 2]
3.enQueue(3) → Queue: [5, 2, 3]
4.deQueue() → Removes 5 →
Queue: [2, 3] 5.deQueue() →
Removes 2 → Queue: [3]
6.enQueue(6) → Queue: [3, 6]
7.enQueue(3) → Queue: [3, 6, 3]
8.Display() → Output:
Q3:Write a program as follows for Circular
QUEUE.

----------------------Array based implementation of


QUEUE-----------------------------------
1. Enqueue an element to queue
2. Dequeue an element from queue
3. Display all
4. Exit
-------------------------------------------------------------------
------------------------------------------
Please Enter Your Choice:

MAX_SIZE = 5 # You can change the size of the


queue class CircularQueue: def init(self,
size=MAX_SIZE): self.queue = [None] * size
self.front = -1 self.rear = -1 self.size = size
def is_full(self):
return (self.rear + 1) % self.size
==
self.front

def is_empty(self):
return self.front ==
-1

def enqueue(self, value):

if self.is_full():
print("Queue is Full!")
return
if self.is_empty():
self.front = 0
self.rear = (self.rear + 1) %
self.size self.queue[self.rear] =
value
print(f"Enqueued: {value}")

def dequeue(self):
if self.is_empty():
print("Queue is Empty!")
return
removed = self.queue[self.front]
if self.front == self.rear:
self.front = self.rear = -1 # Queue
is now empty
else:
self.front = (self.front + 1) %
self.size
print(f"Dequeued: {removed}")

def display(self):
if self.is_empty():
print("Queue is Empty!")
return
print("Queue Elements:")
i = self.front
while True:
print(self.queue[i], end="
") if i == self.rear:
break
i = (i + 1) % self.size
print()

Menu-driven program
def main(): q = CircularQueue() while True: print("\
n----------------------Array based implementation of
QUEUE-----------------------------------") print("1. Enqueue an
element to queue") print("2. Dequeue an element
from queue") print("3. Display all") print("4. Exit")
print("---------------------------------------------------------------------
----------------------------------------") choice = input("Please
Enter Your Choice: ")
if choice == '1':
value = input("Enter the value to
enqueue: ")
q.enqueue(value)
elif choice == '2':
q.dequeue()
elif choice == '3':
q.display()
elif choice == '4':
print("Exiting program.")
break
else:
print("Invalid choice. Please enter
a valid option.")

if name == "main": main()

Q4:Test the program using the following


procedure: QUEUE of size N=6

Call enQueue(5)

Call enQueue(2)

Call enQueue(3)
Call deQueue
()
call
deQueue()
Call
enQueue(6)
Call
enQueue(3)
Call Display()

Initial Setup:

•Queue size: 6
•Queue is empty at
start.
•Let's assume a typical circular queue structure
with: ofront pointing to the index of the front
element.
orear pointing to the index where the next
element will be inserted.

We’ll assume front = -1 and rear = -1


initially to indicate the queue is empty.

Step-by-step Execution:
1. enQueue(5)

•Queue: [5, _, _, _, _, _]
•front = 0, rear = 0
2. enQueue(2)

•Queue: [5, 2, _, _, _, _]
•rear = 1
3. enQueue(3)

•Queue: [5, 2, 3, _, _, _]
•rear = 2
4. deQueue()

•Removes 5 (element at front)


•front = 1
•Queue (logically): [_, 2, 3, _, _, _]
5. deQueue()

•Removes 2
•front = 2
•Queue (logically): [_, _, 3, _, _, _]
6. enQueue(6)

•rear = 3
•Queue: [_, _, 3, 6, _, _]
7. enQueue(3)

•rear = 4
•Queue: [_, _, 3, 6, 3, _]
8. Display()

•front = 2, rear = 4
•Elements from front to rear: 3, 6, 3

You might also like