-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
175 lines (144 loc) · 4.39 KB
/
main.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
# This script merges two sorted linked lists
#
# This script is a part of the Easy Python project which creates a number
# sample python scripts to answer simple programming questions. The
# entire project is accessible at https://github.com/okany/easypython.
# Copyright (c) 2021 Okan Yilmaz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
class linkNode():
def __init__(self, value):
self.value = value
self.next = None
class linklist():
def __init__(self, alist, debug = False):
self.alist = alist
self.head = None
if (alist == None or alist == []):
None
else:
self.head = prev = linkNode(self.alist[0])
for value in alist[1:]:
prev.next = linkNode(value)
prev = prev.next
def merge(self, bll):
merged = list()
blist = bll.head
alist = self.head
if blist == None:
merged = self.head
elif alist == None:
merged = blist
else:
if(self.head.value <= blist.value):
prev = merged = alist
alist = alist.next
else:
prev = merged = blist
blist = blist.next
while alist and blist:
if debug:
print("alist={} alist.value={} blist={} blist.value={} prev={} prev.value={}".
format(alist, alist.value, blist, blist.value, prev, prev.value))
if alist.value <= blist.value:
prev.next = alist
alist = alist.next
else:
prev.next = blist
blist = blist.next
prev = prev.next
if alist:
prev.next = alist
else:
prev.next = blist
self.head = merged
def print(self):
if self.head:
current = self.head
while current:
print("({})".format(current.value), end="")
current = current.next
if current:
print("->", end="")
print()
else:
print("Nil")
if __name__=="__main__":
tno = 0
tno += 1
debug = False
alist = [1, 3, 5, 10, 15, 19, 25]
blist = [0, 4, 6, 10, 11, 12, 27, 29]
all = linklist(alist, debug)
bll = linklist(blist, debug)
print("TEST#{} LIST A:".format(tno))
all.print()
print("LIST B:".format(tno))
bll.print()
all.merge(bll)
print("MERGED:".format(tno))
all.print()
tno += 1
debug = False
alist = []
blist = [0, 4, 6, 10, 11, 12, 27, 29]
all = linklist(alist, debug)
bll = linklist(blist, debug)
print("LIST A:".format(tno))
all.print()
print("LIST B:".format(tno))
bll.print()
all.merge(bll)
print("MERGED:".format(tno))
all.print()
tno += 1
debug = False
alist = [1, 3, 5, 10, 15, 19, 25]
blist = None
all = linklist(alist, debug)
bll = linklist(blist, debug)
print("LIST A:".format(tno))
all.print()
print("LIST B:".format(tno))
bll.print()
all.merge(bll)
print("MERGED:".format(tno))
all.print()
tno += 1
debug = False
alist = [1, 3, 5, 10, 15, 19, 25]
blist = [30, 40, 45]
all = linklist(alist, debug)
bll = linklist(blist, debug)
print("LIST A:".format(tno))
all.print()
print("LIST B:".format(tno))
bll.print()
all.merge(bll)
print("MERGED:".format(tno))
all.print()
tno += 1
debug = False
alist = [50, 55, 56]
blist = [30, 40, 45]
all = linklist(alist, debug)
bll = linklist(blist, debug)
print("LIST A:".format(tno))
all.print()
print("LIST B:".format(tno))
bll.print()
all.merge(bll)
print("MERGED:".format(tno))
all.print()