1"""put and get items from queue"""
2>>> from Queue import Queue
3>>> q = Queue()
4>>> q.put(1)
5>>> q.put(2)
6>>> q.put(3)
7>>> print list(q.queue)
8[1, 2, 3]
9
10>>> q.get()
111
12>>> print list(q.queue)
13[2, 3]
1class Queue:
2 def __init__(self, capacity):
3 self.front = self.size = 0
4 self.rear = capacity - 1
5 self.Q = [None] * capacity
6 self.capacity = capacity
7
8 def isFull(self):
9 return self.size == self.capacity
10
11 def isEmpty(self):
12 return self.size == 0
13
14 def EnQueue(self, item):
15 if self.isFull():
16 print("Full")
17 return
18
19 self.rear = (self.rear + 1) % (self.capacity)
20 self.Q[self.rear] = item
21 self.size = self.size + 1
22 print("%s enqueue to queue" %str(item))
23
24 def DeQueue(self):
25 if self.isEmpty():
26 return "Empty"
27
28 print("%s dequeued from queue" %str(self.Q[self.front]))
29 self.front = (self.front + 1) % (self.capacity)
30 self.size = self.size - 1
31
32 def que_front(self):
33 if self.isEmpty():
34 print("The Queue is empty")
35
36
37 print("Front item is ", self.Q[self.front])
38
39 def que_rear(self):
40 if self.isEmpty():
41 print("Queue is Empty")
42
43 print("The rear item is ", self.Q[self.rear])
44
45
46queue = Queue(30)
47queue.EnQueue(10)
48queue.EnQueue(20)
49queue.EnQueue(30)
50queue.EnQueue(40)
51queue.EnQueue(50)
52queue.que_front()
53print()
54queue.DeQueue()
55queue.que_front()
56queue.que_rear()
57print()
58queue.DeQueue()
59queue.que_front()
60queue.que_rear()
61
62
1# Demonstrate queue implementation using list
2
3# Initializing a queue
4queue = []
5
6# Adding elements to the queue
7queue.append('a')
8queue.append('b')
9print(queue)
10
11# Removing elements from the queue
12print("\nElements dequeued from queue")
13print(queue.pop(0))
14print(queue.pop(0))
15
16print("\nQueue after removing elements")
17print(queue)
18
19# print(queue.pop(0)) will raise and IndexError as the queue is now empty
1#creating a queue using list
2
3#defining a list
4
5my_queue =[]
6
7#inserting the items in the queue
8
9my_queue.append(1)
10
11my_queue.append(2)
12
13my_queue.append(3)
14
15my_queue.append(4)
16
17my_queue.append(5)
18
19print("The items in queue:")
20
21print(my_queue)
22
23#removing items from queue
24
25print(my_queue.pop(0))
26
27print(my_queue.pop(0))
28
29print(my_queue.pop(0))
30
31print(my_queue.pop(0))
32
33#printing the queue after removing the elements
34
35print("The items in queue:")
36
37print(my_queue)