-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab assignment 3.py
103 lines (92 loc) · 2.68 KB
/
Lab assignment 3.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
class Node:
def __init__(self, val, prev=None, next=None):
self.value = val
self.prev = prev
self.next = next
class DoublyList:
def __init__(self, a):
self.head = Node(None, None, None)
tail = Node(a[0], None, None)
self.head.next = tail
tail.next = self.head
self.head.prev = tail
tail.prev = self.head
for i in range(1, len(a)):
n = Node(a[i])
tail.next = n
n.prev = tail
tail = tail.next
tail.next = self.head
self.head.prev = tail
def showList(self):
n = self.head.next
if self.head.next == None:
print("Empty List")
else:
while (n != self.head):
print(n.value, end=' ')
n = n.next
print()
def insert(self, newElement):
n = Node(newElement)
tail = self.head.prev
tail.next = n
self.head.prev = n
n.prev = tail
n.next = self.head
def insertWithIndex(self, newElement, index):
node = Node(newElement)
temp = self.head.next
for i in range(0, index-1):
if temp != self.head.next:
temp = temp.next
if (temp.next != self.head.next):
node.next = temp.next
node.prev = temp
temp.next = node
if (node.next != None):
node.next.prev = node
def remove(self, index):
temp = self.head.next
if (index == 0):
self.head.next = temp.next
temp = None
return
for i in range(index - 1):
temp = temp.next
if temp is None:
break
if (temp is None):
return
if (temp.next == None):
return
n = temp.next.next
temp.next = None
temp.next = n
def removeKey(self, deleteKey):
temp = self.head.next
if (temp != None):
if (temp.value == deleteKey):
self.head = temp.next
temp = None
return
while (temp == self.head.next):
if temp.value == deleteKey:
break
prev = temp
temp = temp.next
if (temp == self.head.next):
return
prev.next = temp.next
temp = None
#####################################################################
x = DoublyList([10, 30, 40, 50])
x.showList()
x.insert(60)
x.showList()
x.insertWithIndex(20, 2)
x.showList()
x.remove(3)
x.showList()
x.removeKey('20')
x.showList()