顺序表的建立、数据元素的插入和删除、数据元素

顺序表数据元素的插入、删除、查找和遍历等操作

题目要求:实现以下操作在这里插入图片描述
IList接口:

public interface IList {
    public void clear();
    public boolean isEmpty();
    public int length();
    public Object get(int i) throws Exception;
    public void insert(int i,Object x) throws Exception;
    public void remove(int i) throws Exception;
    public int indexOf(Object x);public void display();

}

SqList类:
以下是顺序表的置空,判空,获取长度,获得指定位置的方法:

/**
* @author 小仙女
* @create 2020-05-22 16:57
*/
public class SqList implements IList {

private  Object[]  listElem;
   private int curLen;
   private Object temp;
   //构造一个容量为maxSize的空顺序表函数
   public SqList (int maxSize) {
       listElem = new Object[maxSize];
       curLen = 0;
   }

   @Override
   public void clear() {
       curLen = 0;
   }

   @Override
   public boolean isEmpty() {
       return curLen==0;
   }

   @Override
   public int length() {
       return curLen;
   }

   @Override
   public Object get(int i) throws Exception{
       if(i<0||i>curLen-1)
           throw new Exception("第"+i+"个元素不存在");
       return listElem[i];
   }

插入元素方法:

 @Override
    public void insert(int i, Object x) throws Exception {
        if (curLen == listElem.length)
            throw new Exception("顺序表已满");
        if (i < 0 || i > curLen)   // i不合法
            throw new Exception("插入位置不合法");
        for (int j = curLen; j > i; j--)     //后移
            listElem[j] = listElem[j - 1];
        listElem[i] = x;    //插入
        curLen++;    //表长加1
    }

插入元素思路分析:
在这里插入图片描述
删除元素方法:

 @Override
    public void remove(int i) throws Exception {
        if (i < 0 || i > curLen - 1)
            throw new Exception("删除位置不合法");
        for (int j = i; j < curLen - 1; j++)     //前移
            listElem[j] = listElem[j + 1];
        curLen--;     //表长减1
    }

删除元素思路分析:
在这里插入图片描述
以下是顺序表的查找指定元素,显示顺序表,就地逆置的方法:


    @Override
    public int indexOf(Object x) {
        int j = 0;
        while (j < curLen && !listElem[j].equals(x))
            j++;
        if (j < curLen)
            return  j;
        else
            return -1;
    }

    @Override
    public void display() {
        for(int j=0;j<curLen;j++)
            System.out.print(listElem[j] + " ");
        System.out.println();
    }

    public void reverse() {
        for(int i=0,j=curLen-1;i<j;i++,j--)
        {
            temp=listElem[i];
            listElem[i]=listElem[j];
            listElem[j]=temp;
        }
    }
}

测试类:

import java.util.Scanner;
/**
 * @author 小仙女
 * @create 2020-05-23 12:55
 */
public class SqListTest {
        public static void main(String[] args) throws Exception {
            // 第一步:初始化顺序表,表长为5
            SqList L = new SqList(8); // 构造一个8个存储空间的顺序表
            Scanner sc = new Scanner(System.in);
            int item;
            for (int i = 0; i < 5; i++) {
                System.out.print("顺序表第" + i + "个元素是:");
                item = sc.nextInt();
                L.insert(i, item);// 输入的5个值依次插入表中
            }

            System.out.println("顺序表初始化完成!");
            L.display();
            L.reverse();
            L.display();

            // 第二步:显示操作菜单1
            menu();
            // 第三步:循环选择操作菜单,直到输入操作代码为0结束程序
            int op;
            do {
                System.out.print("请输入操作代码(0-退出):");
                op = sc.nextInt();
                switch (op) {
                    case 1:
                        System.out.println("顺序表的长度:" + L.length());// 输出顺序表的长度
                        break;
                    case 2:
                        System.out.println("请输入要插入的位置:");
                        // 位置是从0开始的
                        int loc = sc.nextInt();
                        System.out.println("请输入要插入该位置的值:");
                        int num = sc.nextInt();
                        L.insert(loc, num);
                        System.out.println("插入操作成功!");
                        break;
                    case 3:
                        System.out.print("请输入要删除元素的位置:");
                        loc = sc.nextInt();
                        L.remove(loc);
                        System.out.println("删除操作成功");
                        break;
                    case 4:
                        System.out.print("请输入要查找的元素:");
                        num = sc.nextInt();
                        System.out.println(num + "在表中的位置:" + (L.indexOf(num)));
                        break;
                    case 5:
                        System.out.print("请输入要查找元素的位置:");
                        loc = sc.nextInt();
                        System.out.println(loc + "位置上的元素为:" + L.get(loc));
                        break;
                    case 6:
                        L.display();
                        break;
                    case 0:
                        System.out.print("程序结束!");
                        return;
                    default:
                        System.out.print("输入操作代码有误,请重新选择!");
                }
            } while (op != 0);
            sc.close();

        }

        // 显示操作菜单方法
        public static void menu() {
            System.out.println("-----------------------------");
            System.out.println("操作选项菜单");
            System.out.println("1.输出表长");
            System.out.println("2.插入元素");
            System.out.println("3.删除元素");
            System.out.println("4.定位元素");
            System.out.println("5.取表元素");
            System.out.println("6.显示线性表");
            System.out.println("0.退出");
            System.out.println("-----------------------------");
        }

    }

代码测试:
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值