首页 >类库下载 >java类库 > 正文

JAVA 链表操作

原创2016-10-19 09:48:3401034
一、单链表循环链表

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 && 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;
    }
    
    /*
     * 找到指定链表的节点pos,下标从1开始
     * */
    @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;
    }
    
    /*
     * 链表插入,每次往末端插入,判定末端的标准为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 && 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;
    }

    /*
     * 找到指定链表的节点pos,下标从1开始
     * */
    @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();
    }
}
php中文网最新课程二维码

声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理

相关文章

相关视频


网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • 专题推荐

    作者信息

    高洛峰

    拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,累计授课长达上万课时, 培养了近百名IT名师, 培训数万名学员成功走向IT岗位,近年来致力于推广Web开发技术。

    最近文章
    图文详解.Net语言Smobiler开发之如何仿微信朋友圈的消息样式1851
    ASP.NET代码轻松实现微信抢红包1836
    C#开发微信多客服功能及开发集成实例1546
    推荐视频教程
  • javascript初级视频教程javascript初级视频教程
  • jquery 基础视频教程jquery 基础视频教程
  • 视频教程分类