数据结构与算法分析笔记(2)--线性表

来源:互联网 发布:windows平板推荐 2016 编辑:程序博客网 时间:2024/05/01 14:35

线性表(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);}}
                                             
0 0