Java实现循环链表

之前写实现单链表的初始化只实现了头指针没实现头结点,现在连同之前的博文一起做了修改。今天实现循环链表只是在单链表的基础上将最后一个节点的指向改为头结点,在代码中红色的代码为修改部分。测试代码在实现单链表的博文中有给出,这里不再列出。


package cn.dtstructure;



/*
 * 循环链表
 * 在单链表的基础上将最后一个节点指向头结点(头指针)
 * new节点充当节点 没有new的节点即指针
 * */
public class LoopCircle<E> {
        private Node head_point;//头指针
        private Node head_node;//头节点
        private Node rear;//尾指针
        private Node point;//临时指针
        private Integer length;//长度
        
        private class Node{
            private E data;//数据域
            private Node next;//指针域
            //未带参数的构造函数用于初始化头结点
            public  Node(){
                this.data = null;
            }
            //带参数的构造函数用于初始化节点数据
            public Node(E data){
                this.data = data;
            }
        }
        
        //初始化链表 只创建不含数据的头结点
        public LoopCircle(){        
            this.head_point = new Node();
            this.head_node = new Node();
            this.rear = head_node;
            //1.相对单链表不同 初始化时头结点指向自己而非null
            head_node.next = head_node;

            this.length = 0;
            System.out.println("链表进行初始化");
            show();
        }
        
        //从尾部添加节点 传入数据
        public void insert(E data){
            //临时指针指向新建节点
            point = new Node(data);
            //尾指针的下一指针指向新节点
            rear.next = point;
            //尾指针指向新节点
            rear = point;
            //2.最后一个节点指向头结点
            point.next = head_node;

//            point.next = null;
            length++;
        }
        
        //在指定位置添加节点 默认第一个节点位置为1
        public void insertInPosition(E data,Integer position){
            if (position>=1 && position<=length) {
                //将临时指针移动到操作位置的前一个位置
                move(position);    
                Node tmp = new Node(data);
                tmp.next = point.next;
                point.next = tmp;
                length++;
            }else if (position == length+1) {
                insert(data);
            }else {
                System.out.println("插入位置不正确");
            }
            
        }
        
        //删除指定位置节点
        public void delete(Integer position){
            if (position>=1 && position<=length) {
                //将临时指针移动到操作位置的前一个位置
                move(position);    
                //若是删除最后一个节点 则point.next = null;
                point.next = point.next.next;
                //判断最后一个节点是否改变 若改变移动尾指针到对应位置
                if (point.next == null) {
                    rear = point;
                    //3.若是删除最后一个节点 则更新后链表的最后一个节点指向头结点
                    point.next = head_node;

                }
                length--;
            }else{
                System.out.println("删除位置不正确");
            }

        }
        
        //修改指定位置节点
        public void update(Integer position,E data){
            if (position>=1 && position<=length) {
                //将临时指针移动到操作位置的前一个位置
                move(position);        
                point.next.data = data;
            }else{
                System.out.println("更新位置不正确");
            }
        }
        
        //查询指定位置节点
        public void select(Integer position){
            if (position>=1 && position<=length) {
                //将临时指针移动到操作位置的前一个位置
                move(position);        
                System.out.println("当前位置节点的数据为:"+point.next.data);
            }else{
                System.out.println("查询位置不正确");
            }
        }
        
        //显示链表当前信息
        public void show(){
            String result="head_point->head_node->";
            point = head_node;
            while(point.next!=head_node){
                result = result + point.next.data + "->";
                point = point.next;
            }
            result += "head_node";
            System.out.println(result);
        }
        
        //将临时指针移动到带操作节点的前一个位置
        public void move(Integer position){
            point = head_node;
            while(--position>0){
                point = point.next;
            }
        }
}


测试结果

链表进行初始化
head_point->head_node->head_node
insert(2)
head_point->head_node->2->head_node
insert(3)
head_point->head_node->2->3->head_node
insert(5)
head_point->head_node->2->3->5->head_node
insertInPosition(1, 1)
head_point->head_node->1->2->3->5->head_node
insertInPosition(4, 4)
head_point->head_node->1->2->3->4->5->head_node
insertInPosition(6, 6)
head_point->head_node->1->2->3->4->5->6->head_node
delete(1)
head_point->head_node->2->3->4->5->6->head_node
delete(3)
head_point->head_node->2->3->5->6->head_node
delete(4)
head_point->head_node->2->3->5->head_node
update(1, 1)
head_point->head_node->1->3->5->head_node
update(2, 2)
head_point->head_node->1->2->5->head_node
update(3, 3)
head_point->head_node->1->2->3->head_node
当前位置节点的数据为:1
select(1)
head_point->head_node->1->2->3->head_node
当前位置节点的数据为:2
select(2)
head_point->head_node->1->2->3->head_node
当前位置节点的数据为:3
select(3)
head_point->head_node->1->2->3->head_node
select(4)
查询位置不正确
insertInPosition(5, 5)
插入位置不正确
delete(4)
删除位置不正确
update(0, 0)
更新位置不正确
*****最后结果*****
head_point->head_node->1->2->3->head_node



猜你喜欢

转载自blog.csdn.net/qq646040754/article/details/79747694