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

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

时间:2024-10-12 22:21:37浏览次数:16  
标签: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

相关文章

  • 机器学习主成分分析算法 PCA—python详细代码解析(sklearn)
    一、问题背景在进行数据分析时,我们常常会遇到这样的情况:各个特征变量之间存在较多的信息重叠,也就是相关性比较强。就好比在研究一个班级学生的学习情况时,可能会收集到学生的语文成绩、数学成绩、英语成绩等多个特征变量。但往往会发现,语文成绩好的学生,数学和英语成绩也可能比......
  • Python字符串格式
    文章目录1.数字与ASCII码转换2.字符串输出格式(%)2.1数字进制与小数表示2.2字符串长度与对齐方式3.字符串输出格式(f'')4.字符串输出格式(format)5.转义字符(换行、Tab)6.字符串查找统计替换等1.数字与ASCII码转换将ASCII码转化为数字或者将数字转化为ASCII码时,......
  • 八、Python基础语法(判断语句-下)
    一、ifelifelse结构应用场景:多个判断条件下,并且这些判断条件存在一定的关联。语法:elif也是python中关键字,后面跟一个判断条件,判断条件后面跟冒号存在冒号,需要换行缩进,处于elif缩进中的代码,是eilf代码块。if和多个elif之间,只要有一个条件成立,后续条件不再执行。需......
  • 【python 简易入门应用教程】第二部分:数据处理与分析
    第二部分:数据处理与分析1.Numpy基础Numpy(NumericalPython)是一个强大的Python库,专为科学计算而设计。它提供了高效的多维数组对象,以及丰富的函数库来操作这些数组。Numpy是数据分析和机器学习项目的基础模块之一,相比于纯Python,其处理大规模数据的效率更高。1.1Numpy......