-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlisttail.py
195 lines (166 loc) · 4.02 KB
/
linkedlisttail.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#%%
class node:
def __init__(self,val) -> None:
self.val = val
self.next = None
class llist:
def __init__(self) -> None:
self.head = None
self.tail = None
def size(self):
curr = self.head
size=0
while curr!=None:
size+=1
curr=curr.next
return size
def empty(self):
return True if self.head == None else False
def valueAt(self, index):
if self.empty():
return None
curr = self.head
for _ in range(index):
curr = curr.next
return curr.val
def pushFront(self,value):
new = node(value)
new.next = self.head
self.head = new
if self.tail == None:
self.tail = new
def popFront(self):
if self.empty():
return None
ret = self.head.val
d = self.head
self.head = self.head.next
if self.head == None:
self.tail = None
else:
del d
return ret
def pushBack(self,value):
new = node(value)
if self.empty():
self.head = new
else:
self.tail.next = new
self.tail = new
def popBack(self):
if self.empty():
return None
curr = self.head
prev = None
while curr.next:
prev = curr
curr = curr.next
if prev==None:
self.head = None
self.tail = None
else:
prev.next = None
ret = curr.val
del curr
return ret
def front(self):
if self.empty():
return None
return self.head.val
def back(self):
if self.empty():
return None
return self.tail.val
def insert(self,value,index):
if index>self.size():
return
new = node(value)
curr = self.head
prev = None
for i in range(index):
prev = curr
curr = curr.next
if prev:
prev.next = new
else:
self.head = new
new.next = curr
def erase(self,index):
if index>=self.size():
return
curr = self.head
prev = None
for i in range(index):
prev = curr
curr = curr.next
if prev:
prev.next = curr.next
else:
self.head = self.head.next
del curr
def valueNFromEnd(self,n):
siz = self.size()
if n>=siz:
return None
return self.valueAt(siz-1-n)
def reverse(self):
prev,curr,next = None,self.head,None
while curr:
next = curr.next
curr.next = prev
prev = curr
curr = next
self.head,self.tail = prev,self.head
def removeValue(self,value):
prev,curr = None,self.head
while curr:
if curr.val==value:
if prev:
prev.next = curr.next
else:
self.head = self.head.next
return
prev = curr
curr = curr.next
if curr:
del curr
def printList(self):
curr = self.head
while curr!=None:
print(f"{curr.val}",end=" -> ")
curr=curr.next
print("None")
l = llist()
l.reverse()
l.printList()
l.pushBack(6)
l.reverse()
l.printList()
l.pushFront(2)
l.reverse()
l.printList()
l.pushBack(3)
l.reverse()
l.printList()
l.pushFront(5)
l.reverse()
l.printList()
l.pushBack(1)
l.printList()
l.popFront()
l.popFront()
l.popFront()
l.popFront()
l.popFront()
for i in range(l.size()):
print(l.valueAt(i),l.valueNFromEnd(i))
l.erase(0)
l.printList()
l.erase(4)
l.printList()
l.erase(3)
l.erase(1)
l.erase(1)
l.erase(0)
l.printList()
# %%