線性表的Java實現

線性表是一種可以在任意位置插入和刪除元素,由n個同類型元素組成的線性結構。主要包括順序表,單鏈表,循環單鏈表,雙向鏈表和仿真鏈表。應用比較廣泛的是順序表和單鏈表。

下面是線性表的接口,主要操作包括插入元素,刪除元素,取得元素,得到線性表元素個數,判斷線性表是否爲空。


package com.nishizhen.list;
 
public interface List {
    public void insert(int i,Object obj)throws Exception;
    public void delete(int i)throws Exception;
    public Object getData(int i)throws Exception;
    public int size();
    public boolean isEmpty();
}

順序表:
順序表插入一個元素需要移動元素的平均次數爲n/2次,刪除一個元素需要移動元素次數爲(n-1)/2,所以順序表的時間複雜度爲O(n)。
順序表的實現如下:


package com.nishizhen.list;
 
public class SeqList implements List{
    final int defaultSize = 10;
    int maxSize;//順序表的最大長度
    int size;//線性表當前長度
    Object[] listArray;//存儲線性表元素的數組
   
   
    public SeqList(int size){
       initiate(size);
    }
   
    public SeqList(){
       initiate(defaultSize);
    }
   
    public void initiate(int sz){
       maxSize = sz;
       size = 0;
       listArray = new Object[sz];
    }
   
   
    public void insert(int i,Object obj)throws Exception{
       if(size == maxSize){
           throw new Exception("順序表已滿,不能再插入元素。");
       }
       if(i<0 || i>maxSize){
           throw new Exception("參數有誤。");
       }
       else{
           for(int j=size;j>=i;j--){
              listArray[j] = listArray[j-1];
           }
          
           listArray[i] = obj;
           size++;
       }
    }
   
   
    public void delete(int i)throws Exception{
       if(size == 0){
           throw new Exception("順序表爲空,無法進行刪除元素操作。");
       }
      
       if(i<0 || i>=size){
           throw new Exception("參數出錯。");//數組下標不能小於0或者大於size,因爲size及其以後的元素爲空。
       }
       else{
           for(int j=size-1;j>=i;j--){
              listArray[j-1] = listArray[j];
           }
           listArray[listArray.length-1] = "";
           size--;
       }
    }
   
   
    public Object getData(int i)throws Exception{
       if(size == 0){
           throw new Exception("順序表爲空,無法返回元素。");
       }
      
       if(1<0 || i>=size){
           throw new Exception("參數出錯。");//數組下標不能小於0或者大於size,因爲size及其以後的元素爲空。
       }
       else{
           return listArray[i];
       }
    }
   
   
    public int size(){
       return listArray.length;
    }
   
   
    public boolean isEmpty(){
       boolean flag = false;
       if(listArray.length==0){
           flag = true;
       }
       return flag;
    }
}



單鏈表:
指針是指一個數據元素邏輯意義上的存儲位置,鏈式存儲機構是基於指針實現的,每一個節點由一個數據元素和一個指針構成。鏈式存儲結構是用指針把相互關聯的元素鏈接起來。
在單鏈表中,每個節點只有一個直接只想後繼元素的指針,而雙向鏈表中每個節點有兩個指針,一個只想後繼節點一個只想前驅節點。
單鏈表的實現
節點類:



package com.nishizhen.list;
 
public class Node {
    Object element;
    Node next;
   
   
    Node(Node nextval){
       next = nextval;
    }
   
   
    Node(Object obj,Node nextval){
       element = obj;
       next = nextval;
    }
   
   
    public Node getNext(){
       return next;
    }
   
   
    public void setNext(Node nextval){
       next = nextval;
    }
   
   
    public Object getElement(){
       return element;
    }
   
    public void setElement(Object obj){
       element = obj;
    }
   
    public String toString(){
       return element.toString();
    }
}
單鏈表類:
package com.nishizhen.list;
 
public class LinList implements List{
    Node head;//頭指針
    Node current;//當前操作的節點位置
    int size;//數據元素個數
   
    LinList(){
       head = current = new Node(null);
       size = 0;
    }
   
    public void index(int i) throws Exception{
       if(i<-1 || i>size-1){
           throw new Exception("參數出錯");
       }
       if(i==-1){
           return;
       }
       current = head.next;
       int j = 0;
       while((current !=null)&&j<i){
           current = current.next;
           j++;
       }
    }
   
    public void insert(int i,Object obj)throws Exception{
       if(1<0 || i>=size){
           throw new Exception("參數錯誤");
       }
      
       index(i-1);
       current.setNext(new Node(obj,current.next));
       size++;
    }
   
    public void delete(int i)throws Exception{
       if(size==0){
           throw new Exception("鏈表已空");
       }
       if(1<0 || i>=size){
           throw new Exception("參數錯誤");
       }
      
       index(i-1);
       Object obj = current.next.getElement();
       current.setNext(current.next.next);
       size--;
    }
   
    public Object getData(int i)throws Exception{
       if(1<0 || i>=size){
           throw new Exception("參數錯誤");
       }
       index(i);
       return current.getElement();
    }
   
    public int size(){
       return size;
    }
   
    public boolean isEmpty(){
       return size == 0;
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章