老規矩,看前欣賞美圖

今天給大家分享一下資料結構中一個簡單的順序表,
首先,談到資料結構,都知道他的邏輯是非常嚴謹的,要想學好資料結構,我們必須要做到的是多畫圖,多敲代碼,很多東西你可能看得懂,但是你一上手,你就會發現,你根本寫不出來,前期你可以適當的抄代碼,但最后你還得自己思考,自己畫圖,然后敲代碼,
今天,我就口頭敘述該如何實作以下代碼,
這是我們要實作的模板:
// 列印順序表
public void display() { }
// 獲取順序表的有效資料長度
public int size() { }
// 在 pos 位置新增元素
public void add(int pos, int data) { }
// 判定是否包含某個元素
public boolean contains(int toFind) { return true; }
// 查找某個元素對應的位置
public int search(int toFind) { return -1; }
// 獲取 pos 位置的元素
public int getPos(int pos) { return -1; }
// 給 pos 位置的元素設為 value
public void setPos(int pos, int value) { }
//洗掉第一次出現的關鍵字key
public void remove(int toRemove) { }
// 清空順序表
public void clear() { }
目錄
- 列印順序表
- 獲取順序表的有效長度
- 在pos位置新增元素
- 判定是否包含某個元素
- 查找某個元素的位置
- 獲取pos位置的元素
- 給pos位置元素設為value
- 洗掉第一次出現的關鍵字key
- 清空順序表
- 實作
- 總結
列印順序表
根據上面寫好的方法,我們在Java中定義好這個陣列,
自己先創建一個類
public class MyArrayList {
public int[] elem; //這個陣列(最好不要初始化)
public int usedSize;//有效的資料個數
public MyArrayList() { //構造方法
this.elem = new int[10]; //定義陣列長度
}
再寫一個測驗類,并new好物件
public class Test {
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList();
}
}
這樣列印順序表就比較簡單了
// 列印順序表
public void display() { //方法
for (int i = 0; i < this.usedSize; i++) { //遍歷這個陣列
System.out.print(this.elem[i]+" "); //列印這個陣列
}
System.out.println(); //換行
}
然后實作它
public class Test {
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList();
myArrayList.display();
}
}
當然,我們的usedSize并沒有復制,所以列印了并沒有結果,
獲取順序表的有效長度
這個比較簡單,直接回傳就可以了,
// 獲取順序表的有效資料長度
public int size() {
return this.usedSize; //回傳個數
}
在pos位置新增元素
首先,我們想到了新增元素,就得在一個位置上加一個元素,陣列的下標是從0開始的,元素個數是從1開始的,我們要先搞清楚這一點,
先寫一個方法
// 在 pos 位置新增元素
public void add(int pos, int data) { }
我們要考慮以下幾點
1:新增元素的位置是不是在這個陣列類,我們要先判定以下
及pos < 0 或 pos > usedSize 這些位置都是不合法的,
2:新增一個元素后如果之前這個空間是滿的,這個時候我們就要擴大這個空間了,要用到Array.copyOf()這個函式進行擴容
3:遍歷這個陣列,在要加的位置把他后面的元素后移騰出空間來
4:放入這個元素,元素增加一個,usedSize++一下,
這樣我們思路就清晰了:
// 在 pos 位置新增元素
public void add(int pos, int data) {
//第一步
if(pos < 0 || pos > usedSize) { //判定位置合不合法
System.out.println("pos 位置不合法!");
return; //回傳
}
//第二步
if(isFull()) { //判斷這個陣列滿了沒有
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
//滿了就擴容2倍
}
//第三步
for (int i = this.usedSize-1; i >= pos ; i--) {
//后面的元素往前遍歷,直至遇到新增的位置
this.elem[i+1] = this.elem[i]; //把新增位置后面元素移到后面空出位置
}
//第四步
this.elem[pos] = data; //新增元素
this.usedSize++; //元素加一
}
public boolean isFull() { //實作上面的方法
return this.usedSize == this.elem.length; //滿了
}
判定是否包含某個元素
首先,我們得想到要包含某個元素,有就是true,沒有就false就完了,所以我們可以這樣寫
// 判定是否包含某個元素
public boolean contains(int toFind) {
for (int i = 0; i < this.usedSize; i++) { //從0位置開始遍歷這個陣列
if(this.elem[i] == toFind) { //有元素是要找的元素
return true; //找到了
}
}
return false; //遍歷完都沒有,沒有找到
}
查找某個元素的位置
同上一樣,找到就回傳這個位置,沒有就沒有
// 查找某個元素對應的位置,找不到回傳-1
public int search(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if(this.elem[i] == toFind) {
return i; //找到了回傳下標
}
}
return -1; //沒有找到
}
獲取pos位置的元素
我們思考,又是關于pos位置的元素,那就得考慮pos在哪,合不合法,還有就是,這個陣列是不是空的這些因素,我們可以這樣寫
// 獲取 pos 位置的元素
public int getPos(int pos) {
if(pos < 0 || pos >= this.usedSize) {
System.out.println("pos 位置不合法");
return -1; //不合法回傳了-1
}
if(isEmpty()) {
System.out.println("順序表為空!");
return -1; //空表,回傳-1
}
return this.elem[pos]; //有他,回傳他對應的元素
}
public boolean isEmpty() {
return this.usedSize==0; //空表
}
給pos位置元素設為value
更新元素,pos位置同上要先判斷,合法在更新
// 給 pos 位置的元素設為/更新 value
public void setPos(int pos, int value) {
if(pos < 0 || pos >= this.usedSize) {
System.out.println("pos位置不合法");
return;
}
if(isEmpty()) {
System.out.println("順序表為空!");
return;
}
this.elem[pos] = value;
}
public boolean isEmpty() {
return this.usedSize==0; //空表
}
洗掉第一次出現的關鍵字key
洗掉元素,這個跟上面講的新增元素有點像,要先考慮,表為不為空啊,有沒有你要刪的數呀,刪完了怎么調整新的陣列,這些都是我們要考慮的
我們可以這樣寫
//洗掉第一次出現的關鍵字key
public void remove(int toRemove) {
if(isEmpty()) {
System.out.println("順序表為空!");
return;
}
int index = search(toRemove);
//根據上面實作的方法找不找得到這個數
if(index == -1) {
System.out.println("沒有你要洗掉的數字!");
return; //沒有就回傳
}
for (int i = index; i < this.usedSize-1; i++) {
this.elem[i] = this.elem[i+1];
//有這個數,從這個數開始,把后面的數粘到前一個,從而把要刪的數覆寫
}
this.usedSize--; //刪完陣列減一個元素
}
清空順序表
這個簡單,讓他元素為空就行
public void clear() {
this.usedSize = 0; //有效資料為0
}
實作
當然,我這里實作是把上面所有因素都放進去了,最好是寫一個測一個,這樣方便改正錯誤
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList();
myArrayList.add(0,1);
myArrayList.add(1,2);
myArrayList.add(2,3);
myArrayList.add(3,4);
myArrayList.display();
System.out.println(myArrayList.contains(4));
System.out.println(myArrayList.contains(8));
System.out.println(myArrayList.search(1));
System.out.println(myArrayList.search(8));
System.out.println(myArrayList.getPos(3));
myArrayList.setPos(2,99);
myArrayList.display();
myArrayList.remove(2);
myArrayList.display();
}

總結
好了,今天的分享就到這里了,當然,我今天是用口水話敘述了這個簡單的順序表,希望對剛接觸順序表的你有幫助,但你要知道,學資料結構最重要的就是畫圖理解,當然還有自己敲代碼練習,不是光看看光想想就可以成功的,既然決定了要學好資料結構就不能偷懶,當然,這句話是對你們說的,也是對博主自己說的,歡迎大家指正,感謝你的支持,祝我們一起進步!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/345733.html
標籤:其他
