java数据结构循环链表_JAVA 数据结构链表操作循环链表

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();

}

}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值