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

Java 数据结构链表操作实现代码

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

Java 数据结构链表操作实现代码

 链表是一种复杂的数据结构,其数据之间的相互关系使链表分成三种:单链表、循环链表、双向链表,下面将逐一介绍。链表在数据结构中是基础,也是重要的知识点,这里讲下Java 中链表的实现,

JAVA 链表操作:单链表和双链表

主要讲述几点:

一、链表的简介

二、链表实现原理和必要性

三、单链表示例

四、双链表示例

一、链表的简介 

  链表是一种比较常用的数据结构,链表虽然保存比较复杂,但是在查询时候比较便捷,在多种计算机语言都相应的应用,链表有多种类别,文章针对单链表和双链表进行分析。链表中数据就像被一个链条串联一起,轻易的可以实现数据的访问。

二、链表实现原理和必要性

  这里只分析单链表和双链表。链表的实现过程是有些许复杂的,但是会带来许多好处。比如现在网购时代到来,商家发快递一般会将商品包装在盒子里并写上地址信息,快递公司就可以通过盒子上的信息找到买家,商品完整到达。如果没有盒子的保护,有可能在途中商品受损。而链表就好比那个写了地址信息的盒子,既保护了商品信息,同时又写好了物流信息。链表之中存在一个HEAD节点,类似“火车头”,只要找到相应HEAD节点,就可以对链表进行操作。此次分析中,HEAD节点只是做数据头,不保存有效数据。

  单链表的实现原理如图:

  

  双链表实现原理:

 

三、单链表示例  

ICommOperate 接口操作类:

package linkListTest;
import java.util.Map;
public interface ICommOperate {
  
  public boolean insertNode(T node) ;
  public boolean insertPosNode(int pos, T node) ;
  public boolean deleteNode(int pos) ;
  public boolean updateNode(int pos, Map map) ;
  public T getNode(int pos, Map map) ;
  public void printlink() ;
}

单链表节点:

package linkListTest;
// 单连表节点
public class SNode {
  private String data;
  private SNode nextNode;
  public SNode() {
  }
  public SNode(String data) {
    this.data = data;
    this.nextNode = new SNode();
  }
  
  public String getData() {
    return data;
  }
  public void setData(String data) {
    this.data = data;
  }
  public SNode getNextNode() {
    return nextNode;
  }
  public void setNextNode(SNode nextNode) {
    this.nextNode = nextNode;
  }
  @Override
  public String toString() {
    return "SNode [data=" + data + "]";
  }
}

单链接操作类:

package linkListTest;
import java.util.HashMap;
import java.util.Map;
public class SinglelinkList 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 ; 
    SNode current = this.head ;
    if( this.size==0 ){ // 空链表
      this.head.setNextNode(node) ;
      node.setNextNode(null) ;
    }else{ // 链表内节点
      while( current.getNextNode()!=null ){
 current = current.getNextNode() ;
      }
      current.setNextNode(node) ;
      node.setNextNode(null) ;
    }
    this.size++ ;
    flag = true ;
    
    return flag;
  }

  
  @Override
  public boolean insertPosNode(int pos, SNode node){
    boolean flag = true; 
    SNode current = this.head.getNextNode() ;
    
    if( this.size==0 ){   // 空链表
      this.head.setNextNode(node) ;
      node.setNextNode(null) ;
      this.size++ ;
    }else if( this.size0 && pos<=this.size) { // 链表内节点
      // 1、找到要插入pos位置节点和前节点
      int find = 0;
      SNode preNode = this.head; // 前节点
      SNode currentNode = current; // 当前节点
      while( findthis.size || current==null ){
      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==null ){
      System.out.println("位置信息错误或链表不存在");
      return null;
    }
    int find = 0 ;
    while( find map = new HashMap<>() ;
    map.put("data", "this is a test") ;
    sll.updateNode(pos3, map) ;
    sll.printlink();
  }
}

四、双链表示例

ICommOperate 接口操作类:

package linkListTest;
import java.util.Map;
public interface ICommOperate {  
  public boolean insertNode(T node) ;
  public boolean insertPosNode(int pos, T node) ;
  public boolean deleteNode(int pos) ;
  public boolean updateNode(int pos, Map map) ;
  public T getNode(int pos, Map map) ;
  public void printlink() ;
}

双链表节点:

package linkListTest;
// 双连表节点
public class DNode {
  private DNode priorNode;
  private String data;
  private DNode nextNode;
  
  public DNode(){ 
  }
  public DNode(String data) {
    this.priorNode = new DNode() ;
    this.data = data ;
    this.nextNode = new DNode() ;
  }

  public DNode getPriorNode() {
    return priorNode;
  }
  public void setPriorNode(DNode priorNode) {
    this.priorNode = priorNode;
  }

  public String getData() {
    return data;
  }
  public void setData(String data) {
    this.data = data;
  }

  public DNode getNextNode() {
    return nextNode;
  }
  public void setNextNode(DNode nextNode) {
    this.nextNode = nextNode;
  }

  @Override
  public String toString() {
    return "DNode [data=" + data + "]";
  }  
}

 双链表实现类:

package linkListTest;
import java.util.HashMap;
import java.util.Map;
public class DoublelinkList 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; 
    
    DNode current = this.head ;
    if( this.size==0 ){ // 空链表
      this.head.setNextNode(node) ;
      node.setPriorNode(this.head);
      node.setNextNode(null) ;
    }else{ // 链表内节点
      while( current.getNextNode()!=null ){
 current = current.getNextNode() ;
      }
      current.setNextNode(node);
      node.setNextNode(null);
      node.setPriorNode(current);
    }
    this.size++ ;
    flag = true ;
  
    return flag;
  }
  
  
  @Override
  public boolean insertPosNode(int pos, DNode node) {
    boolean flag = true;
    
    DNode current = this.head.getNextNode() ;
    if( this.size==0){      // 链表为空
      this.head.setNextNode(node) ;
      node.setNextNode(null) ;
      node.setPriorNode(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==null ){
      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==null ){
      System.out.println("位置信息错误或链表不存在");
      return null;
    }
    int find = 0 ;
    while( find map = new HashMap<>() ;
    map.put("data", "this is a test") ;
    dll.updateNode(pos3, map) ;
    dll.printlink();
  } 
}

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

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

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

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