java實現線性表


/**

 * 線性表

 * @author zyyt

 *

 */

public  class LinkList {//框架級別的大師級

 

private int size;//鏈表的實際大小

 

private Object[] elementData ;//存儲元素的數組

 

private final static int DEFAULT_SIZE = 10;//默認鏈表的容量

 

private final Object[] DEFAULT_EMPTY = {};//空數組

 

public LinkList() throws Exception{//構造方法初始化LinkList

 

this(DEFAULT_SIZE);

 

}

 

public LinkList(int initCapicity) throws Exception{//構造方法初始化LinkList

 

if(initCapicity <0){//initCapicity 如果容量大小小於0 則拋出異常

System.out.println("initCapicity" + "需要大於等於0");

throw new Exception();

}

 

if(0 == initCapicity){//如果initCapicity為0 則把空數組  賦值給elementData

elementData =  DEFAULT_EMPTY;

}else{

elementData = new Object[initCapicity];//初始化elementData

}

 

}

 

//順序鏈表中添加數據

public synchronized void add(Object obj){

 

/*

* 判斷鏈表的長度與數組的大小比較,來判斷是否需要擴容

*/

if(size == elementData.length ){

 

Object[]  array = new Object[elementData.length * 2 + 1];

     

System.arraycopy(elementData, 0, array, 0, size);

elementData = array;

}

   

elementData[size++] = obj;

 

}

 

//鏈表中插入數據

    public synchronized void set(int index,Object o){

 

if(elementData.length < size + 1){

 

Object[]  array = new Object[elementData.length * 2 + 1];

     

System.arraycopy(elementData, 0, array, 0, size);

elementData = array;

}

 

for(int i = size - 1 ;i>=index;i--){

 

elementData[i+1] = elementData[i]; 

 

}

 

elementData[index] = o;

 

size++;

}

 

//獲取所在索引的元素

    public  Object get(int index) throws Exception{

    

    ensureIndexNotOutOfBounds(index);

    

    return elementData[index];

    

    }

 

    //判斷這個線性表是否為空

public boolean isEmpty(){

return size == 0;

}

 

//獲取線性表的元素個數

public int size(){

return size;

}

 

//鏈表刪除一個元素

public synchronized void remove(int index) throws Exception{

 

ensureIndexNotOutOfBounds(index);

 

System.arraycopy(elementData, index + 1, elementData, index , size - index - 1);

    size--;

}

 

//確保index的大小  在數組elementData的范圍內

private void ensureIndexNotOutOfBounds(int index) throws Exception{

 

    if(index < 0 || index >= size){

    System.out.println(index + "小於0 或者"+index + "大於" + size);

    throw new Exception();

    }

}

 

//查看線性表中是否包含某個元素

public boolean contain(Object o) throws Exception{

 

return indexOf(o) >= 0;

}

 

//線性表中添加一個線性表

public synchronized void add(LinkList list) throws Exception{

 

if(list != null){

for(int i=0;i<list.size;i++){

 

if(this.contain(list.get(i)) == false){

elementData[size++] = list.get(i);

}

}

}

 

}

 

//獲取一個元素在鏈表中第一次出現的位置

public int indexOf(Object o) throws Exception{

 

int result = -1;

 

if (o == null) {

            for (int i = 0; i < size; i++)

                if (elementData[i]==null)

                result = i;

        } else {

          for (int i = 0; i < size; i++)

              if (o.equals(elementData[i]))

              result = i;

        }

        return result;

 

}

 

//刪除鏈表中包含另外一個鏈表的元素

public synchronized void remove(LinkList list) throws Exception{

 

if(list != null){

for(int i=0;i<list.size;i++){

if(this.contain(list.get(i))){

this.remove(this.indexOf(list.get(i)));

size--;

}

}

}

 

}

 

//獲取迭代器

public Iterator iterator(){

return new Iterator();

}

 

//迭代器類

public class Iterator {

 

int cousor = -1;

 

//判斷是否還有下一個對象

public boolean hasNext(){

return cousor + 1 < size;

}

 

//獲取下一個對象

public Object next(){

return elementData[++cousor];

}

 

}

 

 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM