本文共 3894 字,大约阅读时间需要 12 分钟。
JAVA 数据结构链表操作循环链表
发布于 2020-5-28|
复制链接
摘记: JAVA 链表操作:循环链表主要分析示例:
一、单链表循环链表二、双链表循环链表一、单链表循环链表
```java
package LinkListTest;
import j ..
JAVA 链表操作:循环链表主要分析示例:一、单链表循环链表二、双链表循环链表一、单链表循环链表
```java
package LinkListTest;
import java.util.HashMap;
import java.util.Map;
public class SingleCycleLinkList implements ICommOperate {
private SNode head = new SNode("HEAD") ; // 公共头指针,声明之后不变
private int size = 0 ;
public int getSize() {
return this.size;
}
/*
* 链表插入,每次往末端插入,判定末端的标准为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 ){ // 找到末端节点
current = current.getNextNode() ;
}
current.setNextNode(node) ;
node.setNextNode(this.head) ; // 循坏链表,尾节点指向head
}
this.size++ ;
flag = true ;
return flag;
}
/*
* 插入链表指定位置pos,从1开始,而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) ;// 循坏链表,尾节点指向head
this.size++ ;
}else if( this.size0 && posthis.size || current==this.head ){
System.out.println("位置信息错误或链表无信息");
}else{
// 1、找到要删除节点的前后节点
int find = 0;
SNode preNode = this.head; // 前节点
SNode nextNode = current.getNextNode(); // 后节点
while( find map) {
boolean flag = false ;
SNode node = getNode(pos, map); // 获得相应位置pos的节点
if( node!=null ){
String data = (String) map.get("data") ;
node.setData(data);
flag = true ;
}
return flag;
}
/*
* 找到指定链表的节点pos,下标从1开始
* */
@Override
public SNode getNode(int pos, Map map) {
SNode current = this.head.getNextNode() ;
if( posthis.size || current==this.head ){
System.out.println("位置信息错误或链表不存在");
return null;
}
int find = 0 ;
while( find map = new HashMap() ;
map.put("data", "this is a test") ;
scll.updateNode(pos3, map) ;
scll.printLink();
}
}
```
二、双链表循环链表
```java
package LinkListTest;
import java.util.HashMap;
import java.util.Map;
public class DoubleCycleLinkList implements ICommOperate{
private DNode head = new DNode("HEAD"); // 公共头指针,声明之后不变
private int size = 0 ; // 记录链表节点数量
public int getSize() {
return this.size;
}
/*
* 链表插入,每次往末端插入,判定末端的标准为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{ // 链表内节点
while( current.getNextNode()!=this.head ){ // 找到末端节点
current = current.getNextNode() ;
}
current.setNextNode(node) ;
node.setPriorNode(current);
node.setNextNode(this.head) ; // 循坏链表,尾节点指向head
}
this.size++ ;
flag = true ;
return flag;
}
/*
* 插入链表指定位置pos,从1开始,而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位置大于链表长度,插入末端
insertNode(node) ;
}else if( pos>0 && posthis.size || current==this.head ){
System.out.println("位置信息错误或链表不存在");
}else{
// 1、找到要删除位置pos节点
int find = 0;
while( find 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;
}
/*
* 找到指定链表的节点pos,下标从1开始
* */
@Override
public DNode getNode(int pos, Map map) {
DNode current = this.head.getNextNode() ;
if( posthis.size || current==this.head ){
System.out.println("位置信息错误或链表不存在");
return null;
}
int find = 0 ;
while( find map = new HashMap() ;
map.put("data", "this is a test") ;
dcll.updateNode(pos3, map) ;
dcll.printLink();
}
}
```
转载地址:http://gvodl.baihongyu.com/