色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Java實現線性表的順序存儲

瀏覽:52日期:2022-08-21 17:12:10

本文實例為大家分享了Java實現線性表的順序存儲,供大家參考,具體內容如下

順序表:用一組地址連續的存儲單元依次存儲各個元素,使得在邏輯結構上相鄰的數據元素存儲在相鄰的物理存儲單元中的線性表

package algorithm.datastructure.seqlist;/*順序表** 用一組地址連續的存儲單元依次存儲各個元素,使得在邏輯結構上相鄰的數據元素存儲在相鄰的物理存儲單元中的線性表**/public class SeqList { private int length;//順序表長度 private int[] list;//數組,連續的存儲空間 //初始化,構造一個空的線性表 public SeqList(int listLength) { list = new int[listLength]; } //銷毀表 public void destroyList() { list = null; this.length = 0; } //將線性表置為空表 public void clearList() { for (int i = 0; i < getLength(); i++) { list[i] = 0; } } //判斷線性表是否未空表 public Boolean isEmpty() { return getLength() == 0; } //獲取線性表元素個數 public int getLength() { return length; } //根據下標獲取線性表元素 public int getElem(int i) { if (i < 0 || i >= getLength()) { try {throw new Exception('線性表下標越界'); } catch (Exception e) {e.printStackTrace(); } } return list[i]; } //返回某元素(第一個)的前驅 public Integer priorElem(int element) { for (int i = 0; i < getLength(); i++) { if (element == list[i]) {if (i == 0) { return null;} else { return list[i - 1];} } } return null; } //獲取某元素(第一個)的后繼 public Integer nextElem(int element) { for (int i = 0; i < getLength(); i++) { if (element == list[i]) {if (i == getLength() - 1) { return null;} else { return list[i + 1];} } } return null; } //擴容,這里設置容量變為原來兩倍 public void ensureCapacity(int capacity) { if (capacity >= list.length) {//擴容 int tempList[] = new int[list.length * 2]; for (int i = 0; i < list.length; i++) {tempList[i] = list[i]; } list = tempList; } } //在指定位置插入元素 public Boolean insertElement(int index, int element) { if (index < 0 || index >= list.length) { try {throw new Exception('下標錯誤'); } catch (Exception e) {e.printStackTrace(); } } if (index == getLength()) { return insertTailElement(element); } for (int i = 0; i < getLength(); i++) { if (i == index) {ensureCapacity(getLength() + 1);//index位置后面的元素后移for (int j = getLength() - 1; j >= index; j--) { list[j + 1] = list[j];}list[index] = element;length++; } } return true; } //尾部插入元素 public Boolean insertTailElement(int element) { ensureCapacity(length + 1); list[++length] = element; return true; } //刪除尾部元素 public int deleteTailElement() { if (getLength() == 0) { try {throw new Exception('下標錯誤'); } catch (Exception e) {e.printStackTrace(); } } int tailElement = list[getLength() - 1]; list[getLength() - 1] = 0; length--; return tailElement; } //刪除元素 public int deleteElement(int index) { if (index < 0 || index >= list.length) { try {throw new Exception('下標錯誤'); } catch (Exception e) {e.printStackTrace(); } } if (index == getLength()) { return deleteTailElement(); } for (int i = 0; i < getLength(); i++) { if (i == index) {int tailElement = list[index];//index位置后面的元素前移for (int j = index; j < getLength() - 1; j++) { list[j] = list[j + 1];}list[getLength() - 1] = 0;length--;return tailElement; } } return 0; } //遍歷順序表 public void traverseList() { for (int i = 0; i < getLength(); i++) { System.out.println(list[i]); } } public static void main(String[] args) { //測試 SeqList seqList = new SeqList(2); System.out.println(seqList.insertTailElement(1)); System.out.println(seqList.insertTailElement(2)); System.out.println(seqList.insertTailElement(3)); System.out.println(seqList.insertTailElement(4)); System.out.println(seqList.getElem(0)); System.out.println(seqList.getElem(1)); System.out.println(seqList.getElem(2)); System.out.println(seqList.getElem(3)); System.out.println(seqList.insertElement(0, 4)); System.out.println(seqList.getElem(0)); System.out.println(seqList.getElem(1)); System.out.println(seqList.getElem(2)); System.out.println(seqList.getElem(3)); System.out.println(seqList.getElem(4)); System.out.println(seqList.priorElem(3)); System.out.println(seqList.priorElem(4)); System.out.println(seqList.nextElem(4)); System.out.println(seqList.nextElem(3));// System.out.println(seqList.deleteTailElement());// System.out.println(seqList.deleteTailElement());// System.out.println(seqList.deleteTailElement());// System.out.println(seqList.deleteTailElement());// System.out.println(seqList.deleteTailElement());// System.out.println(seqList.deleteTailElement()); System.out.println(seqList.deleteElement(0)); System.out.println(seqList.deleteElement(1)); seqList.traverseList(); }}

以上就是用Java簡單實現的順序表,在Java中,如果要實現功能更復雜,性能更高的順序表,可參考ArrayList源碼。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 国产精品成人免费视频 | 国产欧美专区在线观看 | 欧美成人免费一级人片 | 日本www免费 | 久草最新在线 | 2022麻豆福利午夜久久 | 久久高清精品 | 成人性欧美丨区二区三区 | 免费看一级视频 | 国产成人毛片精品不卡在线 | 国产操操| 亚洲精品韩国美女在线 | 国产一区二区三区四区波多野结衣 | 欧美极品欧美精品欧美视频 | 国产区在线免费观看 | 一级美国乱色毛片 | 国产三级精品播放 | 窝窝女人体国产午夜视频 | 韩国porno xxxx | 高清在线观看自拍视频 | 欧美一级在线免费观看 | 天堂mv亚洲mv在线播放9蜜 | 亚洲黄色小视频 | 亚洲成a人片在线观看 欧美 | 久久精品观看 | 99久在线观看 | 久久综合久久精品 | 久草在现视频 | 俺来也欧美亚洲a∨在线 | 九九色视频 | 久久国产成人福利播放 | 亚洲欧美日韩高清在线看 | 亚洲综合色一区二区三区另类 | 欧美精品一级 | 亚洲欧美日韩视频一区 | 免费久草 | 天堂一区二区三区在线观看 | 日本私人色多多 | 精品国产精品国产 | 99久久精品国产9999高清 | 在线a国产 |