-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.py
54 lines (48 loc) · 1.29 KB
/
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
# Enter your code here. Read input from STDIN. Print output to STDOUT
class QueueNode:
def __init__(self, data):
self.data = data
self.next = None
class QueueEverything:
def __init__(self):
self.head = None
self.tail = None
def enqueue(self, data):
new_node = QueueNode(data)
if self.tail:
self.tail.next = new_node
self.tail = new_node
if not self.head:
self.head = new_node
def dequeue(self):
if self.head:
self.head = self.head.next
else:
self.tail = None
print("No elements available")
def print_first_element(self):
if self.head:
print(self.head.data)
else:
print("No elements available")
def print_elements(self):
if self.head:
temp = self.head
while temp:
print(temp.data)
temp = temp.next
else:
print("No elements available")
q = int(input())
qe = QueueEverything()
for i in range(q):
t = input()
a = t.split(" ")
t = a[0]
if t == '1':
ele = a[1]
qe.enqueue(ele)
elif t == '2':
qe.dequeue()
elif t == '3':
qe.print_first_element()