Java實(shí)現(xiàn)線性表的順序存儲
本文實(shí)例為大家分享了Java實(shí)現(xiàn)線性表的順序存儲,供大家參考,具體內(nèi)容如下
順序表:用一組地址連續(xù)的存儲單元依次存儲各個(gè)元素,使得在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲在相鄰的物理存儲單元中的線性表
package algorithm.datastructure.seqlist;
/*順序表
*
* 用一組地址連續(xù)的存儲單元依次存儲各個(gè)元素,使得在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲在相鄰的物理存儲單元中的線性表
*
*/
public class SeqList {
private int length;//順序表長度
private int[] list;//數(shù)組,連續(xù)的存儲空間
//初始化,構(gòu)造一個(gè)空的線性表
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;
}
//獲取線性表元素個(gè)數(shù)
public int getLength() {
return length;
}
//根據(jù)下標(biāo)獲取線性表元素
public int getElem(int i) {
if (i < 0 || i >= getLength()) {
try {
throw new Exception("線性表下標(biāo)越界");
} catch (Exception e) {
e.printStackTrace();
}
}
return list[i];
}
//返回某元素(第一個(gè))的前驅(qū)
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;
}
//獲取某元素(第一個(gè))的后繼
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;
}
//擴(kuò)容,這里設(shè)置容量變?yōu)樵瓉韮杀?
public void ensureCapacity(int capacity) {
if (capacity >= list.length) {//擴(kuò)容
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("下標(biāo)錯(cuò)誤");
} 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("下標(biāo)錯(cuò)誤");
} 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("下標(biāo)錯(cuò)誤");
} 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簡單實(shí)現(xiàn)的順序表,在Java中,如果要實(shí)現(xiàn)功能更復(fù)雜,性能更高的順序表,可參考ArrayList源碼。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java運(yùn)行時(shí)環(huán)境之ClassLoader類加載機(jī)制詳解
這篇文章主要給大家介紹了關(guān)于Java運(yùn)行時(shí)環(huán)境之ClassLoader類加載機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
在SpringBoot項(xiàng)目中整合攔截器的詳細(xì)步驟
在系統(tǒng)中經(jīng)常需要在處理用戶請求之前和之后執(zhí)行一些行為,例如檢測用戶的權(quán)限,或者將請求的信息記錄到日志中,即平時(shí)所說的"權(quán)限檢測"及"日志記錄",下面這篇文章主要給大家介紹了關(guān)于在SpringBoot項(xiàng)目中整合攔截器的相關(guān)資料,需要的朋友可以參考下2022-09-09
Jenkins源代碼管理SVN實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Jenkins源代碼管理SVN實(shí)現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Kafka利用Java實(shí)現(xiàn)數(shù)據(jù)的生產(chǎn)和消費(fèi)實(shí)例教程
這篇文章主要給大家介紹了關(guān)于Kafka利用Java實(shí)現(xiàn)數(shù)據(jù)的生產(chǎn)和消費(fèi)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
java使用JDBC動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表及SQL預(yù)處理的方法
這篇文章主要介紹了java使用JDBC動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表及SQL預(yù)處理的方法,涉及JDBC操作數(shù)據(jù)庫的連接、創(chuàng)建表、添加數(shù)據(jù)、查詢等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08
使用@JsonFormat和@DateTimeFormat對Date格式化操作
這篇文章主要介紹了使用@JsonFormat和@DateTimeFormat對Date格式化操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

