【Python 实现顺序表(线性表):从设计到实现的完整指南】

文章目录

    • Python 实现顺序表(线性表):从设计到实现的完整指南
    • 引言
    • 一、顺序表的设计与抽象接口定义
    • 二、顺序表的具体实现(lineList 类)
    • 1.初始化方法
    • 2.判空操作:is_empty()
    • 3.获取长度:get_length()
    • 4.插入操作:insert(pos, item)
    • 5.删除操作:remove(pos)
    • 6.查找操作:get_first_index(item)
    • 7.遍历操作:travel()
    • 单元测试结果:
    • 小结

Python 实现顺序表(线性表):从设计到实现的完整指南

引言

数据结构中,顺序表(Sequential List) 是一种最基础、最常用的线性结构。它通过一段连续的内存空间来存储元素,支持高效的随机访问和插入/删除操作。本文将基于 Python 实现一个完整的顺序表类,并围绕其抽象接口设计、功能实现、使用场景以及常见问题进行详细讲解。

一、顺序表的设计与抽象接口定义

我们首先定义了一个抽象基类 absLineList,用于规范顺序表的基本行为:

from abc import ABC,abstractmethod
# 定义一个抽象线性表类,用于规范线性表的基本操作
class absLineList(ABC):
    @abstractmethod
    def is_empty(self):
        """
        检查线性表是否为空
        """
        pass

    @abstractmethod
    def get_length(self):
        """
        获取线性表的长度
        """
        pass

    @abstractmethod
    def insert(self, pos, item):
        """
        在指定位置插入一个元素
        参数:
        pos - 插入位置
        item - 要插入的元素
        """
        pass

    @abstractmethod
    def remove(self, pos):
        """
        移除指定位置的元素
        参数:
        pos - 移除位置
        """
        pass

    @abstractmethod
    def get_first_index(self, item):
        """
        获取指定元素首次出现的位置
        参数:
        item - 要查找的元素
        """
        pass

    @abstractmethod
    def travel(self):
        """
        遍历线性表
        """
        pass


方法名 功能描述
is_empty() 判断线性表是否为空
get_length() 获取当前长度
insert(pos, item) 在指定位置插入元素
remove(pos) 删除指定位置的元素
get_first_index(item) 查找元素首次出现的位置
travel() 遍历并打印所有元素

二、顺序表的具体实现(lineList 类)

我们基于上述接口实现具体的顺序表类 lineList

1.初始化方法

def __init__(self, max_size):
    self.cur_length = 0
    self.max_size = max_size
    self.my_linelist = [None] * max_size

  • cur_length: 当前实际元素个数
  • max_size: 最大容量
  • my_linelist: 存储数据的数组(固定大小)

2.判空操作:is_empty()

def is_empty(self):
    return self.cur_length == 0
  • 判断线性表是否为空。

3.获取长度:get_length()

def get_length(self):
    return self.cur_length
  • 返回当前线性表的实际长度。

4.插入操作:insert(pos, item)

def insert(self, pos, item):
    if pos < 0 or pos > self.cur_length:
        print('插入位置非法')
    elif pos >= self.max_size:
        print('顺序表已满')
    else:
        for i in range(self.cur_length, pos - 1, -1):
            self.my_linelist[i] = self.my_linelist[i - 1]
        self.my_linelist[pos] = item
        self.cur_length += 1

注意事项:

  • 插入位置必须合法(0 ≤ pos ≤ cur_length)
  • 插入不能超出最大容量(pos < max_size)
  • 插入时需要将插入点之后的元素整体后移一位

5.删除操作:remove(pos)

def remove(self, pos):
    if pos < 0 or pos > self.cur_length - 1:
        return
    else:
        for i in range(pos, self.cur_length - 1):
            self.my_linelist[i] = self.my_linelist[i + 1]
        self.cur_length -= 1
  • 删除位置必须合法(0 ≤ pos < cur_length)
  • 删除后需将后续元素前移一位以填补空位

6.查找操作:get_first_index(item)

def get_first_index(self, item):
    for i in range(self.cur_length):
        if self.my_linelist[i] == item:
            return i
    return False
  • 查找某个元素首次出现的位置索引。

7.遍历操作:travel()

def travel(self):
    for i in range(self.cur_length):
        print(self.my_linelist[i], end=' ')
  • 遍历整个线性表并输出所有元素。

单元测试结果:

【Python 实现顺序表(线性表):从设计到实现的完整指南】_第1张图片

小结

顺序表是理解线性结构的基础,掌握其实现原理对深入学习链表、栈、队列等高级结构至关重要。希望本篇文章能帮助你全面理解顺序表的设计与实现,并能在实际项目中灵活运用。如果你喜欢这篇文章,请点赞、收藏或分享给更多人!如需进一步扩展,欢迎留言讨论 !

你可能感兴趣的:(python数据结构,python,windows,开发语言,数据结构)