Python -----单链表学习(习题八个方法)
1、单链表这里耽误了些时间,网上我看都是定义了head以后写的,而作者zed则是用了头节点和尾节点的方法,并且只给了三个方法,其余的5个方法我目前已经补充完成,并且更改了其中的pop方法。
2、第一次我就没怎么理解单链表,想着还要用什么指针之类的东西,list不都可以做么,也实现了后来看了zed的测试需求;见脚本sllist.py,大家不要见笑。
3、看了zed的视频,也是一头雾水,没看明白,最后是我找了网上一些视频学习了一下,然后开始写的,最重要的是利用pycharm的断点调试,不然真的能搞糊涂,见脚本sllist_org.py
4、测试脚本见test_sllist.py,其中test_pop中增加了打印链表长度的测试。
test_sllist.py脚本如下:
from sllist_org import *
# from sllist import *
def test_push():
colors = SingleLinkedList()
colors.push("Pthalo Blue")
assert colors.count() == 1
colors.push("Ultramarine Blue")
assert colors.count() == 2
def test_pop():
colors = SingleLinkedList()
colors.push("Magenta")
colors.push("Alizarin")
colors.push("yue")
assert colors.count() == 3
#
assert colors.pop() == "yue"
assert colors.count() == 2
assert colors.pop() == "Alizarin"
assert colors.count() == 1
assert colors.pop() == "Magenta"
assert colors.count() == 0
assert colors.pop() == None
def test_unshift():
colors = SingleLinkedList()
colors.push("Viridian")
colors.push("Sap Green")
colors.push("Van Dyke")
assert colors.unshift() == "Viridian"
assert colors.unshift() == "Sap Green"
assert colors.unshift() == "Van Dyke"
assert colors.unshift() == None
def test_shift():
colors = SingleLinkedList()
colors.shift("Cadmium Orange")
assert colors.count() == 1
colors.shift("Carbazole Violet")
assert colors.count() == 2
# colors.shift("yue")
assert colors.pop() == "Cadmium Orange"
assert colors.count() == 1
assert colors.pop() == "Carbazole Violet"
# assert colors.pop() == "yue"
assert colors.count() == 0
def test_remove():
colors = SingleLinkedList()
colors.push("Cobalt")
colors.push("Zinc White")
colors.push("Nickle Yellow")
colors.push("Perinone")
assert colors.remove("Cobalt") == 0
colors.dump("before perinone.")
assert colors.remove("Perinone") == 2
colors.dump("after perinone.")
assert colors.remove("Nickle Yellow") == 1
assert colors.remove("Zinc White") == 0
def test_first():
colors = SingleLinkedList()
colors.push("Cadmium Red Light")
assert colors.first() == "Cadmium Red Light"
colors.push("Hansa Yellow")
assert colors.first() == "Cadmium Red Light"
colors.shift("Pthalo Green")
assert colors.first() == "Pthalo Green"
def test_last():
colors = SingleLinkedList()
colors.push("Cadmium Red Light")
assert colors.last() == "Cadmium Red Light"
colors.push("Hansa Yellow")
assert colors.last() == "Hansa Yellow"
colors.shift("Pthalo Green")
assert colors.last() == "Hansa Yellow"
def test_get():
colors = SingleLinkedList()
colors.push("Vermillion")
assert colors.get(0) == "Vermillion"
colors.push("Sap Green")
assert colors.get(0) == "Vermillion"
assert colors.get(1) == "Sap Green"
colors.push("Cadmium Yellow Light")
assert colors.get(0) == "Vermillion"
assert colors.get(1) == "Sap Green"
assert colors.get(2) == "Cadmium Yellow Light"
assert colors.pop() == "Cadmium Yellow Light"
assert colors.get(0) == "Vermillion"
assert colors.get(1) == "Sap Green"
assert colors.get(2) == None
colors.pop()
assert colors.get(0) == "Vermillion"
colors.pop()
assert colors.get(0) == None
test_push()
test_pop()
test_unshift()
test_shift()
test_remove()
test_first()
test_last()
test_get()
sllist_org.py如下:
原作者只给出了3个方法,我把其余的方法给补全了,
class SingleLinkedListNode(object):
def __init__(self, value, nxt):
self.value = value
self.next = nxt
def __repr__(self):
nval = self.next and self.next.value or None
return f"[{self.value}: {repr(nval)}]"
class SingleLinkedList(object):
def __init__(self):
self.begin = None
self.end = None
def push(self, obj):
node = SingleLinkedListNode(obj, None)
if self.begin == None:
self.begin = node
self.end = node
# elif self.begin == self.end:
# self.begin.next = node
# self.end = node
else:
self.end.next = node
self.end = node
assert self.end.next == None
def unshift(self):
# if self.end == None:
# return None
# elif self.end == self.begin:
# return self.begin.value
try:
node = self.begin
self.begin = node.next
return node.value
except Exception as e:
return None
def shift(self, obj):
node = SingleLinkedListNode(obj, None)
if self.begin == None:
self.begin = node
self.end = node
else:
node.next = self.begin
self.begin = node
def count(self):
"""Counts the number of elements in the list."""
count = 0
node = self.begin
while node:
count += 1
node = node.next
return count
def pop(self):
if self.end == None:
return None
elif self.end == self.begin:
node = self.begin
self.end = self.begin = None
return node.value
else:
node = self.begin
while node.next != self.end:
node = node.next
# node.next = None
count_num = node.next.value
self.end = node
self.end.next = None
return count_num
def remove(self, obj):
node = self.begin
count = 0
while node:
if self.begin.value == obj:
node = node.next
self.begin = node
return count
# break
elif node.value == obj:
self.before.next = node.next
return count
# break
else:
self.before = node
node = node.next
count += 1
# pass
def first(self):
node = self.begin
return node.value
def last(self):
node = self.begin
while node.next != None:
node = node.next
return node.value
def dump(self, obj):
print(obj)
def get(self, index):
node = self.begin
if index == 0:
try:
return node.value
except:
return None
else:
try :
for i in range(index):
node = node.next
return node.value
except:
return None
sllist.py如下:
class SingleLinkedList(object):
def __init__(self):
self.obj_list = []
def push(self, obj):
self.obj_list.append(obj)
def pop(self):
try:
return self.obj_list.pop()
except IndexError:
return None
def unshift(self):
try:
return self.obj_list.pop(0)
except IndexError:
return None
def shift(self, obj):
self.obj_list.insert(0, obj)
def remove(self, obj):
index_num = self.obj_list.index(obj)
self.obj_list.remove(obj)
return index_num
def first(self):
return self.obj_list[0]
def last(self):
return self.obj_list[-1]
def get(self, index):
try:
return self.obj_list[index]
except IndexError:
return None
def count(self):
return len(self.obj_list)
def dump(self, mark):
print(mark)
说明
1、作者链接:https://learncodethehardway.org/more-python-book/ex13.html
作者github:https://github.com/zedshaw/learn-more-python-the-hard-way-solutions 可以看一下作者的13课的code。