栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA 数据结构链表操作循环链表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA 数据结构链表操作循环链表

JAVA 链表操作:循环链表

主要分析示例:

一、单链表循环链表

二、双链表循环链表

其中单链表节点和双链表节点类和接口ICommOperate与上篇一致,这里不在赘述。参考:JAVA链表操作:单链表和双链表https://www.jb51.net/article/95113.htm

一、单链表循环链表

 
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;
  }
  
  
  @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;
  }
  
  
  @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 && pos<=this.size ){ // 链表内节点
      // 1、找到要插入pos位置节点和前节点,node将插入两个节点之间
      int find = 0;
      SNode preNode = this.head; // 前节点
      SNode currentNode = current; // 当前节点
      while( findthis.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;
  }
  
  
  @Override
  public SNode getNode(int pos, Map map) {
    SNode current = this.head.getNextNode() ;
    if( pos<=0 || pos>this.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();
  }

}

 二、双链表循环链表



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;
  }
  
  
  @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;
  }
  
  
  @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 && pos<=this.size ){  // 链表内节点
      // 1、找到要插入位置pos节点,插入pos节点当前位置
      int find = 0;
      while( findthis.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;
  }

  
  @Override
  public DNode getNode(int pos, Map map) {
    DNode current = this.head.getNextNode() ;
    if( pos<=0 || pos>this.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();
  }
}



感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148913.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号