首页 > 编程语言 >数据结构与算法Python版p26-p28 无序表链表实现、有序表

数据结构与算法Python版p26-p28 无序表链表实现、有序表

时间:2024-10-12 22:21:37浏览次数:7  
标签:current head Python self None next 链表 p26 previous

B站视频-数据结构与算法Python版

无序表链表实现、有序表

一、节点

# 节点
class Node:
    def __init__(self,initdata):
        self.data = initdata
        self.next = None
    def getData(self):
        return self.data
    def getNext(self):
        return self.next
    def setData(self,newdata):
        self.data = newdata
    def setNext(self,newnext):
        self.next = newnext

二、无序表

在这里插入图片描述

# 无序表
class UnorderedList:
    def __init__(self):
        self.head = None
    def add(self,items):
        # [a,b,c]c先插入表
        for i in range(len(items)):
            temp = Node(items[len(items)-1-i])
            temp.setNext(self.head)
            self.head = temp
    def size(self):
        k = 0
        current = self.head
        while current != None:
            current = current.next
            k += 1
    def search(self,item):
        current = self.head
        while current != None:
            if current.data == item:
                return True
            else:
                return False
            current = current.next
    def show_list(self):
        current = self.head
        result = []
        while current != None:
            result.append(current.data)
            current = current.next
        print(result)
    def remove(self,item):
        current = self.head
        while current.data == item:
            self.head = current.next
            current = self.head
        previous = self.head
        current = previous.next
        while current != None:
            if current.data == item:
                previous.next = current.next
                current = previous.next
            else:
                previous = current
                current = current.next
    def isEmpty(self):
        return self.head == None
    def size(self):
        current = self.head
        k = 0
        while current != None:
            current = current.next
            k += 1
        return k
    def append(self,item):
        temp = Node(item)
        current = self.head
        if current.data == None:
            current.setNext(temp)
        else:
            while True:
                if current.next == None:
                    current.setNext(temp)
                    break
                current = current.next
                
    def index(self,item):
        current = self.head
        k = 0
        result = []
        if self.size == 0:
            return None
        else:
            while current != None:
                if current.data == item:
                    result.append(k)
                current = current.next
                k += 1
            return result
    def insert(self,pos,item):
        temp = Node(item)
        if pos == 0:
            temp.setNext(self.head)
            self.head = temp
        elif pos >= self.size() + 1:
            print(f'表长:{self.size()},pos取值范围0-{self.size()}')
        elif pos == self.size:
            current = self.head
            while True:
                if current.next == None:
                    current.setNext(temp)
                    break
                current = current.next
        else:
            k = 1
            previous = self.head
            current = previous.next
            while True:
                if k == pos:
                    temp.setNext(current)
                    previous.setNext(temp)
                    break
                previous = current
                current = current.next
                k += 1
    def pop(self,pos=None):
        if pos == None:
            pos = self.size() - 1
        if self.size() == 0:
            print('长度为0')
        elif pos >= self.size():
            print(f'pos取值范围0-{self.size() - 1}')
        elif  pos == 0:
            self.head = self.head.next
        else:
            k = 1
            previous = self.head
            current = previous.next
            while True:
                if k == pos:
                    previous.setNext(current.next)
                    break
                previous = previous.next
                current = current.next
                k += 1

三、有序表

在这里插入图片描述
在这里插入图片描述

  • show_list,isEmpty,size,remove,pop方法同无序表的
# 有序表
class UnorderedList:
    def __init__(self):
        self.head = None
    def show_list(self):
        current = self.head
        result = []
        while current != None:
            result.append(current.data)
            current = current.next
        print(result)
    def isEmpty(self):
        return self.head == None
    def size(self):
        current = self.head
        k = 0
        while current != None:
            current = current.next
            k += 1
        return k
    def remove(self,item):
        current = self.head
        while current.data == item:
            self.head = current.next
            current = self.head
        previous = self.head
        current = previous.next
        while current != None:
            if current.data == item:
                previous.next = current.next
                current = previous.next
            else:
                previous = current
                current = current.next
    def pop(self,pos=None):
        if pos == None:
            pos = self.size() - 1
        if self.size() == 0:
            print('长度为0')
        elif pos >= self.size():
            print(f'pos取值范围0-{self.size() - 1}')
        elif  pos == 0:
            self.head = self.head.next
        else:
            k = 1
            previous = self.head
            current = previous.next
            while True:
                if k == pos:
                    previous.setNext(current.next)
                    break
                previous = previous.next
                current = current.next
                k += 1
    def add(self,item):
        temp = Node(item)
        previous = None
        current = self.head
        while current != None:
            if current.data > item:
                break
            previous = current
            current = current.next
        if previous == None:
            temp.setNext(self.head)
            self.head = temp
        else:
            temp.setNext(current)
            previous.setNext(temp)
    def search(self,item):
        current = self.head
        found = False
        stop = False
        while current != None and not found and not stop:
            if current.data == item:
                found = True
            else:
                if current.data > item:
                    stop = True
                else:
                    current = current.next
        return found
    def index(self,item):
        stop = False
        result = []
        current = self.head
        k = 0
        while current != None and not stop:
            if current.data == item:
                result.append(k)
                current = current.next
                k += 1
            else:
                if current.data > item:
                    stop = True
                else:
                    current = current.next
                    k += 1
        if result == []:
            print('不存在')
        else:
            print(result)

在这里插入图片描述

标签:current,head,Python,self,None,next,链表,p26,previous
From: https://blog.csdn.net/m0_65199047/article/details/142849600

相关文章

  • 以Python方式理解Mixin
    通过Python理解Mixin概念-腾讯云开发者社区-腾讯云(tencent.com)Mixin的概念Mixin即Mix-in,常被译为“混入”,是一种编程模式,在Python等面向对象语言中,通常它是实现了某种功能单元的类,用于被其他子类继承,将功能组合到子类中。利用Python的多重继承,子类可以继承不同功......
  • 第108天:免杀对抗-Python&混淆算法&反序列化&打包生成器&Py2exe&Nuitka
    知识点#知识点:1、Python-对执行代码做文章2、Python-对shellcode做文章3、Python-对代码打包器做文章#章节点:编译代码面-ShellCode-混淆编译代码面-编辑执行器-编写编译代码面-分离加载器-编写程序文件面-特征码定位-修改程序文件面-加壳花指令-资源代码加载面-Dll反......
  • 机器学习主成分分析算法 PCA—python详细代码解析(sklearn)
    一、问题背景在进行数据分析时,我们常常会遇到这样的情况:各个特征变量之间存在较多的信息重叠,也就是相关性比较强。就好比在研究一个班级学生的学习情况时,可能会收集到学生的语文成绩、数学成绩、英语成绩等多个特征变量。但往往会发现,语文成绩好的学生,数学和英语成绩也可能比......
  • Python字符串格式
    文章目录1.数字与ASCII码转换2.字符串输出格式(%)2.1数字进制与小数表示2.2字符串长度与对齐方式3.字符串输出格式(f'')4.字符串输出格式(format)5.转义字符(换行、Tab)6.字符串查找统计替换等1.数字与ASCII码转换将ASCII码转化为数字或者将数字转化为ASCII码时,......
  • 【零基础学Python】项目实战:使用Django框架搭建私人博客第(二)天 —— 注册、登录功能实
    一、前言通过上次的学习我们已经顺利的搭建了一个私人博客框架,接下来我们开始写页面。本章节代码打包上传,资源下载链接:注意:本系列文章旨在使小白在尽可能短的时间内利用Django搭建一个较为完整的项目,因此页面功能将会非常简单,美工==几乎没有,见谅。二、创建应用1.创建accou......
  • 21年计算机挑战赛Python组区域赛题目及个人解答(重置版)
    1.乌鸦喝水代码部分#计算机挑战赛--乌鸦喝水x=int(input('请输入底边的边长:\n'))y=int(input('请输入容器内水面距离瓶子口的距离:\n'))z=0while1:ifx*x*y-8*z<2*x*x:print(f'需要投放{z}个石子乌鸦才能喝到水')breakelse:......
  • Python 潮流周刊#72:Python 3.13.0 最终版已发布!(摘要)
    本周刊由Python猫出品,精心筛选国内外的250+信息源,为你挑选最值得分享的文章、教程、开源项目、软件工具、播客和视频、热门话题等内容。愿景:帮助所有读者精进Python技术,并增长职业和副业的收入。分享了14篇文章,12个开源项目,4则音视频,全文2300字。以下是本期摘要:......
  • 八、Python基础语法(判断语句-下)
    一、ifelifelse结构应用场景:多个判断条件下,并且这些判断条件存在一定的关联。语法:elif也是python中关键字,后面跟一个判断条件,判断条件后面跟冒号存在冒号,需要换行缩进,处于elif缩进中的代码,是eilf代码块。if和多个elif之间,只要有一个条件成立,后续条件不再执行。需......
  • 【python 简易入门应用教程】第一部分:Python 基础
    第一部分:Python基础1.Python简介Python是一种高层次的、解释型的编程语言,由GuidovanRossum于1980年代末发明,并于1991年首次发布。它设计之初的目标是提高代码的可读性和简洁性,采用类似于英语的语法结构。Python的历史与特点历史Python的创始人Guidovan......
  • 【python 简易入门应用教程】第二部分:数据处理与分析
    第二部分:数据处理与分析1.Numpy基础Numpy(NumericalPython)是一个强大的Python库,专为科学计算而设计。它提供了高效的多维数组对象,以及丰富的函数库来操作这些数组。Numpy是数据分析和机器学习项目的基础模块之一,相比于纯Python,其处理大规模数据的效率更高。1.1Numpy......