self.next = next
self.data = data
class LianBiao(object):def __init__(self):self.root =
None# 给单链表添加元素节点def addNode(self, data):ifself.root==
Noneself.root = Node(data=data, next=
:
None)
returnself.root
else:
# 有头结点,则需要遍历到尾部节点,进行链表增加操作cursor = self.root
whilecursor.next!=
Nonecursor.next = Node(data=data, next=
cursor = cursor.next
:
None)
returnself.root
# 在链表的尾部添加新节点,底层调用addNode方法即可def append(self, value):self.addNode(data=value)
# 在链表首部添加节点def prepend(self, value):ifself.root ==
Noneself.root = Node(value,
:
None)
elsenewroot = Node(value,
:
None)
# 更新root索引self.root = newroot
newroot.next = self.root
# 在链表的指定位置添加节点def insert(self, index, value):ifself.root ==
None:
returnifindex<=
0orprint(
index >self.size():
'index %d 非法, 应该审视一下您的插入节点在整个链表的位置!')
returnelifindex==
1:
# 如果index==1, 则在链表首部添加即可self.prepend(value)
elifindex == self.size()+
1:
# 如果index正好比当前链表长度大一,则添加在尾部即可self.append(value)
else:
# 如此,在链表中部添加新节点,直接进行添加即可。需要使用计数器来维护插入未知counter =
2cursor = self.root.next
pre = self.root
whilecursor!=
None:
iftemp = Node(value,
counter == index:
Nonetemp.next = cursor
pre.next = temp
)
breakelsecounter +=
:
1cursor = cursor.next
pre = cursor
# 删除指定位置上的节点def delNode(self, index):ifself.root ==
None:
returnifindex<=
0orindex > self.size():
return# 对第一个位置需要小心处理ifindex ==
1self.root = self.root.next
:
elsecounter =
cursor = pre.next
pre = self.root
:
2whilecursor!=
None:
ifprint(
index == counter:
'can be here!'pre.next = cursor.next
)
breakelsecounter +=
cursor = cursor.next
pre = cursor
:
1# 删除值为value的链表节点元素def delValue(self, value):ifself.root ==
None:
return# 对第一个位置需要小心处理ifself.root = self.root.next
self.root.data == value:
elsecursor = pre.next
pre = self.root
:
whilecursor!=
None:
ifpre.next = cursor.next
cursor.data == value:
# 千万记得更新这个节点,否则会出现死循环。。。cursor = cursor.next
continueelsecursor = cursor.next
pre = cursor
:
# 判断链表是否为空def isempty(self):ifself.root ==
Noneorself.size()==
0:
returnTrueelse:
returnFalse# 删除链表及其内部所有元素def truncate(self):ifself.root ==
Noneorself.size()==
0:
returnelsecursor = self.root
:
whilecursor!=
Nonecursor.data =
:
Noneself.root =
cursor = cursor.next
Nonecursor =
None# 获取指定位置的节点的值def getvalue(self, index):ifself.root
isNoneorself.size()==
0print(
:
'当前链表为空!')
returnNoneifindex<=
0orprint(
index>self.size():
"index %d不合法!"%index)
returnNoneelsecounter =
:
1cursor = self.root
whilecursor
isnotNone:
ifindex == counter:
returncursor.data
elsecounter +=
:
1cursor = cursor.next
# 获取链表尾部的值,且不删除该尾部节点def peek(self):returnself.getvalue(self.size())
# 获取链表尾部节点的值,并删除该尾部节点def pop(self):ifself.root
isNoneorself.size()==
0print(
:
'当前链表已经为空!')
returnNoneelifself.size()==
1self.root =
top = self.root.data
:
Nonereturntop
elsecursor = pre.next
pre = self.root
:
whilecursor.next
isnotNonecursor =
top = cursor.data
cursor = cursor.next
pre = cursor
:
Nonepre.next =
Nonereturntop
# 单链表逆序实现def reverse(self):ifself.root
isNone:
returnifself.size()==
1:
returnelse:
# post = Nonepre =
Nonecursor = self.root
whilecursor
isnotNone:
# print('逆序操作逆序操作')cursor = post
pre = cursor
cursor.next = pre
post = cursor.next
# 千万不要忘记了把逆序后的头结点赋值给root,否则无法正确显示self.root = pre
# 删除链表中的重复元素def delDuplecate(self):# 使用一个map来存放即可,类似于变形的“桶排序”dic = {}
ifself.root ==
None:
returnifself.size() ==
1:
returndic = {}
cursor = pre.next
pre = self.root
# 为字典赋值temp = self.root
whiletemp!=
Nonedic[str(temp.data)] =
:
0temp =
temp = temp.next
None# 开始实施删除重复元素的操作whilecursor!=
None:
ifdic[str(cursor.data)] ==
1cursor = cursor.next
pre.next = cursor.next
:
elsedic[str(cursor.data)] +=
:
1cursor = cursor.next
pre = cursor
# 修改指定位置节点的值def updateNode(self, index, value):ifself.root ==
None:
returnifindex<
0orindex>self.size():
returnifindex ==
1self.root.data = value
:
returnelsecounter =
cursor = self.root.next
:
2whilecursor!=
None:
ifcursor.data = value
counter == index:
breakcounter +=
cursor = cursor.next
1# 获取单链表的大小def size(self):counter =
0ifself.root ==
None:
returncounter
elsecursor = self.root
:
whilecursor!=
Nonecounter +=
:
1cursor = cursor.next
returncounter
# 打印链表自身元素def print(self):if(self.root==
None):
returnelsecursor = self.root
:
whilecursor!=
Noneprint(cursor.data, end=
:
'\t'print()
cursor = cursor.next
)
if__name__ ==
'__main__':
# 创建一个链表对象lianbiao = LianBiao()
# 判断当前链表是否为空print(
"链表为空%d"%lianbiao.isempty())
# 判断当前链表是否为空lianbiao.addNode(
1print(
)
"链表为空%d"%lianbiao.isempty())
# 添加一些节点,方便操作lianbiao.addNode(
2lianbiao.addNode(
)
3lianbiao.addNode(
)
4lianbiao.addNode(
)
6lianbiao.addNode(
)
5lianbiao.addNode(
)
6lianbiao.addNode(
)
7lianbiao.addNode(
)
3)
# 打印当前链表所有值print(
'打印当前链表所有值'lianbiao.print()
)
# 测试对链表求size的操作print(
"链表的size: "+str(lianbiao.size()))
# 测试指定位置节点值的获取print(
'测试指定位置节点值的获取'print(lianbiao.getvalue(
)
1print(lianbiao.getvalue(
print(lianbiao.getvalue(lianbiao.size()))
))
7))
# 测试删除链表中指定值, 可重复性删除print(
'测试删除链表中指定值, 可重复性删除'lianbiao.delNode(
)
4lianbiao.delValue(
lianbiao.print()
)
3lianbiao.print()
)
# 去除链表中的重复元素print(
'去除链表中的重复元素'lianbiao.print()
lianbiao.delDuplecate()
)
# 指定位置的链表元素的更新测试print(
'指定位置的链表元素的更新测试'lianbiao.updateNode(
)
6,
99lianbiao.print()
)
# 测试在链表首部添加节点print(
'测试在链表首部添加节点'lianbiao.prepend(
)
77lianbiao.prepend(
)
108lianbiao.print()
)
# 测试在链表尾部添加节点print(
'测试在链表尾部添加节点'lianbiao.append(
)
99lianbiao.append(
)
100lianbiao.print()
)
# 测试指定下标的插入操作print(
'测试指定下标的插入操作'lianbiao.insert(
)
1,
10010lianbiao.insert(
)
3,
333lianbiao.insert(lianbiao.size(),
)
99999lianbiao.print()
)
# 测试peek 操作print(
'测试peek 操作'lianbiao.print()
print(lianbiao.peek())
)
# 测试pop 操作print(
'测试pop 操作'lianbiao.print()
print(lianbiao.pop())
)
# 测试单链表的逆序输出print(
'测试单链表的逆序输出'lianbiao.print()
lianbiao.reverse()
)
# 测试链表的truncate操作print(
'测试链表的truncate操作'lianbiao.print()
lianbiao.truncate()
)
来源: http://blog.csdn.net/marksinoberg/article/details/69310033