温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python中的单向链表怎么实现

发布时间:2022-02-07 15:58:03 来源:亿速云 阅读:120 作者:iii 栏目:开发技术

这篇文章主要介绍了python中的单向链表怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python中的单向链表怎么实现文章都会有所收获,下面我们一起来看看吧。

一、单向链表概念

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

python中的单向链表怎么实现

二、建立节点对象

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中的单向链表怎么实现”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“python中的单向链表怎么实现”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI