python 链表
链表是一系列数据元素,通过链接连接在一起。每个数据元素都以指针的形式包含到另一个数据元素的连接。python在其标准库中没有链接列表。我们使用前一章讨论过的节点概念来实现链表的概念。我们已经看到了我们如何创建节点类以及如何遍历节点的元素。在本章中,我们将研究被称为单链表的链表的类型。在这种类型的数据结构中,任何两个数据元素之间只有一个链接。我们创建这样一个列表并创建其他方法来插入,更新和从列表中移除元素。
创建链接列表
链表通过使用我们在上一章中研究的节点类创建。我们创建一个node对象并创建另一个类来使用这个ode对象。我们通过节点对象传递适当的值来指向下一个数据元素。下面的程序使用三个数据元素创建链接列表。在下一节中,我们将看到如何遍历链表。
class node:
def __init__(self, dataval=none):
self.dataval = dataval
self.nextval = none
class slinkedlist:
def __init__(self):
self.headval = none
list1 = slinkedlist()
list1.headval = node("mon")
e2 = node("tue")
e3 = node("wed")
# link first node to second node
list1.headval.nextval = e2
# link second node to third node
e2.nextval = e3
遍历链接列表
从第一个数据元素开始,单向链表只能在forwrad方向上遍历。我们只需通过将下一个节点的指针指向当前数据元素来打印下一个数据元素的值。
class node:
def __init__(self, dataval=none):
self.dataval = dataval
self.nextval = none
class slinkedlist:
def __init__(self):
self.headval = none
def listprint(self):
printval = self.headval
while printval is not none:
print (printval.dataval)
printval = printval.nextval
list = slinkedlist()
list.headval = node("mon")
e2 = node("tue")
e3 = node("wed")
# link first node to second node
list.headval.nextval = e2
# link second node to third node
e2.nextval = e3
list.listprint()
当上面的代码被执行时,它会产生以下结果:
mon tue wed
插入链接列表中
在链表中插入元素涉及将指针从现有节点重新分配给新插入的节点。取决于新数据元素是在链表的开始位置还是在中间位置或末尾插入,我们有以下方案。
在链接列表的开头插入
这涉及到将新数据节点的下一个指针指向链表的当前头部。因此,链表的当前头成为第二个数据元素,新节点成为链表的头部。
class node:
def __init__(self, dataval=none):
self.dataval = dataval
self.nextval = none
class slinkedlist:
def __init__(self):
self.headval = none
# print the linked list
def listprint(self):
printval = self.headval
while printval is not none:
print (printval.dataval)
printval = printval.nextval
def atbegining(self,newdata):
newnode = node(newdata)
# update the new nodes next val to existing node
newnode.nextval = self.headval
self.headval = newnode
list = slinkedlist()
list.headval = node("mon")
e2 = node("tue")
e3 = node("wed")
list.headval.nextval = e2
e2.nextval = e3
list.atbegining("sun")
list.listprint()
当上面的代码被执行时,它会产生以下结果:
sun mon tue wed
在链接列表的末尾插入
这包括将链表的当前最后一个节点的下一个指针指向新的数据节点。因此链表的当前最后一个节点成为倒数第二个数据节点,新节点成为链表的最后一个节点。
class node:
def __init__(self, dataval=none):
self.dataval = dataval
self.nextval = none
class slinkedlist:
def __init__(self):
self.headval = none
# function to add newnode
def atend(self, newdata):
newnode = node(newdata)
if self.headval is none:
self.headval = newnode
return
laste = self.headval
while(laste.nextval):
laste = laste.nextval
laste.nextval=newnode
# print the linked list
def listprint(self):
printval = self.headval
while printval is not none:
print (printval.dataval)
printval = printval.nextval
list = slinkedlist()
list.headval = node("mon")
e2 = node("tue")
e3 = node("wed")
list.headval.nextval = e2
e2.nextval = e3
list.atend("thu")
list.listprint()
当上面的代码被执行时,它会产生以下结果:
mon tue wed thu
插入两个数据节点之间
这涉及到将指定节点的指针指向新节点。这可以通过传入新节点和现有节点,然后插入新节点。所以我们定义一个额外的类,将新节点的下一个指针改变为中间节点的下一个指针。然后将新节点分配给中间节点的下一个指针。
class node:
def __init__(self, dataval=none):
self.dataval = dataval
self.nextval = none
class slinkedlist:
def __init__(self):
self.headval = none
# function to add node
def inbetween(self,middle_node,newdata):
if middle_node is none:
print("the mentioned node is absent")
return
newnode = node(newdata)
newnode.nextval = middle_node.nextval
middle_node.nextval = newnode
# print the linked list
def listprint(self):
printval = self.headval
while printval is not none:
print (printval.dataval)
printval = printval.nextval
list = slinkedlist()
list.headval = node("mon")
e2 = node("tue")
e3 = node("thu")
list.headval.nextval = e2
e2.nextval = e3
list.inbetween(list.headval.nextval,"fri")
list.listprint()
当上面的代码被执行时,它会产生以下结果:
mon tue fri thu
从喜好列表中删除项目
我们可以使用该节点的密钥删除现有节点。在下面的程序中,我们找到要删除的节点的前一个节点。然后将该节点的下一个指针指向要删除的节点的下一个节点。
class node:
def __init__(self, data=none):
self.data = data
self.next = none
class slinkedlist:
def __init__(self):
self.head = none
def atbegining(self, data_in):
newnode = node(data_in)
newnode.next = self.head
self.head = newnode
# function to remove node
def removenode(self, removekey):
headval = self.head
if (headval is not none):
if (headval.data == removekey):
self.head = headval.next
headval = none
return
while (headval is not none):
if headval.data == removekey:
break
prev = headval
headval = headval.next
if (headval == none):
return
prev.next = headval.next
headval = none
def llistprint(self):
printval = self.head
while (printval):
print(printval.data),
printval = printval.next
llist = slinkedlist()
llist.atbegining("mon")
llist.atbegining("tue")
llist.atbegining("wed")
llist.atbegining("thu")
llist.removenode("tue")
llist.llistprint()
当上面的代码被执行时,它会产生以下结果:
thu wed mon


