-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeque_using_linked_list.py
56 lines (46 loc) · 1.31 KB
/
deque_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
class Dequeue:
def __init__(self):
self.mydequeue=[]
def is_empty(self):
return len(self.mydequeue)==0
def insert_front(self,data):
self.mydequeue.insert(0,data)
def insert_rear(self,data):
self.mydequeue.append(data)
def delete_front(self):
if not self.is_empty():
self.mydequeue.pop(0)
else:
raise IndexError("dequeue is empty")
def delete_rear(self):
if not self.is_empty():
self.mydequeue.pop()
else:
raise IndexError("dequeue is empty")
def get_front(self):
if not self.is_empty():
return self.mydequeue[0]
else:
raise IndexError("dequeue is empty")
def get_rear(self):
if not self.is_empty():
return self.mydequeue[-1]
else:
raise IndexError("dequeue is empty")
def size(self):
if not self.is_empty():
return len(self.mydequeue)
else:
raise IndexError("dequeue is empty")
dq=Dequeue()
dq.insert_front(10)
dq.insert_front(20)
dq.insert_rear(30)
dq.insert_rear(40)
print(dq.get_front())
print(dq.get_rear())
print("===========")
dq.delete_front()
dq.delete_rear()
print(dq.get_front())
print(dq.get_rear())