国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

順序線性表的代碼實現方法

2020-01-26 14:10:47
字體:
來源:轉載
供稿:網友

1、采用一個數組實現一個順序線性表中添加元素、刪除元素等基本操作

package com.ietree.basic.datastructure.Sequence;import java.util.Arrays;/** * 順序線性表 *  * @param <T> * @author Dylan */public class SequenceList<T> { private final 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);  // 將指定索引處之后的所有元素向后移動一格  System.arraycopy(elementData, index, elementData, index + 1, size - index);  elementData[index] = 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 void add(T element) {  insert(element, size); }  // 刪除順序線性表中指定索引處的元素 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() {  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();  } }}

測試模擬線性表的基本操作:

package com.ietree.basic.datastructure.Sequence;/** * 測試類 *  * @author Dylan */public class SequenceListTest {  public static void main(String[] args) {  SequenceList<String> list = new SequenceList<String>();  list.add("aaa");  list.add("bbb");  list.add("ccc");  list.add("ddd");  list.insert("eee", 1);  System.out.println(list);  list.delete(2);  System.out.println(list);  System.out.println("ccc在順序線性表中的位置:" + list.locate("ccc")); }}

程序輸出:

[aaa,eee,bbb,ccc,dd][aaa,eee,ccc,dd]

ccc在順序線性表中的位置:2

以上這篇順序線性表的代碼實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 康马县| 河东区| 六安市| 神农架林区| 盘锦市| 收藏| 铜山县| 定结县| 集贤县| 乐清市| 九江市| 平阴县| 平舆县| 平远县| 随州市| 三明市| 文山县| 广河县| 城步| 临夏县| 蕉岭县| 樟树市| 绿春县| 博乐市| 大同市| 泰宁县| 五原县| 河西区| 芦山县| 黄平县| 名山县| 泗洪县| 左贡县| 云阳县| 淮安市| 怀来县| 高要市| 林芝县| 林芝县| 韩城市| 吴旗县|