-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkListFunc.py
247 lines (227 loc) · 4.73 KB
/
LinkListFunc.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import random
from DataStructureClass import LinkL
# 创建链表
def newRandomLink(n):
node = None
for i in range(n):
node = LinkL(random.randint(0,n), node)
i+=1
node = LinkL("头结点", node)
return node
def newLink(n):
node = None
for i in range(n):
node = LinkL(n-i,node)
i+=1
node = LinkL("头结点", node)
return node
# 释放节点(伪)
def freeNode(node):
print("节点%s已删除"%node.data)
# 输出链表
def printLink(l):
print("准备输出")
s = ""
while(l.link != None):
l = l.link
s=s + str(l.data)+" "
print(s)
# 按值删除
def delLink (l,x):
pNode = None
while (l.link != None):
if l.link.data == x:
pNode = l.link
l.link = l.link.link
freeNode(pNode)
l = l.link
def re1 (l):
# 空链表或单元素链表直接返回
if l.link==None or l.link.link==None :
return l
# 第一个节点
p1 = l.link
# 第二个节点
p2 = p1.link
# 反转完成的部分
p3 = None
while p2.link != None:
p1.link = p3
p3 = p1
p1 = p2
p2 = p2.link
# 倒数第二节点指向已反序列
p1.link = p3
# 倒数第一节点指向倒数第二节点
p2.link = p1
l.link = p2
def re2 (l):
p1 = None
p2 = None
p1 = l.link
l.link = None
while p1 != None:
p2 = p1.link
p1.link = l.link
l.link = p1
p1 = p2
def sortLink(l):
old = l
p1 = old.link
old.link =None
while p1!= None:
p2 = p1.link
old = l
while old!= None and old.link != None and old.link.data < p1.data:
old = old.link
p1.link = old.link
old.link = p1
p1 = p2
def reSort(l1,l2):
p1 = None
p2 = None
r = None
p1 = l1.link
p2 = l2.link
l1.link = None
l2.link = None
while p1 != None and p2 != None:
if p1.data < p2.data:
r = p1
p1 = p1.link
else:
r = p2
p2 = p2.link
r.link = l1.link
l1.link = r
if p1 == None:
while p2 != None:
r = p2
p2= p2.link
r.link = l1.link
l1.link = r
if p2 == None:
while p1 != None:
r = p1
p1 = p1.link
r.link = l1.link
l1.link = r
def exc(l1,l2):
p1 =None
p2 = None
s = None
r = None
p1 = l1.link
p2 = l2.link
l1.link = None
s = l1
freeNode(l2)
l2.link = None
while p1 != None and p2 != None:
if p1.data == p2.data:
s.link = p1
s = p1
p1 = p1.link
r = p2
p2 = p2.link
freeNode(r)
s.link = None
elif p1.data < p2.data:
r = p1
p1 = p1.link
freeNode(r)
r.link == None
else:
r = p2
p2 = p2.link
freeNode(r)
r.link == None
while p1 != None:
s = p1
p1 = p1.link
freeNode(s)
while p2 != None:
s = p2
p2 = p2.link
freeNode(s)
def son(l1,l2):
p1 = None
p2 = None
p1 = l1.link
p2 = l2.link
while p1 != None and p2 != None:
if p1.data == p2.data:
p2 = p2.link
else:
p2 = l2.link
p1 = p1.link
if p2 == None:
print("true")
else:
print("false")
def refind(l,n):
p1 = None
p2 =None
p1 = l.link
p2 = l.link
while n > 0 and p2 != None:
p2 = p2.link
n-=1
if p2 == None:
print(0)
return
while p2 != None:
p1 = p1.link
p2 = p2.link
print(p1.data)
print(1)
if __name__ == '__main__':
'''(按值删除测试)
LinkList = newLink(10)
printLink(LinkList)
delLink(LinkList,10)
printLink(LinkList)
'''
''' 反序
LinkList = newLink(10)
printLink(LinkList)
re1(LinkList)
# re2(LinkList)
printLink(LinkList)
'''
''' 排序
LinkList = newRandomLink(10)
printLink(LinkList)
sortLink(LinkList)
printLink(LinkList)
'''
'''链表反续归并
LinkList1 = newLink(10)
LinkList2 = newLink(10)
printLink(LinkList1)
reSort(LinkList1,LinkList2)
printLink(LinkList1)
'''
'''有序链表并集
l1 = newRandomLink(10)
l2 = newRandomLink(10)
sortLink(l1)
sortLink(l2)
printLink(l1)
printLink(l2)
exc(l1,l2)
printLink(l1)
'''
'''
l1 = newLink(20)
l2 = newLink(10)
son(l1,l2)
delLink(l1, 9)
son(l1, l2)
delLink(l2, 9)
delLink(l2, 1)
son(l1, l2)
'''
l1 = newLink(10)
printLink(l1)
refind(l1,15)