单链表

链表的构成

在这里插入图片描述

头部添加元素

在这里插入图片描述

# -*- coding: UTF-8 -*-
class SingleNode:
    """单链表的节点"""

    def __init__(self, val):
        self.val = val
        self.next = None


class SingleLinkList(object):
    """单向链表"""

    def __init__(self):
        """单向列表的初始化方法"""
        self.__head = None

    def add(self, val):
        """在链表头部添加元素"""
        node = SingleNode(val)
        node.next = self.__head
        self.__head = node

    def is_empty(self):
        """判断列表是否为空"""
        return self.__head == None

    def length(self):
        cur = self.__head
        count = 0
        while cur != None:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        cur = self.__head
        while cur != None:
            val = cur.val
            print(val)
            cur = cur.next
        print('')

    def append(self, val):
        """尾部添加元素"""
        node = SingleNode(val)
        if self.is_empty():
            self.__head = node
        else:
            cur = self.__head
            while cur.next != None:
                cur = cur.next
            cur.next = node

    def insert(self, index, val):
        """指定位置插入元素"""
        if index <= 0:
            self.add(val)
        elif index > self.length() - 1:
            self.append(val)
        else:
            node = SingleNode(val)
            pre = self.__head
            count = 0
            while count < (index - 1):
                count += 1
                pre = pre.next
            node.next = pre.next
            pre.next = node

    def remove(self, val):
        """删除指定节点"""
        # cur = self.__head
        # pre = None
        # while cur != None:
        #     # 找到删除的元素
        #     if cur.val == val:
        #         # 如果第一个节点是删除的节点
        #         if not pre:
        #             self.__head = cur.next
        #         else:
        #             pre.next = cur.next
        #         break
        #     # 继续按照链表后移节点
        #     else:
        #         pre =  cur
        #         cur = cur.next
        cur = self.__head
        pre = None
        while cur.val != val:
            if cur.next == None:
                print("没有这个元素")
                break
            else:
                pre = cur
                cur = cur.next
        if not pre:
            self.__head = cur.next
        else:
            pre.next = cur.next

    def find_by_value(self, value):
        cur = self.__head
        while (cur is not None) and (cur.val != value):
            cur = cur.next
        return cur


if __name__ == '__main__':
    ll = SingleLinkList()
    ll.add(2)
    ll.add(1)
    ll.append(3)
    ll.insert(1, 9)
    # print(ll.length())
    ll.travel()
    ll.remove(9)
    ll.travel()
    a = ll.find_by_value(1)
    print(a.val)
    ll.is_empty()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值