用python实现链表_用Python实现链表
簡化了代碼,一次只添加一個元素。class Node(object):
def __init__(self, data, next=None):
self.data = data
self.next = next
def __str__(self):
return str(self.data)
class LinkedList(object):
def __init__(self):
self.head = None
self.size = 0
def append(self, data):
if not self.head:
n = Node(data)
self.head = n
return
else:
n = self.head
while n.next != None:
n = n.next
new_node = Node(data)
n.next = new_node;
return
def isEmpty(self):
return not self.head
def printList(self):
n = self.head
while n:
print str(n)
n = n.next
ll = LinkedList()
elems = [1, 2, 3, 54, 6]
for elem in elems:
ll.append(elem)
ll.printList()
輸出:>>>
1
2
3
54
6
總結
以上是生活随笔為你收集整理的用python实现链表_用Python实现链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python处理csv文件 sql_如何
- 下一篇: websocket python爬虫_p