单向链表

Posted 月色真美丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单向链表相关的知识,希望对你有一定的参考价值。

  单向链表的方向是单向的,其节点是由一个数据域和一个指针域组成,每个指针域都指向下一个节点,最后一个节点的指针域为None

  

 代码实现:

# -*- coding = utf-8 -*-
# @Author: Wchime
# @time: 2023/1/20 12:02
# @file: 单向链表.py


class Node(object):
    """
    定义节点
    """
    def __init__(self, item):
        """
        节点初始化为存的值和下一个节点的地址
        :param item:
        """
        self.item = item
        self.next = None


class SingleLinkList(object):
    """
    单向链表
    """
    def __init__(self, node=None):
        """
        初始化链表,没有节点,为None
        :param node:
        """
        self.__link = node

    def is_empty(self):
        """
        判断链表是否为空
        :return:
        """
        return self.__link is None

    def get_length(self):
        """
        获取链表长度
        :return:
        """
        cur = self.__link
        count = 0
        while cur != None:
            count += 1
            cur = cur.next
        return count

    def append(self, item):
        """
        添加节点, 当链表不是空链表时,需要找到最后一个节点
        :param item:
        :return:
        """
        node = Node(item)
        if self.__link is None:
            self.__link = node
        else:
            cur = self.__link
            while cur.next is not None:
                cur = cur.next
            cur.next = node

    def insert(self, index, item):
        """
        在指定位置插入节点,插入在第n个时,需要将第m-n+1个元素向后移动位置
        :param index: 位置下标
        :param item: 插入值
        :return:
        """
        if index == 0:
            node = Node(item)
            node.next = self.__link
            self.__link = node
        elif index > self.get_length()-1:
            self.append(item)
        else:
            node = Node(item)
            pre = self.__link
            cur = 0
            while cur < index-1:
                cur += 1
                pre = pre.next
            node.next = pre.next
            pre.next = node

    def remove(self, item):
        """
        根据值删除节点
        :param item:
        :return:
        """
        cur = self.__link
        pre = None
        while cur is not None:
            if cur.item == item:
                if cur == self.__link:
                    self.__link = cur.next
                else:
                    pre.next = cur.next
                break
            else:
                pre = cur
                cur = cur.next

    def search(self, item):
        """
        查找节点是否存在
        :param item:
        :return:
        """
        cur = self.__link
        while cur is not None:
            if cur.item == item:
                return True
            else:
                cur = cur.next
        return False

    def travel(self):
        """
        链表遍历
        :return:
        """
        cur = self.__link
        while cur is not None:
            print(cur.item, end="  ")
            cur = cur.next
        print("")


if __name__ == "__main__":

    singlelist = SingleLinkList()
    singlelist.append(1)
    singlelist.append(7)
    singlelist.append(5)
    singlelist.travel()

    singlelist.insert(1, 66)
    singlelist.travel()

    length = singlelist.get_length()
    print(length)

    print(singlelist.search(66))

    singlelist.remove(66)
    singlelist.travel()

    print(singlelist.is_empty())

 

  

  

以上是关于单向链表的主要内容,如果未能解决你的问题,请参考以下文章

链表——带表头的单向链表

单向链表

单向链表反转

Java连载83-单向链表双向链表collections常用方法

4.单向链表的常见题型

链表的java实现(单向双向链表,单向链表的反转)