Java實現(xiàn)雙向循環(huán)鏈表
雙向循環(huán)鏈表定義
相比于單鏈表,有兩個指針,next指針指向下一個結(jié)點,prior指針指向上一個結(jié)點,最后一個結(jié)點的next指針指向頭結(jié)點,頭結(jié)點的prior指針指向最后一個結(jié)點
代碼實現(xiàn):
我們對單鏈表的實現(xiàn)加以修改
package algorithm.datastructure.doublelinkedlist;
import java.util.NoSuchElementException;
/**
* 雙向循環(huán)鏈表
* 兩個指針,next指針指向下一個結(jié)點,prior指針指向上一個結(jié)點,最后一個結(jié)點的next指針指向頭結(jié)點,
* 頭結(jié)點的prior指針指向最后一個結(jié)點
* */
public class LinkedList {
private Node head;//頭節(jié)點
private int size;//鏈表長度
static private class Node{
private int data;//數(shù)據(jù)
private Node next;//下一個結(jié)點
private Node prior;//上一個結(jié)點
public Node(){
}
public Node(int data){
this.data=data;
}
private Node(int data,Node next){
this.data=data;
this.next=next;
}
public Node(Node prior,int data,Node next){
this.prior=prior;
this.data=data;
this.next=next;
}
}
//初始化空鏈表
public LinkedList(){
//head=null;
}
//添加元素
public Boolean add(int element){
linkLast(element);
return true;
}
//在某個位置之前添加元素
public Boolean add(int index,Integer element){
checkPositionIndex(index);
if (index==size){
linkLast(element);
} else {
linkBefore(element,node(index));
}
return true;
}
//根據(jù)下標(biāo)獲取元素
public int get(int index){
checkElementIndex(index);
return node(index).data;
}
//獲取第一個元素
public Integer getFirst(){
Node f=head;
if (f==null){
throw new NoSuchElementException();
} else {
return f.data;
}
}
//獲取最后一個元素
public Integer getLast(){
if (size==0){
throw new NoSuchElementException();
}
return head.prior.data;
}
//刪除第一個元素
public Integer removeFirst(){
Node f=head;
if (f==null){
throw new NoSuchElementException();
} else {
return unlink(head);
}
}
//刪除最后一個元素
public Integer removeLast(){
if (size==0){
throw new NoSuchElementException();
}
int index=size-1;
return unlink(node(index));
}
//根據(jù)索引刪除元素
public Integer remove(int index){
checkElementIndex(index);
return unlink(node(index));
}
//銷毀鏈表
public void destroyList(){
clearList();
}
//將鏈表置為空表
public void clearList() {
for (Node p=head;p!=null;){
Node next=p.next;//記錄下一個結(jié)點
p=null;//刪除當(dāng)前結(jié)點
p=next;//指向下一個結(jié)點
}
size=0;
head=null;
}
//遍歷鏈表
public void traverseList(Boolean isReverseVisited){
if (!isEmpty()){
if (!isReverseVisited){
for (Node p=head;p!=head.prior;p=p.next){
System.out.println(p.data);
}
System.out.println(head.prior.data);
} else {
for (Node p=head.prior;p!=head;p=p.prior){
System.out.println(p.data);
}
System.out.println(head.data);
}
}
}
//返回鏈表元素個數(shù)
public int size(){
return size;
}
public Boolean isEmpty(){
return size==0;
}
/**雙向鏈表插入一個結(jié)點,要改變的指針如下:
*
*(1)前一個結(jié)點的next指針
*(2)后一個結(jié)點的prior指針
*(3)新創(chuàng)建的結(jié)點的prior指針和next指針
* */
//尾部添加結(jié)點
private void linkLast(int element){
if (size==0){//沒有結(jié)點時
head=new Node(element);
head.next=head;
head.prior=head;
size++;
} else {
//得到最后一個結(jié)點
Node oldTail=head.prior;
//new新的尾結(jié)點 newTail
//newTail的前一個結(jié)點設(shè)置為舊的尾結(jié)點,
//newTail的后一個結(jié)點指向head
Node newTail=new Node(oldTail,element,head);
//head的下一個結(jié)點指向newTail
head.prior=newTail;
//舊的尾結(jié)點的下一個結(jié)點指向新的尾結(jié)點
oldTail.next=newTail;
size++;
}
}
//在某結(jié)點之前插入結(jié)點
private void linkBefore(int element,Node node){
if (node==null){
linkLast(element);
} else {
Node p=head;
if (node.data==p.data){
Node q=p.prior;
Node newNode=new Node(q,element,p);
q.next=newNode;
p.prior=newNode;
size++;
} else {
for (p=p.next;p!=head;){
if (node.data==p.data){
Node q=p.prior;
Node newNode=new Node(q,element,p);
q.next=newNode;
p.prior=newNode;
size++;
}
p=p.next;
}
}
}
}
/*
* 雙向鏈表刪除一個結(jié)點:
* (1)找到該結(jié)點
* (2)找到該結(jié)點的前一結(jié)點(prior)p和下一結(jié)點(next)q
* (3)p的next指針指向q,q的prior指針指向p
* (4)如果是刪除的是頭結(jié)點,指明當(dāng)前頭結(jié)點
* */
//刪除結(jié)點
private Integer unlink(Node node){
Integer deleteNodeData=node.data;
Node p=null,q=null;
if (deleteNodeData==head.data){//該結(jié)點為頭結(jié)點
Node cur=head;
p=head.prior;
q=head.next;
p.next=q;
q.prior=p;
head=q;
cur=null;
size--;
} else {
Node m;
for (m=head.next;m!=head;){
if (m.data==deleteNodeData){
p=m.prior;
q=m.next;
p.next=q;
q.prior=p;
m=null;
size--;
break;
}
m=m.next;
}
}
return deleteNodeData;
}
//數(shù)組下標(biāo)是否越界
private Boolean isElementIndex(int index){
return index>=0&&index<size;
}
//插入位置是否越界
public Boolean isPositionIndex(int index){
return index>=0&&index<=size;
}
//檢驗下標(biāo)是否越界,拋出異常
private void checkElementIndex(int index){
if(!isElementIndex(index)){
try {
throw new IndexOutOfBoundsException("下標(biāo)越界");
} catch (Exception e) {
e.printStackTrace();
}
}
}
//檢驗插入下標(biāo)是否越界,拋出異常
private void checkPositionIndex(int index){
if(!isPositionIndex(index)){
try {
throw new IndexOutOfBoundsException("下標(biāo)越界");
} catch (Exception e) {
e.printStackTrace();
}
}
}
//返回指定位置的元素
private Node node(int index){
int nowIndex = 0;
if(size>0){
for (Node p=head;p!=head.prior;){
if (nowIndex==index){
return p;
}
p=p.next;
nowIndex++;
}
if (nowIndex==index){
return head.prior;
}
}
return null;
}
public static void main(String[] args) {
java.util.LinkedList linkedList0=new java.util.LinkedList();
linkedList0.add(6);
linkedList0.remove(0);
linkedList0.size();
linkedList0.peek();
//linkedList0.getFirst();
linkedList0.clear();
//測試
LinkedList linkedList=new LinkedList();
linkedList.add(2);
linkedList.add(3);
linkedList.add(5);
linkedList.add(7);
linkedList.add(1);
linkedList.add(33);
linkedList.add(4,0);
linkedList.add(3,1);
linkedList.add(7,6);
linkedList.add(0,10);
linkedList.add(10,11);
linkedList.remove(0);
linkedList.remove(0);
linkedList.remove(0);
linkedList.remove(1);
linkedList.remove(4);
linkedList.remove(5);
linkedList.remove(0);
// linkedList.remove(0);
// linkedList.remove(0);
// linkedList.remove(0);
// linkedList.remove(0);
System.out.println(linkedList.get(0));
System.out.println(linkedList.get(1));
System.out.println(linkedList.get(2));
System.out.println(linkedList.get(3));
System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());
linkedList.removeFirst();
linkedList.removeLast();
System.out.println("遍歷鏈表");
linkedList.traverseList(false);
// System.out.println("逆向遍歷鏈表");
// linkedList.traverseList(true);
System.out.println("鏈表長度");
System.out.println(linkedList.size());
linkedList.clearList();
}
}
總結(jié)
以上就是Java簡單實現(xiàn)雙向鏈表,更多功能可參考Java集合中的LinkedList實現(xiàn)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
1秒鐘實現(xiàn)Springboot?替換/寫入?word文檔里面的文字、圖片功能
這篇文章主要介紹了Springboot?替換/寫入?word文檔里面的文字、圖片,1秒鐘實現(xiàn),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問題
這篇文章主要介紹了解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Springboot 使用maven release插件執(zhí)行版本管理及打包操作
maven-release-plugin 可用于構(gòu)建release版本項目,實現(xiàn)自動打tag、遞增版本號、分發(fā)release版本jar包至倉庫,接下來通過本文給大家介紹Springboot 使用maven release插件執(zhí)行版本管理及打包操作,需要的朋友可以參考下2022-03-03
一小時迅速入門Mybatis之Prepared Statement與符號的使用
這篇文章主要介紹了一小時迅速入門Mybatis之Prepared Statement與符號的使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
基于SpringBoot后端導(dǎo)出Excel文件的操作方法
這篇文章給大家介紹了基于SpringBoot后端導(dǎo)出Excel文件的操作方法,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
70行Java代碼實現(xiàn)深度神經(jīng)網(wǎng)絡(luò)算法分享
這篇文章主要介紹了70行Java代碼實現(xiàn)深度神經(jīng)網(wǎng)絡(luò)算法分享,涉及神經(jīng)網(wǎng)絡(luò)的計算過程,神經(jīng)網(wǎng)絡(luò)的算法程序?qū)崿F(xiàn),多層神經(jīng)網(wǎng)絡(luò)完整程序?qū)崿F(xiàn)等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以參考下。2017-11-11

