-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLL.py
78 lines (64 loc) · 1.77 KB
/
DLL.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
73
74
75
76
77
78
import tarfile
class Node:
def __init__(self, data) :
self.data = data
self.next = None
self.prev = None
class DLL:
def __init__(self):
self.head = None
self.tail = None
def isEmpty(self):
return self.head is None
def insertFirst(self,data):
new_node = Node(data)
if self.isEmpty():
self.head = new_node
self.tail = new_node
else:
new_node.next = self.head
self.head.prev = new_node
self.head = new_node
def insertLast(self,data):
newNode = Node(data)
if self.isEmpty():
self.head = newNode
self.tail = newNode
else:
self.tail.next = newNode
newNode.prev = self.tail
self.tail = newNode
def deleteLast(self):
if self.isEmpty():
print("List is Empty")
return
if self.head == self.tail:
self.head = None
self.tail = None
else:
self.tail = self.tail.prev
self.tail.next = None
def deleteFirst(self):
if self.isEmpty():
print("List is Empty")
return
if self.head == self.tail:
self.head = None
self.tail = None
else:
self.head = self.head.next
self.head.prev = None
def traverse(self):
current = self.head
while current:
print(current.data, end="\n")
current = current.next
print(None)
dl = DLL()
dl.insertFirst(10)
dl.insertLast(20)
dl.insertFirst(5)
dl.insertLast(30)
dl.traverse()
dl.deleteLast()
dl.traverse()