数据结构-java-顺序表的各种操作-2

使用Java方法来实现顺序表的各种操作

  1. 定义一顺序表类型,并定义顺序表。
  2. 将顺序表的建立、初始化、插入、删除等方法实现。
  3. 由主函数按照用户要求对各个顺序表操作访问。
  4. 每次操作之前要有明确的说明,操作后要输出操作结果。
  5. 分析顺序表的插入、删除、查找的时间复杂度。

定义顺序表接口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();

}

定义顺序表类源代码:


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() {
		// TODO Auto-generated method stub
		curLen = 0; 
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return curLen==0;
	}

	@Override
	public int length() {
		// TODO Auto-generated method stub
		return curLen;
	}

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

	@Override
	public void insert(int i, Object x) throws Exception {
		// TODO Auto-generated method stub
		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 {
		// TODO Auto-generated method stub
		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) {
		// TODO Auto-generated method stub
		int j = 0;
		while (j < curLen && !listElem[j].equals(x))
			j++;
		if (j < curLen)
		       return  j;  
		 else
		       return -1; 
	}

	@Override
	public void display() {
		// TODO Auto-generated method stub
		for(int j=0;j<curLen;j++)
			System.out.print(listElem[j] + "");
		System.out.println();
	}
	
	public void reverse() {
		// TODO Auto-generated method stub
		for(int i=0,j=curLen-1;i<j;i++,j--)
		{
			temp=listElem[i];
			listElem[i]=listElem[j];
			listElem[j]=temp;
		}
	}
}

定义测试类(包括显示菜单方法和main方法):

import java.util.Scanner;

public class Shiyan2 {
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		// 第一步:初始化顺序表,表长为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("-----------------------------");
			System.out.println("作者:XXX         班级:18软件工程X班");
		}		
		

		private int length() {
			// TODO Auto-generated method stub
			return 0;
		}


	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值