博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java数据结构循环链表_JAVA 数据结构链表操作循环链表
阅读量:6903 次
发布时间:2019-06-27

本文共 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/

你可能感兴趣的文章
Delphi 常用控件之TlistView总结
查看>>
QUnit系列 -- 1.介绍单元测试(上)
查看>>
asp.net导出excel文件方法两则
查看>>
【103】若是有钱,我想买的东西
查看>>
single page
查看>>
Oil Deposits(poj 1526 DFS入门题)
查看>>
JavaEE入境后在做什么——公共入口疑问的答案
查看>>
Linux内核触摸屏驱动--多点触摸 【转】
查看>>
蓝桥杯——说好的进阶之入学考试
查看>>
开发API文档相关问题(*.chm)
查看>>
分布拟合——正态/拉普拉斯/对数高斯/瑞利 分布
查看>>
算法笔记_150:图论之双连通及桥的应用(Java)
查看>>
隐藏执行批处理bat文件
查看>>
函数y=sin(1/x)曲线
查看>>
WebStorm for Mac(Web 前端开发工具)破解版安装
查看>>
computational biology | Bioinformatician | 开发者指南
查看>>
Perl文件、目录常用操作
查看>>
从0开始--倒序输出。
查看>>
吉特仓库管理系统-.NET打印问题总结
查看>>
POJ 3026 Borg Maze(bfs+最小生成树)
查看>>