博客
关于我
线型表的顺序存储结构----顺序表(学习笔记)
阅读量:489 次
发布时间:2019-03-07

本文共 2725 字,大约阅读时间需要 9 分钟。

首先,我们定义了一条长度为80的链表,该链表的元素编号从1到80。接下来,我们实现了链表的增、删、清空功能。以下是各操作的实现细节:

插入算法

  • 判断插入位置是否合理。如果位置超出链表长度或为负数,提示插入位置不对。
  • 向插入位置后面的所有元素向后移一位。
  • 将目标元素插入到指定位置。
  • 链表长度增加1。
  • 删除算法

  • 判断删除位置是否合理。若位置超出链表长度或为负数,提示位置不对。
  • 向删除位置后面的所有元素向前移一位。
  • 删除指定位置的元素。
  • 链表长度减1。
  • 查找算法

  • 使用for循环和equals方法逐一比较目标值和链表中每个元素的值。
  • 如果找到目标值,返回其索引值。
  • 下面是实现代码:

    public interface ListIntf {    public int size(); // 获取链表长度    public void clear(); // 清空链表    public boolean isEmpty(); // 判断是否为空表    public Object get(int i); // 获取i位置的元素    public int indexOf(Object obj); // 获取元素的索引位置    public Object getPre(Object obj); // 获取前驱元素    public Object getNext(Object obj); // 获取后驱元素    public void insertElementAt(Object obj, int i); // 插入元素    public Object remove(int i); // 移除元素    public Object remove(Object obj); // 移除元素}

    实现上述接口的具体类为Sqlist:

    public class Sqlist implements ListIntf {    final int maxlen = 100;    int len = 80;    int num[] = new int[maxlen];    public Sqlist() {        for (int i = 0; i < len; i++) {            num[i] = 0;        }    }    public int size() {        return len;    }    public void clear() {        for (int i = 0; i < num.length; i++) {            num[i] = 0;        }    }    public boolean isEmpty() {        int index = this.indexOf(null);        return index == -1;    }    public Object get(int i) {        return num[i - 1];    }    public int indexOf(Object obj) {        for (int i = 0; i < num.length; i++) {            if (obj.equals(num[i])) {                return i;            }        }        return -1;    }    public Object getPre(Object obj) {        int index = this.indexOf(obj);        if (index == 0) {            return null;        } else {            return num[index - 1];        }    }    public Object getNext(Object obj) {        int index = this.indexOf(obj);        if (index == num.length - 1) {            return null;        } else {            return num[index + 1];        }    }    public void insertElementAt(Object obj, int i) {        if (i < 1 || i > len) {            System.out.println("插入位置不对");        } else {            for (int j = len; j > i; j--) {                num[j + 1] = num[j];            }            num[i] = (int) obj;            len++;        }    }    public Object remove(int i) {        if (i < 1 || i > len) {            System.out.println("删除位置不对");            return null;        } else {            for (int j = i - 1; j >= 0; j--) {                num[j] = num[j + 1];            }            num[i - 1] = 0;            len--;            return num[i];        }    }}

    运行结果示例:

    • 索引16位置前驱元素为15
    • 索引16位置后驱元素为17
    • 索引16位置元素为17
    • 插入索引80的81后,链表为12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535454555657585960616263646566676869707172737475767778798081

    转载地址:http://uhwcz.baihongyu.com/

    你可能感兴趣的文章
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>