-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_linked_list.py
197 lines (168 loc) · 5.27 KB
/
single_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
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
196
197
"""
@version: python3.6
@author: Fieldhunter
@contact: 1677532160yuan@gmail.com
@time: 2020-05-03
"""
import functools
class Link_Node():
# Stored data is digital
def __init__(self, data):
self.data = data
self.next = None
class Linkedlist():
def __init__(self):
self.__head = None
self.__num = 0
"""
num is the number to be added.
pos_num is which the data is added after.(position ordinal)
It is filled in the header node by default.
"""
def add_data(self, num, pos_num=None):
new_node = Link_Node(num)
if self.__head == None or pos_num == None:
new_node.next = self.__head
self.__head = new_node
self.__num += 1
else:
data_1 = self.__head
finder = False
while data_1.data != pos_num and data_1 != None:
data_1 = data_1.next
if data_1.data == pos_num:
finder = True
if finder == False:
print("No data in need")
else:
new_node.next = data_1.next
data_1.next = new_node
self.__num += 1
def del_data(self, num):
data_1 = None
data_2 = self.__head
finder = False
while not data_2 == None:
if data_2.data == num:
finder = True
if data_1 == None:
if data_2.next == None:
self.__head = None
else:
self.__head = data_2.next
else:
data_1.next = data_2.next
self.__num -= 1
data_2.next = data_2.data = None
break
else:
data_1 = data_2
data_2 = data_2.next
if finder == True:
print("delete OK!")
else:
print("No data")
def find_data(self, num):
data_list = self.__head
pos = 0
while not data_list == None:
pos += 1
if data_list.data == num:
break
else:
data_list = data_list.next
print(pos)
def print_all_data(self):
data_list = self.__head
while data_list != None:
print(data_list.data, end=" ")
data_list = data_list.next
# single linked list inversion
def reversal(self):
try:
data_1 = None
data_2 = self.__head
data_3 = self.__head.next
while not data_3 == None:
data_2.next = data_1
data_1 = data_2
data_2 = data_3
data_3 = data_3.next
data_2.next = data_1
self.__head = data_2
except:
pass
def find_middle_data(self):
middle_pos = self.__num // 2
data_list = self.__head
pos = 1
while pos < middle_pos:
pos += 1
data_list = data_list.next
if middle_pos % 2 != 0 and self.__num != 1:
data_list = data_list.next
try:
print(data_list.data)
except:
print("No data in linkedlist")
def check_ring(self):
result = False
fast_point = self.__head
slow_point = self.__head
while fast_point != None and slow_point != None:
if fast_point.next == slow_point:
result = True
break
else:
if fast_point.next == None:
break
else:
fast_point = fast_point.next.next
slow_point = slow_point.next
if result == True:
print("Find circle")
else:
print("No circle")
"""
Check if the code used to access the linked list's head,Decorator function.
The purpose of simply adding code is to prevent linked list from
being tampered with maliciously and to provide the API for developers.
"""
def __check_code(func):
@functools.wraps(func)
def check(self, code):
if code != 'adsf;{h3096j34ka`fd>&/edgb^45:6':
raise Exception('code is wrong!')
result = func(self, code)
return result
return check
@__check_code
def return_head(self, code):
return self.__head
def return_num(self):
return self.__num
# merge two linked lists
def merge(list1, list2):
data1 = list1.return_head('adsf;{h3096j34ka`fd>&/edgb^45:6')
data2 = list2.return_head('adsf;{h3096j34ka`fd>&/edgb^45:6')
new_link_list = Linkedlist()
while not data1 == None and not data2 == None:
if data1.data >= data2.data:
num = data1.data
data1 = data1.next
new_link_list.add_data(num)
else:
num = data2.data
data2 = data2.next
new_link_list.add_data(num)
if data1 == None:
while not data2 == None:
num = data2.data
data2 = data2.next
new_link_list.add_data(num)
else:
while not data1 == None:
num = data1.data
data1 = data1.next
new_link_list.add_data(num)
return new_link_list