forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear_Queue.py
65 lines (53 loc) · 1.82 KB
/
Linear_Queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class LinearQueue:
def __init__(self, max_size):
# Initialize an empty list to store queue elements
self.queue = []
# Set the maximum size of the queue
self.max_size = max_size
def is_empty(self):
# Check if the queue is empty
return len(self.queue) == 0
def is_full(self):
# Check if the queue has reached its maximum size
return len(self.queue) == self.max_size
def enqueue(self, item):
# Add an item to the rear of the queue
if self.is_full():
print("Queue is full. Cannot enqueue.")
else:
self.queue.append(item)
print(f"Enqueued: {item}")
def dequeue(self):
# Remove and return an item from the front of the queue
if self.is_empty():
print("Queue is empty. Cannot dequeue.")
return None
else:
item = self.queue.pop(0) # Remove the first item (index 0)
print(f"Dequeued: {item}")
return item
def front(self):
# Return the front item without removing it
if self.is_empty():
print("Queue is empty.")
return None
return self.queue[0] # Return the first item without removing it
def display(self):
# Display the current state of the queue
print("Queue:", self.queue)
# Example usage
if __name__ == "__main__":
# Create a new LinearQueue instance with a maximum size of 5
q = LinearQueue(5)
# Add elements to the queue
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
# Display the current state of the queue
q.display()
# Remove an element from the front of the queue
q.dequeue()
# Display the updated state of the queue
q.display()
# Check the front element
print("Front:", q.front())