1. 程式人生 > >資料結構之順序表的java實現

資料結構之順序表的java實現

通過查詢資料,再加上我的理解,初步建立了順序表,可以實現的功能有:查值、取值、插入、刪除。

1、建立一個介面,介面中存放你所希望的順序表所實現的功能。

public interface List1 {
	//線性表介面List給出了任何實現線性表功能的類中必須要實現的成員函式原型
	public int find(Object i) throws Exception;//查詢
	public Object getvalue(int i) throws Exception;//取值
	public void insert(int i,Object value) throws Exception;//插入
	public void delete(int i) throws Exception;//刪除
}

2、建立一個順序表類。 查值:查值是通過輸入的一個數據,通過對順序表的遍歷得到在該順序表中是否存在給定的資料(缺點是查詢大量資料時,速度較慢)。 取值:取值是通過輸入的一個在順序表中的位置,通過對順序表的遍歷得到在該位置的值(缺點是查詢大量資料時,速度較慢)。 刪除:將順序表中該位置上的資料刪除(缺點是會導致順序表有個空位,浪費空間)。 插入:插入的前提應是順序表中有位置,若沒有位置則丟擲異常。

public class SequenceList implements List1 {
	Object[] sList;
	private int maxsize;
	private int size;
	
	//初始化
	public SequenceList(Object[] obj) {
		maxsize=obj.length;
		sList=obj;
		size=maxsize;
	}
	
	//插入
	@Override//重寫(覆蓋)了一個方法,以實現不同的功能
	public void insert(int i,Object value)throws Exception {
		if(maxsize==size)
			throw new Exception("順序表已滿,無法插入資料");
		if(maxsize==0)
			throw new Exception("順序表為空");
		if(i<0||i>=maxsize)
			throw new Exception("你輸入的資料錯誤");
		Object[] middle;
		middle=sList;
		sList[i]=value;
		for(int j=i+1;j<maxsize;j++)
		{
			sList[j]=middle[j-1];
		}
		
	}
	
	//刪除
	@Override//重寫(覆蓋)了一個方法,以實現不同的功能
	public void delete(int i)  throws Exception{
		if(maxsize==0)
			throw new Exception("順序表為空");
		if(i<0||i>=maxsize)
			throw new Exception("你輸入的資料錯誤");
		for(int j=i;j<maxsize;j++)
		{
			sList[j-1]=sList[j];
		}
		size--;
	}
	
	//查值
	@Override//重寫(覆蓋)了一個方法,以實現不同的功能
	public int find(Object i) throws Exception{
		if(maxsize==0)
			throw new Exception("順序表為空");
		int value=0;//儲存查詢到的位置
		for(int j=0;j<maxsize;j++){
			if(i.equals(sList[j])){
				value=j+1;
				break;
			}
			else
				value=0;
		}
		return value;
	}

	//取值
	@Override//重寫(覆蓋)了一個方法,以實現不同的功能
	public Object getvalue(int i) throws Exception {
		if(maxsize==0)
			throw new Exception("順序表為空");
		if(i<0||i>=maxsize)
			throw new Exception("你輸入的資料錯誤");
		Object value;//儲存查詢到的值
		value=sList[i-1];
		return value;
	}	
}

3、順序表中功能的使用。

import java.util.Scanner;
//List中存放的是順序表的介面
//SequenceList中存放的是順序表類
//User1是順序表的使用
public class User1 {
	public static void main(String[] args) throws Exception{
		System.out.print("輸入表:");
		Scanner sc=new Scanner(System.in);
		String str;
		String[] st=new String[4] ;
		for(int i=0;i<4;i++) {
			str=sc.nextLine();
			st[i]=str;
		}
		sc.close();
		SequenceList a=new SequenceList(st);
		int len=a.sList.length;
		for(int i=0;i<len;i++) {
			System.out.println(i+"  "+a.sList[i]);
		}
		System.out.println();
		
		int jj;
		//查詢功能
		String b="心";
		int f=a.find(b);//存放查詢到在順序表中的位置
		if(f==0)
			System.out.println("未查詢到!");
		else
			System.out.println("在順序表中的位置:"+f);
		System.out.println();
		
		//取值功能
		jj=2;
		System.out.println("順序表的第"+jj+"位是:"+a.getvalue(jj));//輸出查詢結果
		System.out.println();
		
		//刪除功能
		jj=2;
		a.delete(jj);//刪除
		System.out.println("刪除後的順序表:");
		for(int i=0;i<len-1;i++) {
			System.out.println(i+"  "+a.sList[i]);
		}
		System.out.println();
		
		//插入功能
		jj=3;
		String value="成功了!!!";
		a.insert(jj,value);
		System.out.println("插入後的順序表:");
		for(int i=0;i<len;i++) {
			System.out.println(i+"  "+a.sList[i]);
		}		
	}
}