JAVA 數(shù)據(jù)結(jié)構(gòu)鏈表操作循環(huán)鏈表
JAVA 鏈表操作:循環(huán)鏈表
主要分析示例:
一、單鏈表循環(huán)鏈表
二、雙鏈表循環(huán)鏈表
其中單鏈表節(jié)點(diǎn)和雙鏈表節(jié)點(diǎn)類(lèi)和接口ICommOperate<T>與上篇一致,這里不在贅述。參考:JAVA鏈表操作:?jiǎn)捂湵砗碗p鏈表http://www.dbjr.com.cn/article/95113.htm
一、單鏈表循環(huán)鏈表
package LinkListTest;
import java.util.HashMap;
import java.util.Map;
public class SingleCycleLinkList implements ICommOperate<SNode> {
private SNode head = new SNode("HEAD") ; // 公共頭指針,聲明之后不變
private int size = 0 ;
public int getSize() {
return this.size;
}
/*
* 鏈表插入,每次往末端插入,判定末端的標(biāo)準(zhǔn)為next是否指向head
* */
@Override
public boolean insertNode(SNode node) {
boolean flag = false ;
initLinkList() ; // 初始化鏈表
if( this.size==0 ){ // 空鏈表
this.head.setNextNode(node) ;
node.setNextNode(this.head) ;
}else{
SNode current = this.head ;
while( current.getNextNode()!=this.head ){ // 找到末端節(jié)點(diǎn)
current = current.getNextNode() ;
}
current.setNextNode(node) ;
node.setNextNode(this.head) ; // 循壞鏈表,尾節(jié)點(diǎn)指向head
}
this.size++ ;
flag = true ;
return flag;
}
/*
* 插入鏈表指定位置pos,從1開(kāi)始,而pos大于size則插入鏈表末端
* */
@Override
public boolean insertPosNode(int pos, SNode node) {
boolean flag = true ;
SNode current = this.head.getNextNode() ;
initLinkList() ;// 初始化鏈表
if( this.size==0 ){ // 鏈表為空
this.head.setNextNode(node) ;
node.setNextNode(this.head) ;// 循壞鏈表,尾節(jié)點(diǎn)指向head
this.size++ ;
}else if( this.size<pos ){ // pos位置大于鏈表長(zhǎng)度,插入末端
insertNode(node) ;
}else if( pos>0 && pos<=this.size ){ // 鏈表內(nèi)節(jié)點(diǎn)
// 1、找到要插入pos位置節(jié)點(diǎn)和前節(jié)點(diǎn),node將插入兩個(gè)節(jié)點(diǎn)之間
int find = 0;
SNode preNode = this.head; // 前節(jié)點(diǎn)
SNode currentNode = current; // 當(dāng)前節(jié)點(diǎn)
while( find<pos-1 && currentNode!=this.head ){
preNode = current ; // 前節(jié)點(diǎn)后移
currentNode = currentNode.getNextNode() ; // 當(dāng)前節(jié)點(diǎn)后移
find++ ;
if( find<pos-1 && currentNode!=this.head ){ // 未結(jié)束尋找節(jié)點(diǎn)前,后移前節(jié)點(diǎn)
current = current.getNextNode() ;
}
}
// System.out.println(preNode);
// System.out.println(currentNode);
// 2、插入節(jié)點(diǎn)
preNode.setNextNode(node);
node.setNextNode(currentNode);
this.size++ ;
}else {
System.out.println("位置信息錯(cuò)誤");
flag = false ;
}
return flag;
}
private void initLinkList(){
if( size==0 ){
this.head.setNextNode(this.head);
}
}
/*
* 指定鏈表的節(jié)點(diǎn)pos,刪除對(duì)應(yīng)節(jié)點(diǎn)。方式:找到要?jiǎng)h除節(jié)點(diǎn)的前后節(jié)點(diǎn),進(jìn)行刪除,下標(biāo)從1開(kāi)始
* */
@Override
public boolean deleteNode(int pos) {
boolean flag = false;
SNode current = this.head.getNextNode() ;
if( pos<=0 || pos>this.size || current==this.head ){
System.out.println("位置信息錯(cuò)誤或鏈表無(wú)信息");
}else{
// 1、找到要?jiǎng)h除節(jié)點(diǎn)的前后節(jié)點(diǎn)
int find = 0;
SNode preNode = this.head; // 前節(jié)點(diǎn)
SNode nextNode = current.getNextNode(); // 后節(jié)點(diǎn)
while( find<pos-1 && nextNode!=this.head ){
preNode = current ; // 前節(jié)點(diǎn)后移
nextNode = nextNode.getNextNode() ; // 后節(jié)點(diǎn)后移
find++ ;
if( find<pos-1 && nextNode!=this.head ){ // 未結(jié)束找節(jié)點(diǎn)前,后移"前節(jié)點(diǎn)"
current = current.getNextNode() ;
}
}
// System.out.println(preNode);
// System.out.println(nextNode);
// 2、刪除節(jié)點(diǎn)
preNode.setNextNode(nextNode);
System.gc(); // 回收刪除節(jié)點(diǎn)
this.size-- ;
flag = true ;
}
return flag;
}
/*
* 指定鏈表的節(jié)點(diǎn)pos,修改對(duì)應(yīng)節(jié)點(diǎn),下標(biāo)從1開(kāi)始
* */
@Override
public boolean updateNode(int pos, Map<String, Object> map) {
boolean flag = false ;
SNode node = getNode(pos, map); // 獲得相應(yīng)位置pos的節(jié)點(diǎn)
if( node!=null ){
String data = (String) map.get("data") ;
node.setData(data);
flag = true ;
}
return flag;
}
/*
* 找到指定鏈表的節(jié)點(diǎn)pos,下標(biāo)從1開(kāi)始
* */
@Override
public SNode getNode(int pos, Map<String, Object> map) {
SNode current = this.head.getNextNode() ;
if( pos<=0 || pos>this.size || current==this.head ){
System.out.println("位置信息錯(cuò)誤或鏈表不存在");
return null;
}
int find = 0 ;
while( find<pos-1 && current!=this.head ){
current = current.getNextNode() ;
find++ ;
}
return current;
}
/*
* 打印鏈表
* */
@Override
public void printLink() {
int length = this.size ;
if( length==0 ){
System.out.println("鏈表為空!");
return ;
}
SNode current = this.head.getNextNode() ;
System.out.println("總共有節(jié)點(diǎn)數(shù): " + length +" 個(gè)");
int find = 0 ;
while( current!=this.head ){
System.out.println("第 " + (++find) + " 個(gè)節(jié)點(diǎn) :" + current);
current=current.getNextNode() ;
}
}
public static void main(String[] args) {
SingleCycleLinkList scll = new SingleCycleLinkList() ;
SNode node1 = new SNode("節(jié)點(diǎn)1");
SNode node2 = new SNode("節(jié)點(diǎn)2");
SNode node3 = new SNode("節(jié)點(diǎn)3");
SNode node4 = new SNode("節(jié)點(diǎn)4");
SNode node5 = new SNode("節(jié)點(diǎn)5");
SNode node6 = new SNode("插入指定位置");
// scll.insertPosNode(scll.getSize()+1, node1) ;
// scll.insertPosNode(scll.getSize()+1, node2) ;
// scll.insertPosNode(scll.getSize()+1, node3) ;
// scll.insertPosNode(scll.getSize()+1, node4) ;
// scll.insertPosNode(scll.getSize()+1, node5) ;
scll.insertNode(node1);
scll.insertNode(node2);
scll.insertNode(node3);
scll.insertNode(node4);
scll.insertNode(node5);
System.out.println("*******************輸出鏈表*******************");
scll.printLink();
System.out.println("*******************獲得指定鏈表節(jié)點(diǎn)*******************");
int pos = 2 ;
System.out.println("獲取鏈表第 "+pos+" 個(gè)位置數(shù)據(jù) :"+scll.getNode(pos, null));
System.out.println("*******************向鏈表指定位置插入節(jié)點(diǎn)*******************");
int pos1 = 3 ;
System.out.println("將數(shù)據(jù)插入第"+pos1+"個(gè)節(jié)點(diǎn):");
scll.insertPosNode(pos1, node6) ;
scll.printLink();
System.out.println("*******************刪除鏈表指定位置節(jié)點(diǎn)*******************");
int pos2 = 3 ;
System.out.println("刪除第"+pos2+"個(gè)節(jié)點(diǎn):");
scll.deleteNode(pos2) ;
scll.printLink();
System.out.println("*******************修改鏈表指定位置節(jié)點(diǎn)*******************");
int pos3 = 3 ;
System.out.println("修改第"+pos3+"個(gè)節(jié)點(diǎn):");
Map<String, Object> map = new HashMap<>() ;
map.put("data", "this is a test") ;
scll.updateNode(pos3, map) ;
scll.printLink();
}
}
二、雙鏈表循環(huán)鏈表
package LinkListTest;
import java.util.HashMap;
import java.util.Map;
public class DoubleCycleLinkList implements ICommOperate<DNode>{
private DNode head = new DNode("HEAD"); // 公共頭指針,聲明之后不變
private int size = 0 ; // 記錄鏈表節(jié)點(diǎn)數(shù)量
public int getSize() {
return this.size;
}
/*
* 鏈表插入,每次往末端插入,判定末端的標(biāo)準(zhǔn)為next是否指向head
* */
@Override
public boolean insertNode(DNode node) {
boolean flag = false ;
initLinkList() ; // 初始化鏈表
DNode current = this.head ;
if( this.size==0 ){ // 空鏈表
this.head.setNextNode(node) ;
node.setPriorNode(this.head);
node.setNextNode(this.head) ;
}else{ // 鏈表內(nèi)節(jié)點(diǎn)
while( current.getNextNode()!=this.head ){ // 找到末端節(jié)點(diǎn)
current = current.getNextNode() ;
}
current.setNextNode(node) ;
node.setPriorNode(current);
node.setNextNode(this.head) ; // 循壞鏈表,尾節(jié)點(diǎn)指向head
}
this.size++ ;
flag = true ;
return flag;
}
/*
* 插入鏈表指定位置pos,從1開(kāi)始,而pos大于size則插入鏈表末端
* */
@Override
public boolean insertPosNode(int pos, DNode node) {
boolean flag = true;
initLinkList() ; // 初始化鏈表
DNode current = this.head.getNextNode() ;
if( this.size==0 ){ // 鏈表為空
this.head.setNextNode(node) ;
node.setPriorNode(this.head);
node.setNextNode(this.head) ;
this.size++ ;
}else if( pos>this.size ){ // pos位置大于鏈表長(zhǎng)度,插入末端
insertNode(node) ;
}else if( pos>0 && pos<=this.size ){ // 鏈表內(nèi)節(jié)點(diǎn)
// 1、找到要插入位置pos節(jié)點(diǎn),插入pos節(jié)點(diǎn)當(dāng)前位置
int find = 0;
while( find<pos-1 && current.getNextNode()!=this.head ){
current = current.getNextNode() ;
find++ ;
}
// 2、插入節(jié)點(diǎn)
if( current.getNextNode()==this.head ){ // 尾節(jié)點(diǎn)
node.setPriorNode(current);
node.setNextNode(this.head);
current.setNextNode(node);
} else if( current.getNextNode()!=this.head ) { //中間節(jié)點(diǎn)
node.setPriorNode(current.getPriorNode());
node.setNextNode(current);
current.getPriorNode().setNextNode(node);
current.setPriorNode(node);
}
this.size++ ;
}else{
System.out.println("位置信息錯(cuò)誤");
flag = false ;
}
return flag;
}
private void initLinkList(){
if( size==0 ){
this.head.setNextNode(this.head);
this.head.setPriorNode(this.head);
}
}
/*
* 指定鏈表的節(jié)點(diǎn)pos,刪除對(duì)應(yīng)節(jié)點(diǎn)。方式:找到要?jiǎng)h除節(jié)點(diǎn)的前后節(jié)點(diǎn)刪除,下標(biāo)從1開(kāi)始
* */
@Override
public boolean deleteNode(int pos) {
boolean flag = false;
DNode current = this.head.getNextNode() ;
if( pos<=0 || pos>this.size || current==this.head ){
System.out.println("位置信息錯(cuò)誤或鏈表不存在");
}else{
// 1、找到要?jiǎng)h除位置pos節(jié)點(diǎn)
int find = 0;
while( find<pos-1 && current.getNextNode()!=this.head ){
current = current.getNextNode() ;
find++ ;
}
// 2、刪除節(jié)點(diǎn)
if( current.getNextNode()==this.head ){ // 尾節(jié)點(diǎn)
current.getPriorNode().setNextNode(this.head) ;
} else if( current.getNextNode()!=this.head ) { //中間節(jié)點(diǎn)
current.getPriorNode().setNextNode(current.getNextNode()) ;
current.getNextNode().setPriorNode(current.getPriorNode()) ;
}
System.gc(); // 回收刪除節(jié)點(diǎn)
this.size-- ;
flag = true ;
}
return flag;
}
/*
* 指定鏈表的節(jié)點(diǎn)pos,修改對(duì)應(yīng)節(jié)點(diǎn),下標(biāo)從1開(kāi)始
* */
@Override
public boolean updateNode(int pos, Map<String, Object> map) {
boolean flag = false ;
DNode node = getNode(pos, map);
if( node!=null ){
String data = (String) map.get("data") ;
node.setData(data);
flag = true ;
}
return flag;
}
/*
* 找到指定鏈表的節(jié)點(diǎn)pos,下標(biāo)從1開(kāi)始
* */
@Override
public DNode getNode(int pos, Map<String, Object> map) {
DNode current = this.head.getNextNode() ;
if( pos<=0 || pos>this.size || current==this.head ){
System.out.println("位置信息錯(cuò)誤或鏈表不存在");
return null;
}
int find = 0 ;
while( find<pos-1 && current!=this.head ){
current = current.getNextNode() ;
find++ ;
}
return current;
}
/*
* 打印鏈表
* */
@Override
public void printLink() {
int length = this.size ;
if( length==0 ){
System.out.println("鏈表為空!");
return ;
}
DNode current = this.head.getNextNode() ;
int find = 0 ;
System.out.println("總共有節(jié)點(diǎn)數(shù): " + length +" 個(gè)");
while( current!=this.head ){
System.out.println("第 " + (++find) + " 個(gè)節(jié)點(diǎn) :" + current);
current=current.getNextNode() ;
}
}
public static void main(String[] args) {
DoubleCycleLinkList dcll = new DoubleCycleLinkList() ;
DNode node1 = new DNode("節(jié)點(diǎn)1");
DNode node2 = new DNode("節(jié)點(diǎn)2");
DNode node3 = new DNode("節(jié)點(diǎn)3");
DNode node4 = new DNode("節(jié)點(diǎn)4");
DNode node5 = new DNode("節(jié)點(diǎn)5");
DNode node6 = new DNode("插入指定位置");
dcll.insertPosNode(10, node1) ;
dcll.insertPosNode(10, node2) ;
dcll.insertPosNode(8, node3) ;
dcll.insertPosNode(88, node4) ;
dcll.insertPosNode(8, node5) ;
// dcll.insertNode(node1);
// dcll.insertNode(node2);
// dcll.insertNode(node3);
// dcll.insertNode(node4);
// dcll.insertNode(node5);
System.out.println("*******************輸出鏈表*******************");
dcll.printLink();
System.out.println("*******************獲得指定鏈表節(jié)點(diǎn)*******************");
int pos = 2 ;
System.out.println("獲取鏈表第 "+pos+"個(gè)位置數(shù)據(jù) :"+dcll.getNode(pos, null));
System.out.println("*******************向鏈表指定位置插入節(jié)點(diǎn)*******************");
int pos1 = dcll.getSize()+1 ;
System.out.println("將數(shù)據(jù)插入第"+pos1+"個(gè)節(jié)點(diǎn):");
dcll.insertPosNode(pos1, node6) ;
dcll.printLink();
System.out.println("*******************刪除鏈表指定位置節(jié)點(diǎn)*******************");
int pos2 = 7 ;
System.out.println("刪除第"+pos2+"個(gè)節(jié)點(diǎn):");
dcll.deleteNode(pos2) ;
dcll.printLink();
System.out.println("*******************修改鏈表指定位置節(jié)點(diǎn)*******************");
int pos3 = 3 ;
System.out.println("修改第"+pos3+"個(gè)節(jié)點(diǎn):");
Map<String, Object> map = new HashMap<>() ;
map.put("data", "this is a test") ;
dcll.updateNode(pos3, map) ;
dcll.printLink();
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)雙向鏈表的示例
- java實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)單鏈表示例(java單鏈表)
- Java數(shù)據(jù)結(jié)構(gòu)之簡(jiǎn)單鏈表的定義與實(shí)現(xiàn)方法示例
- Java模擬單鏈表和雙端鏈表數(shù)據(jù)結(jié)構(gòu)的實(shí)例講解
- Java 數(shù)據(jù)結(jié)構(gòu)鏈表操作實(shí)現(xiàn)代碼
- java 數(shù)據(jù)結(jié)構(gòu)之刪除鏈表中的元素實(shí)例代碼
- Java模擬有序鏈表數(shù)據(jù)結(jié)構(gòu)的示例
- 詳解java數(shù)據(jù)結(jié)構(gòu)與算法之雙鏈表設(shè)計(jì)與實(shí)現(xiàn)
- Java描述數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之鏈表的增刪改查詳解
- Java數(shù)據(jù)結(jié)構(gòu)之鏈表實(shí)現(xiàn)(單向、雙向鏈表及鏈表反轉(zhuǎn))
相關(guān)文章
詳細(xì)解讀JAVA多線程實(shí)現(xiàn)的三種方式
本篇文章主要介紹了詳細(xì)解讀JAVA多線程實(shí)現(xiàn)的三種方式,主要包括繼承Thread類(lèi)、實(shí)現(xiàn)Runnable接口、使用ExecutorService、Callable、Future實(shí)現(xiàn)有返回結(jié)果的多線程。有需要的可以了解一下。2016-11-11
Java元素排序Comparable與Comparator的區(qū)別
這篇文章主要介紹了Java元素排序Comparable與Comparator的區(qū)別,二者都是頂級(jí)的接口,但擁有的方法和用法是不同的,下面我們分別來(lái)看看具體是怎樣的區(qū)別吧2022-05-05
java Spring Boot 配置redis pom文件操作
這篇文章主要介紹了java Spring Boot 配置redis pom文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Spring?Cloud?Gateway編碼實(shí)現(xiàn)任意地址跳轉(zhuǎn)的示例
本文主要介紹了Spring?Cloud?Gateway編碼實(shí)現(xiàn)任意地址跳轉(zhuǎn)的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
java 中動(dòng)態(tài)代理詳解及實(shí)例
這篇文章主要介紹了java 中動(dòng)態(tài)代理詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06
springboot mybatis里localdatetime序列化問(wèn)題的解決
這篇文章主要介紹了springboot mybatis里localdatetime序列化問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Java實(shí)踐練習(xí)輕松幾行實(shí)現(xiàn)追書(shū)神器
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個(gè)追書(shū)神器,用技術(shù)改變生活,大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-10-10
java Quartz定時(shí)器任務(wù)與Spring task定時(shí)的幾種實(shí)現(xiàn)方法
本篇文章主要介紹了java Quartz定時(shí)器任務(wù)與Spring task定時(shí)的幾種實(shí)現(xiàn)方法的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
Spring?Data?JPA系列QueryByExampleExecutor使用詳解
這篇文章主要為大家介紹了Spring?Data?JPA系列QueryByExampleExecutor使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

