Java实现顺序表



一、什么是顺序表?

顺序表是指用一组地址连续的存储单元依次存储各个元素,使得在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中的线性表。

在Java中,可以借助数组来表示一组地址连续的存储单元,通过对数组进行操作来表示对顺序表进行操作。我们可以将顺序表定义成一个类,将顺序表的基本操作定义成类的方法,初始化顺序表就是将这个类实例化成对象,销毁顺序表就是销毁对象。

一个标准的顺序表需要实现以下基本操作:

  1. 初始化顺序表

  2. 清空顺序表

  3. 检测顺序表是否为空

  4. 返回顺序表的元素个数

  5. 返回顺序表中指定位置元素的值

  6. 返回顺序表中第一个与指定值相同的元素的位置

  7. 向指定位置插入元素

  8. 删除指定位置的元素

  9. 遍历顺序表



二、顺序表的实现

1、接口的定义:

interface Ilist{		//线性表的抽象数据Java接口
	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();
}

2、创建顺序表:

class SqList implements Ilist{
	private Object[] listElem;	//线性表存储空间
	private int len;			//线性表当前长度
	
	//构造一个存储空间容量为maxSize的线性表
	public SqList(int maxSize) {
		len = 0;
		listElem = new Object[maxSize];
	}
}

3、清空顺序表:

public void clear() {
	len = 0;
}

4、判断顺序表是否为空:

public boolean isEmpty() {
	return len == 0;
}

5、顺序表的元素个数:

public int length() {
	return len;
}

6、返回指定位置的值:

//查找线性表第i个元素并返回其值
public Object get(int i) throws Exception{
	if( i<0 || i> len - 1)
		throw new Exception("第" + i + "个元素不存在");
	return listElem[i];
}

7、插入元素:

//在线性表第i个元素前加入一个值为x的元素
public void insert (int i, Object x) throws Exception  {
	if(len == listElem.length)
		throw new Exception("顺序表已满");
	if(i < 0 || i > len)
		throw new Exception("插入位置不合法");
	for(int j = len; j > i; j--)
		listElem[j] = listElem[j - 1];
	listElem[i] = x;
	len++;
}

8、删除指定位置元素:

//构造删除第i个元素的函数
public void remove(int i) throws Exception {
	if(i < 0 || i > len - 1)
		throw new Exception("删除位置不合法");
	for(int j = i; j < len - 1; j++)
		listElem[j] = listElem[j + 1];
	len--;
}

9、返回初次出现指定元素的位置:

//构造返回线性表中初次出现值为x的元素的位置,若不存在则返回-1
public int indexOf(Object x) {	
	int j = 0;
	while(j < len && !listElem[j].toString().equals(x.toString()))
		j++;
	if(j < len)
		return j;
	else
		return -1;
}

10、遍历输出顺序表:

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


附上全部代码:

import java.util.Scanner;

interface Ilist{		//线性表的抽象数据Java接口
	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();
}
class SqList implements Ilist{
	private Object[] listElem;	//线性表存储空间
	private int len;			//线性表当前长度
	
	//构造一个存储空间容量为maxSize的线性表
	public SqList(int maxSize) {
		len = 0;
		listElem = new Object[maxSize];
	}
	
	//清空函数
	public void clear() {
		len = 0;
	}
	
	//判断是否为空函数
	public boolean isEmpty() {
		return len == 0;
	}
	
	//线性表实际元素个数
	public int length() {
		return len;
	}
	
	//查找线性表第i个元素并返回其值
	public Object get(int i) throws Exception{
		if( i<0 || i> len - 1)
			throw new Exception("第" + i + "个元素不存在");
		return listElem[i];
	}
	
	//在线性表第i个元素前加入一个值为x的元素
	public void insert (int i, Object x) throws Exception  {
		
		if(len == listElem.length)
			throw new Exception("顺序表已满");
		if(i < 0 || i > len)
			throw new Exception("插入位置不合法");
		for(int j = len; j > i; j--)
			listElem[j] = listElem[j - 1];
		listElem[i] = x;
		len++;
	}
	
	//构造删除第i个元素的函数
	public void remove(int i) throws Exception {
		
		if(i < 0 || i > len - 1)
			throw new Exception("删除位置不合法");
		for(int j = i; j < len - 1; j++)
			listElem[j] = listElem[j + 1];
		len--;
	}
	
	//构造返回线性表中初次出现值为x的元素的位置,若不存在则返回-1
	public int indexOf(Object x) {
		
		int j = 0;
		while(j < len && !listElem[j].toString().equals(x.toString()))
			j++;
		if(j < len)
			return j;
		else
			return -1;
	}
	
	//输出线性表中的元素
	public void display() {
		for(int j = 0; j < len; j++)
			System.out.print(listElem[j] + " ");
		System.out.println();
	}
	
}
public class Task_1_1 {
	public static void main(String[] args) throws Exception {
		Scanner in = new Scanner(System.in);
		System.out.print("输入顺序表长度n:");
		int n = in.nextInt(); 
		//例如 输入'8'
		
		SqList L = new SqList(n);	//构造一个空顺序表
		for(int i=0; i<n/2; i++) 	//存入前n/2个元素
			L.insert(i, i+1);
		System.out.print("输出顺序表中个元素的值:");
		L.display();
		//输出:1 2 3 4
		
		L.insert(2, 3);		//在2位置前插入值为'3'的元素
		System.out.print("输出插入元素'3'后的顺序表:");
		L.display();
		//输出:1 2 3 3 4
		
		L.remove(4);		//删掉值为'4'的元素
		System.out.print("输出删除元素'4'后的顺序表:");
		L.display();
		//输出:1 2 3 3 
		
		System.out.print("输入要查找的值:");
		int pos = L.indexOf(in.nextInt());		//输入一个值,找到其位置
		if(pos!=-1)
			System.out.println("顺序表中初次出现值为x的元素的位置为:" + pos);
		else
			System.out.println("此顺序表中不包含值为'5'的数据元素!");
		//输入:5
		//输出:此顺序表中不包含值为'5'的数据元素!
		
		for(int i=0; i<L.length()-1; i++){ 		//删除掉相同元素
			 for(int j=L.length()-1; j>i; j--){ 
			      if( L.get(j).toString().equals(L.get(i).toString()) ){ 
			        L.remove(j); 
			      } 
			 } 
		} 
		System.out.print("输出删除相同元素后的顺序表:");
		L.display();
		//输出:1 2 3
	}
}

运行结果:
在这里插入图片描述

  • 12
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值