目錄
1.基本框架圖
2.頭插法
3.尾插法
4.任意位置插入
5.原始碼
1.基本框架圖
雙向鏈表由數值域、兩個地址域組成,head.prev = null , last.next = null

2.頭插法
建立一個新節點 node,新鏈表的prev域和next域為null,將其插在鏈表的前面,最后就變成鏈表的第一個節點

3.尾插法
建立一個新節點 node,新鏈表的prev域和next域為null,原本節點last.next = node,node.prv = last,
最后last = node,這樣新的節點就成了尾節點了

4.任意位置插入
任意位置包括了頭插、尾插、中間位置插,鏈表的節點數是從下標0開始數的,
我們在插入前,先找到要插入的位置,則被插入的位置就往后了一步,不是說真的往后移動了一步,只是下標增加了1

5.原始碼
創建兩個Java檔案,
一個是鏈表的方法:
class ListNode {
public int val;
public ListNode prev;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
//雙向鏈表
public class MyLinkedList {
public ListNode head;
public ListNode last;
//列印鏈表,和單鏈表一樣
public void display() {
ListNode cur = head;
while(cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
//得到鏈表的長度
public int size() {
ListNode cur = head;
int count = 0;
while(cur != null) {
cur = cur.next;
count++;
}
return count;
}
//查找是否包含關鍵字key,是否在鏈表當中
public boolean contains(int key) {
ListNode cur = head;
while(cur != null) {
if(cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//頭插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
if(this.head ==null) {
this.head = node;
this.last = node;
}else {
node.next = this.head;//后面的給前面
this.head.prev = node;//前面的給后面
this.head = node;//再放到前面去
}
}
//尾插法
public void addLast(int data) {
ListNode node = new ListNode(data);
if(this.head == null) {
this.head = node;
this.last = node;
}else {
this.last.next = node;//后面的給前面
node.prev = this.last;//前面的給后面
this.last = node;//再放到后面去
}
}
//找到要插入的位置
public ListNode searchIndex (int index) {
ListNode cur = this.head;
while (index != 0) {
cur = cur.next;
index--;
}
return cur;
}
//任意位置插入,第一個資料節點為0下標
public void addIndex(int index,int data) {
ListNode node = new ListNode(data);
if (index < 0 || index >size()) {
System.out.println("index位置不合法");
return;
}
//頭插
if (index == 0) {
addFirst(data);
return;
}
//尾插
if (index == size()) {
addLast(data);
return;
}
//中間部位插
ListNode cur = searchIndex (index);
node.next = cur.prev.next;
cur.prev.next = node;
node.prev = cur.prev;
cur.prev = node;
}
//洗掉第一次出現關鍵字key的位置
public void remove(int key) {
ListNode cur = head;
while(cur != null) {
if(cur.val == key) {
if(cur == head) {
head = head.next;
if(head != null) {
head.prev = null;
}else {
last = null;
}
}else {
cur.prev.next = cur.prev;
if(cur.next != null) {
//中間位置
cur.next.prev = cur.prev;
}else {
last = last.prev;
}
}
return;
}
cur = cur.next;
}
}
//洗掉重復節點
public void removeAllKey(int key) {
ListNode cur = head;
while(cur != null) {
if(cur.val == key) {
if(cur == head) {
head = head.next;
if(head != null) {
head.prev = null;
}else {
last = null;
}
}else {
cur.prev.next = cur.prev;
if(cur.next != null) {
//中間位置
cur.next.prev = cur.prev;
}else {
last = last.prev;
}
}
}
//洗掉一個繼續往下走
cur = cur.next;
}
}
//清空鏈表
public void clear() {
ListNode cur = head;
while(cur != null) {
ListNode curNext = cur.next;
cur.next = null;
cur.prev = null;
cur = curNext;
}
//頭和尾最后清空
last = null;
head = null;
}
}
一個是主函式:
下面的主函式就簡單實作幾個方法( 尾插法、指定位置添加,列印,清空)
public class TestDemo {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addLast(4);
myLinkedList.addLast(3);
myLinkedList.display();
//myLinkedList.removeAllKey(3);
myLinkedList.addIndex(2,10);
myLinkedList.display();
myLinkedList.clear();
myLinkedList.display();
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/381983.html
標籤:其他
