-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_using_linked_list.py
72 lines (57 loc) · 1.56 KB
/
queue_using_linked_list.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
66
67
68
69
70
71
72
class Node:
def __init__(self,item=None,next=None):
self.item=item
self.next=next
class Queue:
def __init__(self):
self.rear=None
self.front=None
self.item_count=0
def is_empty(self):
return self.rear==None
def enqueue(self,data):
n=Node(data)
if self.is_empty():
self.front=n
else:
self.rear.next=n
self.rear=n
self.item_count+=1
def dequeue(self):
if not self.is_empty():
if self.front==self.rear:
self.front=None
self.rear=None
else:
self.front=self.front.next
self.item_count-=1
else:
return IndexError("queue is underflow(empty)")
def get_front(self):
if not self.is_empty():
return self.front.item
else:
return IndexError("queue is underflow(empty)")
def get_rear(self):
if not self.is_empty():
return self.rear.item
else:
return IndexError("queue is underflow(empty)")
def size(self):
if not self.is_empty():
return self.item_count
else:
return IndexError("queue is underflow(empty)")
q1=Queue()
print(q1.is_empty() )
print()
q1.enqueue(10)
q1.enqueue(20)
print(q1.get_front())
print(q1.get_rear())
print(q1.size())
print()
q1.dequeue()
print(q1.get_front())
print(q1.get_rear())
print(q1.size())