JAVA數據結構 線性表順序是實現

1線性表

1.1線性表的基本操做

線性表是一種最經常使用的最簡單的一種數據結構在計算機中可喲以使用順序存儲鏈式存儲結構。對於線性表雖然每一個數據元素的值不相同但必須具備相同的數據類型,同時數據元素之間按存在一種一對一的邏輯關係及就是java

(1)      第一個數據元素沒有前驅這個數據元素稱爲開始節點數組

(2)      最後一個數據元素沒有後繼這個節點稱爲終端節點數據結構

(3)      除了第一個和最後一個節點其餘節點有且僅有一個前驅和一個後繼ide

線性表的基本操做以下:測試

(1)      線性表的置空clear()this

(2)      線性表的判空isEmpty()spa

(3)      求線性表的長度length()code

(4)      取元素操做get(i)orm

(5)      插入操做insert(I,x)在第i個數據元素以前插入一個值爲x的數據元素接口

(6)      刪除操做remove(i)

(7)      查找操做indexOf(x)

(8)      輸出操做display();

線性表的抽象數據類型用JAVA接口實現 具體描述以下:

/*
 * Kiss_My_Love
 * 2012/8/26
 * xaut.media082
 * */
package www.xaut.com;

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


1.2線性表的順序存儲和實現

順序存儲是用一組地址連續的存儲單元依次存放線性表的各個數據元素,通常採用數組來實現順序表類的描述下面是順序表的JAVA語言描述:

 

/*
 * Kiss_My_Love
 * 2012/8/26
 * xaut.media082
 * */
package www.xaut.com;

public class SqList implements Ilist {
	public Object[] listItem;                         //線性表的存儲空間
	private int curLen;                               //線性表的當前長度
	
	public SqList(int maxSize){
		this.curLen=0;
		this.listItem=new Object[maxSize];
	}
	public SqList(){
		this.curLen=0;
		
	}
	@Override
	public void clear() {
		this.curLen=0;
	}

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

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

	@Override
	public Object get(int i) {
		if(i<0||i>this.curLen){
			try {
				throw  new Exception("第"+i+"個數據元素不存在");
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return listItem[i];
	}

	@Override
	public void insert(int i, Object x) {
		if(this.curLen==this.listItem.length){
			try {
				throw new Exception("順序表已滿");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}if(i<0||i>this.curLen){
			try {
				throw  new Exception("插入位置不合法");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			for(int j=this.curLen;j>i;j--){
				this.listItem[j]=this.listItem[j-1];
			}
			this.listItem[i]=x;
			this.curLen++;
		}
         
	}

	@Override
	public void remove(int i) {
		if(i<0||i>this.curLen-1){
			try {
				throw new Exception("刪除位置不合法");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			for(int j=i;j<this.curLen-1;j++){
				this.listItem[j]=this.listItem[j+1];
			}
		}
		this.curLen--;
	}

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

	@Override
	public void display() {
		System.out.print("[");
		for(int i=0;i<this.curLen-1;i++){
			System.out.print(this.listItem[i]+",");
		}
		System.out.println(this.listItem[this.curLen-1]+"]");
		
	}

}

測試類

/*
 * Kiss_My_Love
 * 2012/8/26
 * xaut.media082
 * */
package www.xaut.com;

public class TestSqList {

	public static void main(String[] args) {
		SqList L=new SqList(10);
		L.insert(0, 'q');
		L.insert(1, 'w');
		L.insert(2, 'd');
		L.insert(3, 'f');
		L.insert(4, 'k');
		L.display();
		System.out.println("線性表是空嗎?"+L.isEmpty());
		System.out.println("線性表的長度是"+  L.length());
		System.out.println("獲取第i個元素"+L.get(4));
		L.remove(2);
		L.display();
        System.out.print("查找木個元素"+L.indexOf('f'));
		

	}

}
相關文章
相關標籤/搜索