-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData_struck_Linklist.py
75 lines (67 loc) · 1.82 KB
/
Data_struck_Linklist.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Linklist:
def __init__(self):
self.head = None
self.tail = None
def append(self, data):
node = Node(data)
if not self.head:
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
def iter(self):
cur = self.head
yield cur.data
while cur.next:
cur = cur.next
yield cur.data
def insert(self, idx, data):
cur = self.head
cur_idx = 0
while cur_idx < idx -1:
cur = cur.next
if cur is None:
raise Exception('list is shorter than index')
cur_idx += 1
node = Node(data)
node.next = cur.next
cur.next = node
if node.next is None:
self.tail = node
def remove(self, idx):
cur = self.head
cur_idx = 0
while cur_idx < idx-1:
cur = cur.next
if cur is None:
raise Exception('List is shorter than index')
cur_idx += 1
cur.next = cur.next.next
if cur.next is None:
self.tail = cur
def pop(self, idx=None):
if idx:
print('Pop start')
self.remove(idx)
else:
cur = self.head
while True:
cur = cur.next
if not cur.next.next:
break
cur.next =None
self.tail = cur
if __name__ == '__main__':
lst = Linklist()
for i in range(10):
lst.append(i)
# lst.insert(3,133)
# lst.remove(3)
# lst.pop()
for u in lst.iter():
print(u)