一. 链表
# 链表节点
class Node:
def __init__(self, dataVal=None):
self.dataVal = dataVal
self.next = None
# 开始节点
class SLinkedList:
def __init__(self):
self.next = None
# 打印链表
def printLink(self):
pNode = self.next
while pNode is not None:
print(pNode.dataVal)
pNode = pNode.next
# 添加节点(尾部添加)
def addNode(self, newData):
newNode = Node(newData)
pNode = self.next
while pNode is not None:
if pNode.next is None:
pNode.next = newNode
break
pNode = pNode.next
# 修改节点
def updateNode(self, oldVal, newVal):
pNode = self.next
while pNode is not None:
if pNode.dataVal == oldVal:
pNode.dataVal = newVal
break
pNode = pNode.next
# 删除节点
def delNode(self, data):
pNode = self
while pNode.next is not None:
if pNode.next.dataVal == data:
pNode.next = pNode.next.next
break
pNode = pNode.next
temp = SLinkedList()
e1 = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")
temp.next = e1
e1.next = e2
e2.next = e3
temp.addNode("Sun")
temp.delNode('Sun')
temp.updateNode('Wed', 'jfbvjdjfv')
temp.printLink()
标签:None,temp,self,next,pNode,dataVal,数据结构
From: https://www.cnblogs.com/gjjcode/p/17623119.html