导读 大家好,本篇文章主要讲的是python中的单向链表实现,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
一、单向链表概念

单向链表的链接方向是单向的,由结点构成,head指针指向第一个成为head结点,而终止于最后一个指向None的指针,对链表的访问要通过顺序读取从头部开始。

二、建立节点对象
class Node:
    def __init__(self,data):
        self.data = data #节点的值域
        self.next = None #连接下一个节点,暂时指向空
三、链表对象的初始定义
class linkList:
    def __init__(self):
        self.head = None #首先建立链表头,暂时指向空
四、判断链表是否为空
#判断链表是否为空
def isEmpty(self):
    if self.head:
        return False
    else:
        return True
五、获取链表长度
def length(self):
    if self.isEmpty():
        return 0
    else:
        t = self.head
        n = 1
        while t.next:
            t = t.next
            n = n + 1
        return n
六、向头部添加节点
def addhead(self,data):
    node = Node(data) #新建一个节点
    node.next = self.head #新建的节点接上原来的链表
    self.head = node #重置链表的头
七、向尾部添加节点
def addtail(self,data):
    node = Node(data) #新建一个节点
    #先判断链表是否为空
    if self.isEmpty():
        self.addhead(data)
    else:
        t = self.head 
        while t.next: #通过循环找到尾部
            t = t.next
        t.next = node #尾部接上
八、指定位置插入节点
def insert(self,data,index):
    if index == 0 or self.isEmpty():
        self.addhead(data)
    elif index >= self.length():
        self.addtail(data)
    else:
        node = Node(data)
        t = self.head
        n = 1
        while n < index - 1:
            t = t.next
            n = n + 1
        a = t.next.next
        t.next = node
        node.next = a
九、删除指定位置的节点
def delete(self,index):
    if self.isEmpty():
        print("The linked list is empty")
    else:
        t = self.head
        if index == 0:
            self.head = t.next
        elif index == self.length() - 1:
            n = 1
            while n < self.length() - 1:
                t = t.next
                n = n + 1
            t.next = None
        elif index > self.length() - 1:
            print("Out of range")
        elif index < 0:
            print("Wrong operation")
        else:
            n = 1
            while n < index - 1:
                t = t.next
                n = n + 1
            a = t.next.next
            t.next = a
十、查找是否有该数据的节点
def search(self,data):
    t = self.head
    n = 1
    while t.next:
        if t.data == data:
            print(str(n) + " ")
        t = t.next
        n = n + 1
    if (t.data == data):
        print(str(n) + " ")
十一、遍历输出整个链表
def form(self,datalist):
    self.addhead(datalist[0])
    for i in range(1,len(datalist)):
        self.addtail(datalist[i])
    t = self.head
    while t.next:
        print(t.data)
        t = t.next
    print(t.data)
十二、输入数据创建链表
def form(self,datalist):
    self.addhead(datalist[0])
    for i in range(1,len(datalist)):
        self.addtail(datalist[i])
    t = self.head
    while t.next:
        print(t.data)
        t = t.next
    print(t.data)
十三、具体实现
data = input("input(以空格为界):")
data = data.split(" ")
linkList = linkList()
linkList.form(data) #创建链表
addlist = linkList.addhead(5) #在头节点加入
linkList.ergodic() #遍历输出
addlist = linkList.addtail(5) #在尾节点加入
linkList.ergodic() #遍历输出
linkList.search(5) #查找是否有"5"的节点
linkList.delete(4) #删除第4个数据
linkList.ergodic() #遍历输出
print(linkList.length()) #输出链表长度
linkList.insert(89,2) #指定位置插入数据
linkList.ergodic() #遍历输出

到此这篇关于python中的单向链表实现的文章就介绍到这了。

原文来自:https://www.jb51.net/article/236310.htm

本文地址:https://www.linuxprobe.com/python-linux-one.html编辑:倪家兴,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/