05顺序表

线性表

  • 线性表是最基本,最简单最常用的一种数据结构,可以理解为我们高数中所学的数列,一个线性表是n个具有相同特性的数据元素组成的有限序列。
  • 前驱元素:若a元素在b元素的前面,则称a为b的前驱元素。
  • 后继元素:若a元素在b元素的后面,则称a为b的后继元素。

特征

  • 1.第一个数据没有前驱元素,称为头节点。
  • 2.末尾元素没有后继元素,称为尾节点。

分类

线性表中数据的存储方式可以是顺序存储,也可以是链式存储。按照存储方式不同分为顺序表和链表

顺序表

  • 顺序表是在计算机内存中以数组形式保存的线性表,顺序存储是指一组地址连续的存储单元,依次存储线性表中的各个元素,所以我们实现顺序表是基于数组实现的。

代码实现

  • 实现代码
public class SequenceList<T> {
    //1.存储元素的数组
    private T[] array;
    //2.当前线性表的长度
    private int n;
    //capacity n.容量
    public SequenceList(int capacity) {
        //1.初始化数组
        this.array = (T[]) new Object[capacity];
        //2.初始化长度
        this.n = 0;
    }
    //1.置空
    public void clean() {this.n = 0;}
    //2.判空
    public boolean isEmpty() {return n == 0;}
    //3.线性表的长度
    public int length() {return n;}
    //4.返回指定索引处的元素的值
    public T get(int i) {return array[i];}
    //5.在i之前插入值为t的元素
    public void insert(int i,T t) {
        //先将i索引处及其后面的元素依次向后移动一位
        for (int index = n-1; index > i ; index--) {
            array[index] = array[index-1];
        }
        //再将t放置i索引处
        array[i] = t;
    }
    //6.添加元素(末尾)
    public void insert(T t) {
        array[n++] = t;//先给索引0处赋值,然后再自增
    }
    //7.删除并返回删除的元素
    public T remove(int i) {
        //记录i处的值
        T current = array[i];
        //i后的元素依次前移
        for (int j = i; j < n-1; j++) {
            array[j] = array[j+1];
        }
        //元素个数-1
        n--;
        return current;
    }
    //8.返回首次出现元素的位置,不存在返回-1
    public int indexOf(T t) {
        for (int i = 0; i < n; i++) {
            if (array[i].equals(t)) {
                return i;
            }
        }
        return -1;
    }
}
  • 测试代码
public class SequenceListTest {
    public static void main(String[] args) {
        //create
        SequenceList list = new SequenceList(10);
        //insert
        list.insert("0");
        list.insert("0");
        list.insert("8");
        list.insert("2");
        list.insert("8");
        list.insert(0,"1999");
        //check
        System.out.println(list.get(0));
        //delete
        Object remove = list.remove(1);
        System.out.println(remove);
    }
}

容量可变

那我们呢再深入的考虑一个问题,我们在初始化顺序表的时候,是设置了它的大小的,换言之,顺序表的大小是不可变的,那我们在对其进行添加操作时,当添加满之后呢?
很显然,程序一定会报错(索引越界异常),因此在设计顺序表时,一定要考虑它的可伸缩性(扩容)。也就是说,在添加元素时,我们要进行适当的扩容,在移除元素是
需要进行适当的缩容(内存空间浪费)。

  • 代码实现
    //根据参数newSize来重置array大小
    public void controlSize(int newSize) {
        //1.create a temporary array
        T[] temp = array;
        //2.create a new array
        array = (T[])new Object[newSize];
        //3.copy
        for (int i = 0; i < n; i++) {
            array[i] = temp[i];
        }
    }
//5.在i处插入值为t的元素
public void insert(int i,T t) {
        if (n == array.length) {
        controlSize(2*array.length);
        }
        //先将i索引处及其后面的元素依次向后移动一位
        for (int index = n; index > i ; index--) {
        array[index] = array[index-1];
        }
        //再将t放置i索引处
        array[i] = t;
        //元素个数+1
        n++;
        }

时间复杂度

  • 查询某个元素的时间复杂度为O(1)。
  • 插入时,元素要进行后移(for循环),所以时间复杂度为O(n),同理删除也是。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答关于图书管理系统的问题。以下是基于顺序表的图书管理系统代码,包括初始化、取值、查找、插入和删除操作。 首先,我们需要明确一下图书的信息包括什么,比如书名、作者、出版社、出版日期等等。在本代码中,我们假定书籍信息是保存在book.txt文件中的,每行代表一本书的信息,以逗号分隔。具体实现如下: ```python # 定义一个书籍类 class Book: def __init__(self, name, author, publisher, date): self.name = name self.author = author self.publisher = publisher self.date = date # 定义一个管理系统类 class Library: # 初始化函数,读取book.txt中的信息并存储到lst列表中 def __init__(self): self.lst = [] with open('book.txt', 'r') as f: lines = f.readlines() for line in lines: data = line.strip().split(',') book = Book(data[0], data[1], data[2], data[3]) self.lst.append(book) # 取值操作,返回位于index位置的书籍对象 def get_book(self, index): return self.lst[index] # 查找操作,返回第一个符合条件的书籍对象 def find_book(self, name): for book in self.lst: if book.name == name: return book return None # 插入操作,将书籍对象插入到index位置 def insert_book(self, index, book): self.lst.insert(index, book) # 删除操作,删除位于index位置的书籍对象 def delete_book(self, index): self.lst.pop(index) # 示例代码,创建一个图书管理系统对象并进行测试 lib = Library() print(lib.get_book(0).name) # 输出第一本书籍的名称 print(lib.find_book('Python基础教程').author) # 输出搜索到的书籍的作者 new_book = Book('新书', '新作者', '新出版社', '2021-05-01') lib.insert_book(1, new_book) # 在第二本书后插入新书 print(lib.get_book(2).name) # 输出插入的新书的名称 lib.delete_book(3) # 删除第四本书 print(len(lib.lst)) # 输出当前书籍数量 ``` 希望本代码能够帮助您理解顺序表的基本操作,以及如何在 Python 中实现一个简单的图书管理系统。如果您有任何问题,请随时联系我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值