本文是对python对链表的使用的一个总结
使用python语言创建单链表相比于c来说比较简便
单链表的创建
我们定义一个Node类用于创建链表,传入item为该链表的元素
class Node(object):
"""创建单链表的结点"""
def __init__(self, item):
# item存放数据元素
self.item = item
# next是下一个节点的标识
self._next = None
计算链表长度
定义一个listLink类用于链表的操作,定义一个count变量用于计数,让cur指向头节点,当cur存在链表就一直循环,每次循环count+1,cur指向先一个链表,返回计数器
class listLink():
def __init__(self):
self._head=None
"创建链表"
def length(self):
count=0
cur=self._head
while cur:
count+=1
cur=cur._next
return count
单链表的插入
尾部插入
先定义一个cur指向头节点。首先判断头节点是否为空,不为空则指向创建的头节点,当cur.next不为空时,循环让链表指向下一个链表,这就实现了尾插入
def init_list(self,item):
# 虚拟头结点
head = Node(item)
cur = self._head
if not cur:
self._head=head
else:
while cur._next:
cur=cur._next
cur._next=head
头部插入
定义一个insert函数,传入参数,postition代表插入方式,item为元素,首先调用Node类创建一个需要插入的链表,让插入的链表的next指向头,让头指向插入的链表
def insert(self,postion,item):
"头插入"
if postion<=0:
node=Node(item)
node._next=self._head
self._head=node
中间插入(指定位置插入)
这里同样时在insert类中编写的,这里定义一个计时器用于判断位置(插入方法时在指定的位置的前一个位置插入链表,所以要postiton-1),循环查找链表位置,建立需要插入的链表,让这个链表的next指向查找到的链表的next,让查找到链表next指向插入链表
count=0
cur=self._head
while count<postion-1:
count+=1
cur=cur._next
node=Node(item)
node._next=cur._next
cur._next=node
完整代码
def insert(self,postion,item):
"头插入"
if postion<=0:
node=Node(item)
node._next=self._head
self._head=node
# 尾插入
elif postion>=self.length():
self.init_list(item)
else:
count=0
cur=self._head
while count<postion-1:
count+=1
cur=cur._next
node=Node(item)
node._next=cur._next
cur._next=node
链表的指向要严格按照顺序来,一个链表可以被多个链表指向,但是一个链表不能指向多个链表,插入链表时被插入链表需要先连接再断开
链表的删除
链表的删除就是把需要删除的链表断开就可以实现
def delete(self,item):
head=self._head
pre=None
while head!=None:
if head.item==item:
if not pre:
self._head=head._next
else:
pre._next=head._next
break
else:
pre=head
head=head._next
链表元素打印
def print_linked_list(self):
cur = self._head
while cur:
print(cur.item, end=" ")
cur = cur._next
print()
完整代码
class Node(object):
"""创建单链表的结点"""
def __init__(self, item):
# item存放数据元素
self.item = item
# next是下一个节点的标识
self._next = None
class listLink():
def __init__(self):
self._head=None
"创建链表"
def length(self):
count=0
cur=self._head
while cur:
count+=1
cur=cur._next
return count
def init_list(self,item):
# 虚拟头结点
head = Node(item)
cur = self._head
if not cur:
self._head=head
else:
while cur._next:
cur=cur._next
cur._next=head
def insert(self,postion,item):
"头插入"
if postion<=0:
node=Node(item)
node._next=self._head
self._head=node
# 尾插入
elif postion>=self.length():
self.init_list(item)
else:
count=0
cur=self._head
while count<postion-1:
count+=1
cur=cur._next
node=Node(item)
node._next=cur._next
cur._next=node
def delete(self,item):
head=self._head
pre=None
while head!=None:
if head.item==item:
if not pre:
self._head=head._next
else:
pre._next=head._next
break
else:
pre=head
head=head._next
"打印链表"
def print_linked_list(self):
cur = self._head
while cur:
print(cur.item, end=" ")
cur = cur._next
print()
if __name__ == '__main__':
l = [1, 2, 3, 4, 5]
list=listLink()
for i in range(len(l)):
list.init_list(l[i])
list.print_linked_list()#打印元素
list.insert(0,12)#头插入元素
list.print_linked_list()
# list.insert(list.length()+1,44)#尾插入元素
# list.print_linked_list()
#指定位置插入元素
list.insert(4,11)
list.insert(3,45)
list.print_linked_list()
#指定位置删除元素
list.delete(1)
list.print_linked_list()
出现问题
在对链表进行增加元素操作时无论是首部、中间和尾部增加元素都要注意到指针的指向问题,需要特别注意指向顺序问题,先指向再断开链表,因为一个链表可以被多个链表指向,但是一个链表不能被多个链表指向,关于这方面我建议可以手动画几个链表,用来分析指向