日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python中的單向鏈表實現_python

作者:tt丫 ? 更新時間: 2022-04-10 編程語言

一、單向鏈表概念

單向鏈表的鏈接方向是單向的,由結點構成,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() #遍歷輸出  

原文鏈接:https://blog.csdn.net/weixin_55073640/article/details/122736503

欄目分類
最近更新