【數據結構】線性表之順序線性表

public class SequenceList<T> {

   private int DEFAULT_SIZE = 16;
   // 保存數組的長度
   private int capacity;
   // 定義一個數組用於保存順序線性表元素
   private Object[] elementData;
   // 保存順序表中元素的當前個數
   private int size = 0;

   // 以默認數組長度建立空順序線性表
   public SequenceList() {
      capacity = DEFAULT_SIZE;
      elementData = new Object[capacity];
   }

   // 以一個初始化元素來建立順序線性表
   public SequenceList(T element) {
      this();
      elementData[0] = element;
      size++;
   }

   /**
    * 以指定長度的數組來建立順序線性表
    *
    * @param element  指定順序線性表中第一個元素
    * @param initSize 指定順序線性表底層數組的長度
    */
   public SequenceList(T element, int initSize) {
      capacity = 1;
      // 把capacity設爲大於initSize的最小的2的n次方
      while (capacity < initSize) {
         capacity <<= 1;
      }
      elementData = new Object[capacity];
      elementData[0] = element;
      size++;
   }

   // 獲取順序線性表的大小
   public int length() {
      return size;
   }

   // 獲取順序線性表中索引爲i處的元素
   public T get(int i) {
      if (i < 0 || i > size - 1) {
         throw new IndexOutOfBoundsException("線性表索引越界");
      }
      return (T) elementData[i];
   }

   // 查找順序線性表中指定元素的索引
   public int locate(T element) {
      for (int i = 0; i < size; i++) {
         if (elementData[i].equals(element)) {
            return i;
         }
      }
      return -1;
   }

   // 向順序線性表的指定位置插入一個元素
   public void insert(T element, int index) {
      if (index < 0 || index > size) {
         throw new IndexOutOfBoundsException("線性表索引越界");
      }
      ensureCapacity(size + 1);
      // 將index處以後全部元素向後移動一個
      System.arraycopy(elementData, index, elementData, index + 1, size - index);
      elementData[index] = element;
      size++;
   }

   // 在線性順序表的開始處添加一個元素
   public void addAtHeader(T element) {
       insert(element, 0);
   }

   //在線性表中添加元素
   public void add(T element) {
      insert(element, size);
   }

   //
   private void ensureCapacity(int minCapacity) {
      if (minCapacity > capacity) {
         // 不斷地將capacity * 2, 直到capacity大於minCapacity爲止
         while (capacity < minCapacity) {
            capacity <<= 1;
         }
         elementData = Arrays.copyOf(elementData, capacity);
      }
   }

   // 刪除順序線性表中指定索引處的元素
   public T delete(int index) {
      if (index < 0 || index > size - 1) {
         throw new IndexOutOfBoundsException("線性表索引越界");
      }
      T oldValue = (T) elementData[index];
      int numMoved = size - index - 1;
      if (numMoved > 0) {
         System.arraycopy(elementData, index + 1, elementData, index, numMoved);
      }
      // 清空最後一個元素
      elementData[--size] = null;
      return oldValue;
   }

   // 刪除順序線性表中最後一個元素
   public T remove() {
      return delete(size - 1);
   }

   // 判斷順序線性表是否爲空表
   public boolean empty() {
      return size == 0;
   }

   // 清空線性表
   public void clear() {
      // 將底層數組全部元素賦爲null
      Arrays.fill(elementData, null);
      size = 0;
   }

   public String toString() {
      if (size == 0) {
         return "[]";
      } else {
         StringBuilder sb = new StringBuilder("[");
         for (int i = 0; i < size; i++) {
            sb.append(elementData[i].toString() + ", ");
         }
         int len = sb.length();
         return sb.delete(len - 2, len).append("]").toString();
      }
   }
}
public class SequenceListTest {

    public static void main(String[] args) {
        SequenceList<String> list = new SequenceList<String>();
        list.add("aaaa");
        list.add("bbbb");
        list.add("cccc");
        System.out.println("線性表初始化列表:" + list.toString());
        list.insert("dddd", 1);
        System.out.println("線性表插入元素後列表:" + list.toString());
        list.delete(2);
        System.out.println("線性表刪除元素後列表:" + list.toString());
        //獲取cccc字符串在順序線性表中的位置
        System.out.println("cccc在順序線性表中的位置:" + list.locate("cccc"));
    }
}
相關文章
相關標籤/搜索