思路源于Learn more python in hard way一书的Exercise 13: Single Linked Lists一节;
效率应该比较低,就是为了练练脑而已;
测试也是最基础的测试,使用nosetests与pycharm。
功能 | 函数名 |
---|---|
节点 | __init__(self, value, nxt) |
打印节点 | __repr__(self) |
在末尾插入 | push(self, obj) |
在开始插入 | shift(self, obj) |
删除末尾一项 | pop(self) |
删除开始一项 | unshift(self) |
删除匹配项 | remove(self, obj) |
读取(不移除,下同)第一项 | first(self) |
读取最后一项 | last(self) |
读取链表长度 | count(self) |
读取第index + 1项 | get(self, index) |
打印链表 | dump(self, mark=None) |
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): """Appends a new value on the end of the list.""" def pop(self): """Removes the last item and returns it.""" def shift(self, obj): """Another name for push.""" def unshift(self): """Removes the first item and returns it.""" def remove(self, obj): """Finds a matching item and removes it from the list.""" def first(self): """Returns a *reference* to the first item, does not remove.""" def last(self): """Returns a reference to the last item, does not remove.""" def count(self): """Counts the number of elements in the list.""" def get(self, index): """Get the value at index.""" def dump(self, mark=None): """Debugging function that dumps the contents of the list."""
sllist_test.py
from .sllist import * # from ex13chao 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") assert colors.pop() == "Alizarin" assert colors.count() == 1 assert colors.pop() == "Magenta" 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 assert colors.pop() == "Carbazole Violet" assert colors.count() == 1 assert colors.pop() == "Cadmium Orange" 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
sllist.py
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):
"""Appends a new value on the end of the list."""
node = SingleLinkedListNode(obj, None)
if self.begin is None:
self.begin = node
self.end = self.begin
else:
self.end.next = node
self.end = node
# assert self.begin != self.end
# assert self.end.next == None
def pop(self):
"""Removes the last item and returns it."""
if self.end is None:
return None
elif self.begin == self.end:
probe = self.begin
self.begin = self.end = None
return probe.value
else:
probe = self.begin
while probe.next != self.end:
probe = probe.next
assert self.end != probe
self.end = probe
pop = probe.next.value
probe.next = None
return pop
def shift(self, obj):
"""Another name of push"""
self.push(obj)
def unshift(self):
"""Removes the first item and returns it."""
if self.end is None:
return None
elif self.begin == self.end:
probe = self.begin
self.begin = self.end = None
return probe.value
else:
probe = self.begin
self.begin = probe.next
return probe.value
def remove(self, obj):
"""Finds a matching item and removes it from the list."""
index = 0
probe = self.begin
if obj == probe.value:
return index
while probe.next and obj != probe.next.value:
probe = probe.next
index += 1
if probe.next:
probe.next = probe.next.next
return index
def first(self):
"""Returns a *reference* to the first item, does not remove."""
return self.begin and self.begin.value or None
def last(self):
"""Returns a reference to the last item, does not remove."""
return self.end and self.end.value or None
def count(self):
"""Counts the number of elements in the list."""
probe = self.begin
count = 0
while probe:
count += 1
probe = probe.next
return count
def get(self, index):
"""Get the value at index."""
if index < 0 or index >= self.count():
return None
else:
probe = self.begin
while index > 0 and probe.next:
probe = probe.next
index -= 1
return probe.value
def dump(self, mark=None):
"""Debugging function that dumps the contents of the list.
将(计算机内部存储器中的数据)复制到外部存储器或输出设备"""
if mark:
print(mark)
lyst = list()
probe = self.begin
while probe:
lyst.append(probe)
probe = probe.next
print(lyst)