Python3單鏈表簡單操作


單鏈表是一種鏈式存取的數據結構,用一組地址任意的存儲單元存放線性表中的數據元素。鏈表中的數據是以結點來表示的,每個結點的構成:元素(數據元素的映象) + 指針(指示后繼元素存儲位置),元素就是存儲數據的存儲單元,指針就是連接每個結點的地址數據。

class Node():
    # 定義頭結點
    def __init__(self, data):
        self.data = data
        # 頭指針為空
        self.next = None


# 頭插法
class SNode():
    def __init__(self):
        self.current_node = None

    def add_node(self, data):
        node = Node(data)
        node.next = self.current_node
        self.current_node = node

    def append_node(self, data):
        # 尾插法插入節點
        node = Node(data)
        cur = self.current_node
        # 遍歷鏈表直到頭節點處停止遍歷
        while cur:
            if cur.next == None:
                break
            cur = cur.next
        cur.next = node

    def travel(self):
        '''
        遍歷鏈表
        :return:
        '''
        cur = self.current_node
        while cur:
            print(cur.data)
            cur = cur.next

    def is_empty(self):
        '''
        判斷鏈表非空
        :return:
        '''
        return self.current_node == None

    def get_lenth(self):
        '''
        獲取鏈表的長度
        :return:
        '''
        cur = self.current_node
        count = 0
        while cur:
            count += 1
            cur = cur.next
        return count

    def insert_node(self, index, data):
        '''
        指定位置插入節點
        :param index:
        :param data:
        :return:
        '''
        link_len = self.get_lenth()
        if index == 0:
            self.add_node(data)
        elif index >= link_len:
            self.append_node(data)
        else:
            cur = self.current_node
            for i in range(1, index):
                cur = cur.next
            node = Node(data)
            node.next = cur.next
            cur.next = node

    def del_node(self, index):
        '''
        根據索引刪除節點
        :param index:
        :return:
        '''

        # 找到前節點
        cur = self.current_node
        # 前驅節點
        pre = None
        count = 1
        len_num = self.get_lenth()
        while cur:
            if index == 1:
                self.current_node = cur.next
                break
            if count == index and count < len_num:
                pre.next = cur.next
                break
            if count >= len_num:
                pre.next = None
                break
            count += 1
            pre = cur
            cur = cur.next



if __name__ == "__main__":
    test = SNode()
    list_data = [1, 2, 3]
    for i in list_data:
        test.add_node(i)
    test.travel()
    # print(test.is_empty())
    # print (test.get_lenth())
    # test.append_node(4)
    # test.insert_node(1, 4)
    # test.travel()
    # test.del_node(4)
    # test.travel()

    '''
    單鏈表的操作
is_empty() 鏈表是否為空
length() 鏈表長度
travel() 遍歷整個鏈表
add(item) 鏈表頭部添加元素
append(item) 鏈表尾部添加元素
insert(pos, item) 指定位置添加元素
remove(item) 刪除節點
search(item) 查找節點是否存在
    '''

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM