【数据结构】单向循环链表

写的中间发现一个问题,我试了一下,final修饰的引用是指该引用只能指向一个对象不能更改指向其他对象,但是该引用调该对象的方法进行内部信息修改是OK的

package 链表.循环链表;

import java.util.NoSuchElementException;


/**
 * @author 芜情
 * 带头结点的单向循环链表
 */
public class CLinkedList{

    //定义链表节点
    private static class Node{
        private int data;
        private Node next;
        public int getData() {
            return data;
        }
        public void setData(int data) {
            this.data = data;
        }
        public Node getNext() {
            return next;
        }
        public void setNext(Node next) {
            this.next = next;
        }

        Node(){
        }

        Node(int e, Node next){
            data = e;
            this.next = next;
        }
    }

    //头结点
    private Node head;

    //链表长度
    private int size;

    //初始化双向链表
    public CLinkedList() {
        head = new Node();
        head.setNext(null);
        size = 0;
    }

    //判断链表是否为空
    public boolean isEmpty(){
        return head.getNext() == null;
    }

    //清空链表
    public void clear(){
        head.setNext(null);
    }

    //链表长度
    public int size(){
        return size;
    }
    /*============================================*/
    /*内部方法,为了好理解,从名字能看出来代码要干什么*/

    //判断下标是不是有效
    private boolean isValidIndex(int index) {
        return index >= 0 && index < size;
    }

    //判断某个操作的位置是否有效,如执行插入操作时
    private boolean isValidPosition(int position) {
        return position >= 0 && position <= size;
    }

    //定义报错信息
    private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size;
    }

    private void checkIndex(int index) {
        if (!isValidIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private void checkPosition(int position) {
        if (!isValidPosition(position))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(position));
    }

    //返回指定下标节点
    private Node node(int index){
        Node currentNode = head;
        int i = 0;
        while(i <= index){
            currentNode = currentNode.getNext();
            ++i;
        }
        return currentNode;
    }
    /*============================================*/

    /**
     * 增加节点
     */

    //在中间插入节点
    public void add(int position, int e){
        checkPosition(position);
        if(position == 0){
            addFirst(e);
        }else{
            final Node positionNode = node(position - 1);
            final Node node = new Node(e, positionNode.getNext());
            positionNode.setNext(node);
            ++size;
        }
    }

    //插入首节点
    public void addFirst(int e){
        final Node last = node(size - 1);
        final Node node = new Node(e, head.getNext());
        head.setNext(node);
        last.setNext(node);
        ++size;
    }

    //插入尾节点
    public void addLast(int e){
        if(isEmpty()){
            addFirst(e);
        }else{
            final Node first = head.getNext();
            final Node last = node(size - 1);
            final Node node = new Node(e, first);
            last.setNext(node);
            ++size;
        }
    }

    public void add(int e){
        addLast(e);
    }

    /**
     * 删除节点
     */

    //删除中间的一个节点
    public int remove(int index){
        checkIndex(index);
        if(index == 0){
            return remove();
        }else{
            final Node node = node(index);
            final Node beforeNode = node(index - 1);
            beforeNode.setNext(node.getNext());
            --size;
            return node.getData();
        }
    }

    //删除首节点,返回被删除的元素
    public int removeFirst(){
        final Node first = head.getNext();
        if(first == null){
            throw new NoSuchElementException();
        }
        final Node last = node(size - 1);
        head.setNext(first.getNext());
        last.setNext(first.getNext());
        --size;
        return first.getData();
    }

    public int remove(){
        return removeFirst();
    }

    //删除尾节点
    public int removeLast(){
        if(isEmpty()){
            throw new NoSuchElementException();
        }
        return remove(size - 1);
    }

    /*链表主要以增删节点的操作比较重要,改和查就不写了*/

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值