数据结构复习一 单链表(python实现)

前言

复习一下数据结构,感觉生疏了,用python重新写一遍吧

正文

单链表的实现

这里由于python变量的特殊性,python变量相当于维护的是一个地址,不像其他语言的的变量一样要事先定义自身的类型,这就使得python实现单链表比较方便,自我感觉。。。。比如在C语言里面设定了int类型的变量,你只能够往里面存放整形数据,而且是直接对应的数据,但是python的变量类型直接是一个地址,就例如 a = 20, 其实这里的a存放的是20这个数的地址, 通过a就可以找到20这个数字了。。这也解释了为什么Python里面可以这样去把两个数调换,比如

a =20 
b = 30 
a, b = b , a

下面是单链表实现代码(python 3.5)

# -*- coding: utf-8 -*-
# Author:0verWatch

class Node(object):
    def __init__(self, elem):
        self.elem = elem
        self.next = None



class SingleLinklist(object):
    def __init__(self, node=None):
        self.__head = node  #这里用了__来实现一定程度的私有化,因为用户一般不需要知道这个参数的存在


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


    def length(self): #获取长度
        cur = self.__head #设置游标,类似于C里面的指针
        count = 0 #最好设置为0,不然的话下面的条件可能也看情况变化,个人感觉这样好理解
        while cur is not None:
            count += 1
            cur = cur.next
        return count



    def travel(self): #遍历一遍链表,这个跟获取长度就差不多
        cur = self.__head
        while cur is not None:
            print(cur.elem,end=" ")
            cur = cur.next


    def add(self,item): #头插法
        node = Node(item)
        node.next = self.__head
        self.__head = node



    def append(self,item): #尾插法
        node = Node(item)
        if self.is_empty():
            self.__head = node
        else:
            cur = self.__head
            while cur.next is not None:
                cur = cur.next
            cur.next = node



    def insert(self, pos, item ): #在某个位置插入数字
        if pos < 0:
            self.add(item)  # 位置小于0的时候默认头插法
        elif pos> self.length()-1:
            self.append(item) # 位置大于长度,默认尾插法,所以不能使用等于号会误插
        else:
            pre = self.__head
            count = 0
            node = Node(item)
            while count < pos-1:
                count += 1
                pre = pre.next
            node.next = pre.next
            pre.next = node




    def remove(self, item): #默认删除一个找到的节点
        cur = self.__head
        pre = None
        while cur is not None:
            if cur.elem == item: #找到了的话
                #判断是否为头结点
                  if cur == self.__head:
                      self.__head = cur.next
                  else:
                      pre.next = cur.next
                  break
            else:
                pre = cur
                cur = cur.next


    def search(self,item): # 查找元素
        cur = self.__head
        while cur is not None:
            if cur.elem == item:
                return True
            else:
                cur = cur.next

        return False


if __name__ == '__main__':
    # test_1
    lst = SingleLinklist()
    print(lst.is_empty())
    print(lst.length())

    # test_2
    lst.append(2)
    lst.add(3) # 3 2
    lst.insert(1, 5)   # 3  1  5 2
    lst.add(4)    # 4 3  5 2
    lst.append(6)  # 4 3  5 2 6
    print(lst.search(5))
    lst.travel()


    # test_3 
    lst.remove(5)
    lst.remove(4)
    lst.remove(6)
    lst.travel()

小结

总体感觉在写单链表的时候要注意几个问题,注意要多考虑一下几种典型的情况,比如空链表,只有一个节点的时候,或者是最后一个节点的情况等等。。。第二的话还是要注意一下变量的赋值,这东西很容易写着写着脑子就抽了,然后就会出现溢出的情况额。。。


欢迎多来踩踩我的博客:https://0verwatch.top

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值