顺序表【使用数组实现单链表】

 顺序表的特点:

1.中间或前面部分的插入删除时间复杂度O(N)

2.增容的代价比较大

链表的特点:

1.任意位置插入删除时间复杂度为O(1)

2.没有增容问题,插入一个开辟一个空间。


顺序表就是一个数组,如下代码就是使用数组实现一个单链表。

import java.util.Arrays;
public class MyArrayList {
    public int[] elem;//数组
    public int usedSize;//有效数据的个数
    public static final int intCapacity = 10;//初始容量
    //构造方法
    public MyArrayList(){
        this.elem = new int[intCapacity];
        this.usedSize = 0;
    }
    //打印顺序表
    public void display(){
        for(int i = 0; i < this.usedSize;i++){
            System.out.print(this.elem[i]+" ");
        }

    }
    private void checkPos(int pos){
        if(pos < 0 || pos > this.usedSize){
            throw new RuntimeException("pos位置不合法!");
        }
    }
    private boolean isFull(){
//        if(this.usedSize == this.elem.length){
//            return true;
//        }
//        return false;
        return this.usedSize == this.elem.length;
    }

    // 在 pos 位置新增元素
public void add(int pos, int data) {
    if(isFull()){
        //如果满了  数组扩容 原来的销毁 
        this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
    }
        //在pos位置新增元素
//        if(pos < 0 || pos > this.usedSize){
//            System.out.println("Error");
//            return;
//        }
    checkPos(pos);
        //挪数据
    for(int i = this.usedSize-1; i >= pos; i--){
        this.elem[i+1] = this.elem[i];
    }
    this.elem[pos] = data;
    this.usedSize++;
}
// 判定是否包含某个元素
    public boolean contains(int toFind) {
        for(int i = 0; i < this.usedSize; i++){
            if(this.elem[i] == toFind){
                return true;
            }
        }
         return false;
    }
// 查找某个元素对应的位置
     public int search(int toFind) {
        for(int i = 0; i < this.usedSize; i++){
            if(this.elem[i] == toFind){
                return i;
            }
        }
         return -1;
    }
// 获取 pos 位置的元素
// 1.顺序表是否为空,为空返回-1
// 2.pos合法性   最后return pos位置的数据就ok
    private boolean isEmpty(){
        return this.usedSize == 0;
    }

  public int getPos(int pos) {
      //1.顺序表是否为空,为空返回-1
//      if(isEmpty()){
//            return -1;
//        }

      //2.pos合法性
        if(pos < 0 || pos >= this.usedSize){
            return -1;
        }
//        for(int i = 0; i < this.usedSize; i++){
//            if(i == pos){
//                return this.elem[i];
//            }
//        }
//        return -1;
      return this.elem[pos];
    }

//删除第一次出现的关键字key
   public void remove(int toRemove) {

        int index = search(toRemove);
        if(index == -1){
            System.out.println("没有要删除的数据");
            return;//不能省略  不然会顺序在执行
        }

        for(int i =index; i < this.usedSize-1; i++){
            this.elem[i] = this.elem[i+1];

        }
        this.usedSize--;
   }
// 获取顺序表长度
 public int size() {

       return this.usedSize;
    }
    // 给 pos 位置的元素设为 value

    public void setPos(int pos,int value){
        if(pos < 0 || pos >= this.usedSize){
            throw new IndexOutOfBoundsException("pos位置不合法!!!");
        }
        this.elem[pos] = value;
    }
    //清空数据表
    public void clear(){
        this.usedSize = 0;
    }

    public static void main(String[] args) {
    MyArrayList myArrayList = new MyArrayList();
    for(int i = 0; i < 10; i++){
        myArrayList.add(i,i);

    }
             myArrayList.display();
            System.out.println();
             myArrayList.add(10,25);
            myArrayList.display();
        System.out.println();
        System.out.println("=========================");
        System.out.println(myArrayList.search(5));
        System.out.println(myArrayList.contains(5));
        myArrayList.remove(38);
        myArrayList.display();
//        System.out.println("clear()");
//        myArrayList.clear();
//        myArrayList.display();
        myArrayList.setPos(10,6);
        myArrayList.display();

        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题,我来给出答案。 假设我们已知一个稀疏矩阵 A,其中有 m 行,n 列,且其中非零元素个数为 k,那么可以使用三元组表示法来表示这个矩阵。 三元组顺序表是指按照行优先顺序,将所有非零元素的行、列、值存储在一个一维数组中,每个非零元素对应数组中的一个元素。具体来说,可以定义一个结构体 Triplet,其中包含三个成员变量 i、j、value,分别表示非零元素的行、列、值。然后,可以定义一个一维数组 triplets,长度为 k,用来存储所有非零元素的三元组信息。 三元组单链表是指将所有非零元素的三元组信息存储在一个单链表中。具体来说,可以定义一个结构体 TripleNode,其中包含三个成员变量 i、j、value,分别表示非零元素的行、列、值。然后,可以定义一个指向 TripleNode 的指针 head,用来表示单链表的头节点。 下面是 Python 代码实现: ``` # 定义三元组结构体 class Triplet: def __init__(self, i, j, value): self.i = i self.j = j self.value = value # 定义稀疏矩阵 A m = 3 n = 4 k = 5 A = [[0, 0, 3, 0], [0, 0, 0, 0], [0, 0, 0, 7]] # 将 A 转换为三元组顺序表 triplets = [None] * k index = 0 for i in range(m): for j in range(n): if A[i][j] != 0: triplets[index] = Triplet(i, j, A[i][j]) index += 1 # 输出三元组顺序表 for i in range(k): print("({0}, {1}, {2})".format(triplets[i].i, triplets[i].j, triplets[i].value)) # 将 A 转换为三元组单链表 head = None for i in range(m): for j in range(n): if A[i][j] != 0: node = TripleNode(i, j, A[i][j]) node.next = head head = node # 输出三元组单链表 node = head while node: print("({0}, {1}, {2})".format(node.i, node.j, node.value)) node = node.next ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值