数据结构-循环链表

理解与实现循环链表
循环链表是一种特殊的数据结构,其头结点与尾节点相连形成闭环。在编程实现中,包括创建新链表、添加元素、删除元素和查询元素等操作。添加元素时要考虑头结点、尾节点为空或非空的情况。删除元素则需处理头结点、尾节点和其他位置的元素。查询操作需遍历链表,区分头尾节点与其他节点。

循环链表:将头结点与尾节点相连构成一个闭环,自行车的链子就是很好的具体形象。

编码思路:

1、要头尾相连,需要有头结点head,尾节点end,并且初始化时end.next = head,next;head.next=end.next,构成一个闭

2、往循环链表中添加数据,a、如果头结点head 为空则head=newNode,b、若头结点不为空,尾节点为空则end=newNode,并且将头结点和尾节点连接构成闭环;c、若head和end都不为null则end.next=newNode;newNode.next=head;end=newNode

3、删除数据:分三种情况,头结点,尾节点,其他,头结点:head=head.next;end.next=head;其他:以tem.next.data作为判断;如果判断出相等时,tem.next=tem.next.next;尾节点:当tem.next==end时,tem.next=head;

4、查询:不论是根据索引还是根据数据,都先判断头结点和尾节点,然后再判断其他。

代码:

package list.linklist;

public class CircularLinkedList {
    /**
     * 头结点
     */
    Node head;
    /**
     * 尾节点
     */
    Node end;
    int size = 0;

    /**
     * 循环链表,添加
     * @param data
     */
    public void add(T data) {
        Node tem = head;
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            this.size++;
            return;
        }
        if (end == null) {
            end = newNode;
            head.next = end;
            end.next = head;
            this.size++;
            return;
        }

        while (tem.next != end) {
            tem = tem.next;
        }
        
        end.next=newNode;
        newNode.next=head;
        end=newNode;
        this.size++;
    }

    /**
     * 循环链表 删除
     * @param data
     * @return
     */
    public T delete(T data) {
        Node tem = head;
        if (head.data.equals(data)) {
            head = tem.next;
            end.next = tem.next;
            this.size--;
            return tem.data;
        }
        while (tem.next != end) {
            if (tem.next.data.equals(data)) {
                tem.next = tem.next.next;
                this.size--;
                return tem.next.data;
            }
            tem = tem.next;
        }
        if (end.data.equals(data)) {
            tem.next = head;
            end = tem;
            return tem.data;
        }

        return null;
    }

    /**
     * 查询
     * @param index
     * @return
     */
    public T getValue(int index) {
        if (index >= 0 && index < size) {
            int i = 0;
            if (index == 0) {
                return head.data;
            }
            if (index == this.size - 1) {
                return end.data;
            }
            Node tem = head;
            while (tem.next != end) {
                i++;
                if (i == index) {
                    return tem.data;
                }
                tem = tem.next;
            }

        }
        return null;
    }

    public int getIndex(T data) {
        int i = 0;
        Node tem = head;
        while (tem.next != end) {
            if (tem.data.equals(data)) {
                return i;
            }
            i++;
            tem = tem.next;
        }
        if (end.data.equals(data)) {
            return this.size - 1;
        }
        return -1;
    }
    public void selectAll(){
        Node tem = head;
        while(tem.next!=end){
            System.out.println(tem.data.toString());
            tem=tem.next;
        }
        System.out.println(end.data.toString());
    }
    /**
     * 链表的大小
     * @return
     */
    public int size() {
        return this.size;
    }

    public boolean isEmpty() {
        if (head != null) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        CircularLinkedList list = new CircularLinkedList();
        for ( int i = 0; i < 50; i++ ) {
            list.add(i);
        }
       list.selectAll();
        list.delete(44);
        list.selectAll();
        System.out.println(list.getIndex(30));
        System.out.println(list.getValue(5));
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值