數據結構與算法分析筆記(2)--線性表

線性表(list)是由叫作元素(element)的數據項組成的一種有限並且有序的序列。包括有序線性表(sorted list)和無序線性表(unsorted list)。

線性表的實現有兩種標準方法:順序表(sequential list)和鏈表(linked list)。

順序表的實現是用數組來存儲表中的元素,這就意味着將要分配固定長度的數組。因此當線性表生成時數組的長度必須是已知的。既然每個線性表可以有一個不等長的數組,那麼這個長度就必須由線性表對象來記錄。在任何給定的時刻,線性表事實上都有一定數目的元素,這個數目應該小於數組允許的最大值。線性表中當前的實際元素數目也必須在線性表中記錄。順序表把表中的元素存儲在數組中相鄰的位置上。數組的位置與元素的位置相對應。換句話說,就是表中第i個元素存儲在數組的第i個單元中。表頭總是在第0個位置。

線性表的抽象數據類型(ADT)

add(newEntry)

add(newPosition,newEntry)

remove(givenPosition)

clear()

replace(givenPosition,newEntry)

getEntry(givenPosition)

contains(anEntry)

getLength()

isEmpty()

ifFull()

display()

線性表ListInterface接口

public interface ListInterface{
	public boolean add(Object newEntry);
	public boolean add(int newPosition,Object newEntry);
	public Object remove(int givenPosition);
	public void clear();
	public boolean replace(int givenPosition,Object newEntry);
	public Object getEntry(int givenPosition);
	public boolean contains(Object anEntry);
	public int getLength();
	public boolean isEmpty();
	public boolean ifFull();
	public void display();
}

ListInterface接口的實現:

AList類:

public class AList<T> implements ListInterface<T> {  
    private T[] list;  
    private int length;  
    private static final int MAX_SIZE = 50;  
    public AList(){  
        this(MAX_SIZE);  
    }  
    @SuppressWarnings("unchecked")  
    public AList(int maxSize){  
        length = 0;  
        list = (T[]) new Object[maxSize];  
    }  
    public boolean add(T newEntry) {  
        boolean isSuccessful = true;  
          
        if(!isFull()){  
            list[length] = newEntry;  
            length++;  
        }else {  
            isSuccessful =false;  
        }  
        return isSuccessful;  
    }  
      
    public boolean add(int newPosition, T newEntry) {  
        boolean isSuccessful = true;  
          
        if(!isFull()&&(newPosition>=1)&&(newPosition<=length+1)){  
            makeRoom(newPosition);  
            list[newPosition-1] = newEntry;  
        }else   
            isSuccessful = false;  
          
        return isSuccessful;  
    }  
    public void makeRoom(int newPosition){  
        for (int index = length;index >= newPosition;index--){  
            list[index] = list[index-1];  
        }  
    }  
    public T remove(int givenPosition) {  
        T result = null;  
          
        if ((givenPosition >= 1)&& (givenPosition <= length)){  
            assert !isEmpty();  
            result = list[givenPosition - 1];  
            if (givenPosition < length)  
                removeGap(givenPosition);  
              
            length--;  
        }  
        return result;  
    }  
    public void removeGap(int givenPosition){  
        int removedIndex = givenPosition -1;  
        int lastIndex = length -1;  
          
        for(int index = removedIndex;index < lastIndex;index++){  
            list[index] = list[index+1];  
        }  
    }  
    public void clear() {  
        length = 0;       
    }  
      
    public boolean replace(int givenPosition, T newEntry) {  
        boolean isSuccessful = true;  
        if((givenPosition >= 1)&&(givenPosition <= length))  
            list[givenPosition - 1] = newEntry;  
        else  
            isSuccessful = false;  
        return isSuccessful;  
    }  
  
    public T getEntry(int givenPosition) {  
        T result = null;  
          
        if((givenPosition>=1)&&(givenPosition<=length)){  
            assert !isEmpty();  
            result = list[givenPosition-1];  
        }  
        return result;  
    }  
    public boolean contains(T anEntry) {  
        boolean found = false;  
        for (int index = 0;!found && (index < length);index++){  
            if(anEntry.equals(list[index]))  
                found = true;  
        }  
        return found;  
    }  
    public int getLength() {  
        return length;  
    }  
    public boolean isEmpty() {  
        return length == 0;  
    }  
    public boolean isFull() {  
        return length == list.length;  
    }  
    public void display() {  
        for (int index = 0;index < length;index++ ){  
            System.out.println(list[index]);  
        }         
    }  
}  

數組實現線性表的優缺點:
1.檢索元素很快
2.在線性表的末尾插入一個元素很快
3.在其他元素之間插入或刪除一個元素需要在數組中移動元素
4.增加數組的長度需要複製元素

線性表測試類 myList.java

public class myList {
	public static void main(String args[]){
		AList<String> myfirstList = new AList<String>(5);
		myfirstList.add("a");
		myfirstList.add("b");
		myfirstList.add("c");
		myfirstList.add("d");
		myfirstList.add("e");
		myfirstList.display();
		
		myfirstList.remove(2);
		myfirstList.display();
		String stringInList = myfirstList.getEntry(2);
		System.out.println(stringInList);
	}
}

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