【Java】——連結串列增刪查改功能的模擬實現
阿新 • • 發佈:2021-01-01
1.連結串列:
連結串列是一種物理儲存結構上非連續儲存結構,資料元素的邏輯順序是通過連結串列中的引用連結次序來實現的。
2.連結串列的模擬實現:
首先,建立一個MyLinkedList類:
class Node{
public int data;//例項成員變數
public Node next;
public Node(){//無參構造
}
public Node(int data){//帶有一個引數的構造方法
this.data = data;
}
}
(1)建立連結串列
public class MyLinkedList{ public Node head;//表示當前連結串列的頭,其值預設為null //1.建立連結串列 public void createLinked(){ this.head = new Node(10); Node node2 = new Node(20); Node node3 = new Node(30); Node node4 = new Node(40); this.head.next= node2; node2.next = node3; node3.next = node4; } //2.列印連結串列 public void display(){ Node cur = this.head; while(cur != null){ System.out.print(cur.data + " "); cur = cur.next; } System.out.println(); } }
測試程式碼:
public class Test {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.createLinked();
myLinkedList.display();
}
}
執行結果:
(2)通過遍歷,列印連結串列的每一個元素
//3.通過遍歷,列印連結串列的每一個元素 public void display(){ Node cur = this.head; while(cur != null){ System.out.print(cur.data + " "); cur = cur.next; } System.out.println(); }
(3)通過遍歷,找到連結串列的最後一個節點
//4.通過遍歷,找到連結串列的最後一個節點
public Node findLastNode(){
if(this.head == null){
System.out.println("head == null");
return null;
}
Node cur = this.head;
while(cur.next != null){
cur = cur.next;
}
return cur;
}
(4)通過遍歷,找到連結串列的倒數第二個節點
//5.通過遍歷,找到連結串列的倒數第二個節點
public Node findLastTwo(){
if(this.head == null){
System.out.println("一個節點也沒有!");
return null;
}
if(this.head.next == null){
System.out.println("只有一個節點!");
return null;
}
Node cur = this.head;
while(cur.next.next != null){
cur = cur.next;
}
return cur;
}
(5)通過遍歷,找到連結串列的第n個節點(連結串列的長度 >= n)
//6.通過遍歷,找到連結串列的第n個節點(連結串列的長度 >= n)
public Node findN(int n){
if(this.head == null){
System.out.println("此連結串列為空!");
return null;
}
if(n <= 0){
System.out.println("輸入的n太小了!");
return null;
}
if(n > size()){
System.out.println("輸入的n超過了連結串列的長度了!");
return null;
}
int count = 1;
Node cur = this.head;
while(count != n){
count++;
cur = cur.next;
}
return cur;
}
(6)通過遍歷,計算連結串列元素的個數
//7.通過遍歷,計算連結串列元素的個數
public int size(){
Node cur = this.head;
int count = 0;//計數
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
(7)通過遍歷,找到連結串列中是否包含某個元素
//8.通過遍歷,找到連結串列中是否包含某個元素
public boolean contains(int key){
Node cur = this.head;
while(cur != null){
if(cur.data == key){
return true;
}
}
return false;
}
(8)頭插法
//9.頭插法
public void addFirst(int data){
//0.建立節點
Node node = new Node(data);
//1.判斷連結串列是否為空
if(this.head == null){
this.head = node;
}else{
//2.插入節點
node.next = this.head;
this.head = node;
}
}
(9)尾插法
//10.尾插法
public void addLast(int data){
//0.建立節點
Node node = new Node(data);
if(this.head == null){
this.head = node;
}else{
//1. cur 找尾巴
Node cur = this.head;
while(cur.next != null){
cur = cur.next;
}
//2.插入
cur.next = node;
}
}
(10)任意位置插入,第一個資料節點為0號下標
//11.任意位置插入,第一個資料節點為0號下標
public void addIndex(int index,int data) {
if(index < 0 || index > size()){
System.out.println("index位置不合法!");
return;
}
//頭插法
if(index == 0){
addFirst(data);
return;
}
//尾插法
if(index == size()){
addLast(data);
return;
}
//cur儲存的是index- 1位置的節點的引用
Node cur = moveIndex(index);
Node node = new Node(data);
node.next = cur.next;
cur.next = node;
}
/***
* 該函式的功能是找到index - 1位置的節點的引用
* @param index
* @return
*/
public Node moveIndex(int index){
Node cur = this.head;
int count = 0;
while (count != index - 1){
cur = cur.next;
count++;
}
return cur;
}
(11)刪除第一次出現關鍵字key的節點
//12.刪除第一次出現關鍵字為key的節點
public void remove(int key) {
if(this.head == null){
System.out.println("沒有節點!");
}
if(this.head.data == key){//刪除頭結點
this.head = this.head.next;
return;
}
Node prev = searchPrev(key);
if(prev == null){
System.out.println("沒有這個可以的前驅!");
}
else{
Node del = prev.next;
prev.next = del.next;
}
}
/***
* 此函式的作用找到要刪除節點的前驅
* @param key
* @return
*/
public Node searchPrev(int key){
Node cur = this.head;
while(cur.next != null){
if(cur.next.data == key){
return cur;//返回要刪除節點的前驅
}
cur = cur.next;
}
return null;
}
(12)刪除所有值為key的節點
//13.刪除所有值為key的節點
public void removeAllKey(int key){
if(this.head == null){
return;
}
Node curHead = this.head;
Node prev = this.head;
Node cur = this.head.next;
while(cur != null){
if(cur.data == key){
prev.next = cur.next;
cur = cur.next;
}else{
prev = cur;
cur = cur.next;
}
}
if(this.head.data == key){
this.head = this.head.next;
}
}
3.連結串列的問題
連結串列的缺點 | (1)以節點為單位進行儲存,不支援隨機訪問; |
連結串列的優點 | (1)任意位置插入刪除時間複雜度為O(1); (2)沒有增容問題,插入一個開闢一個空間。 |