循环双链表Java

本文介绍了一种循环双链表的数据结构设计与实现方法,并提供了完整的Java代码示例。该循环双链表支持基本操作如添加、删除、获取等,并实现了自定义的循环双链表类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考
算法导论
java数据结构与算法之双链表设计与实现

package com.example;

import com.sun.org.apache.bcel.internal.classfile.PMGClass;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

/**
 * Created by 谢晗超 on 2018/6/21.
 */

public class LoopDoubleLinkedList<T> implements ILinkedList<T> {
    DNode<T> head;

    public LoopDoubleLinkedList() {
        this.head = new DNode<>();
        this.head.next = this.head;
        this.head.prev = this.head;
    }

    public LoopDoubleLinkedList(T[] arrays) {
        this();
        if (arrays.length < 1 || arrays == null) {
            return;
        }
        DNode<T> p = new DNode<>(arrays[0]);
        this.head.next = p;
        this.head.prev = p;
        p.prev = this.head;
        p.next = this.head;
        int index = 1;
        while (index < arrays.length) {
            p.next = new DNode<>(arrays[index++], p, this.head);
            this.head.prev = p.next;
            p = p.next;
        }

    }

    public void print() {
        DNode<T> cur = this.head.next;
        //因为是循环链表不用考虑null的情况
        while (cur != this.head) {
            System.out.println(cur.data);
            cur = cur.next;
        }
    }

    @Override
    public T set(int index, T data) {
        if (index < 0 || data == null || isEmpty()) {
            return null;
        }
        DNode<T> cur = this.head.next;
        int curIndex = 0;
        while (cur != this.head && curIndex < index) {
            cur = cur.next;
            curIndex++;
        }
        if (cur != head) {
            T oldData = cur.data;
            cur.data = data;
            return oldData;
        }
        return null;
    }

    public static void main(String[] args) {
        //0, 1, 2, 3, 4, 5, 6, 7, 8, 9
        Integer[] arrays = new Integer[]{0, 1, 2, 3};
        LoopDoubleLinkedList<Integer> loopDoubleLinkedList = new LoopDoubleLinkedList(arrays);
        ILinkedList removeList = new LoopDoubleLinkedList(new Integer[]{0, 2});
        loopDoubleLinkedList.removeAll(removeList);
        loopDoubleLinkedList.print();
    }

    @Override
    public boolean isEmpty() {
        if (this.head == this.head.next) {
            return true;
        }
        return false;
    }

    @Override
    public int length() {
        int count = 0;
        if (isEmpty()) {
            return 0;
        }
        DNode<T> cur = this.head.next;
        while (cur != this.head) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public T get(int index) {
        DNode<T> cur = null;
        if (index < 0 || isEmpty()) {
            return null;
        }
        cur = this.head.next;
        int curIndex = 0;
        while (cur != this.head) {
            if (curIndex == index) {
                break;
            } else {
                cur = cur.next;
            }
            curIndex++;
        }
        return cur.data;
    }


    @Override
    public boolean add(int index, T data) {
        if (index < 0 || isEmpty()){
            return false;
        }

        DNode<T> cur = this.head.next;
        int curIndex = 0;
        while (cur != this.head && curIndex < index){
            curIndex++;
            cur = cur.next;
        }

        DNode<T> newData = new DNode<>(data, cur, cur.next);
        cur.next.prev = newData;
        cur.next = newData;
        return true;
    }

    @Override
    public boolean add(T data) {
        if (data == null){
            return false;
        }
        DNode<T> newDNode = null;
        newDNode = new DNode<>(data, this.head.prev, this.head);
        this.head.prev.next = newDNode;
        this.head.prev = newDNode;
        return true;
    }

    @Override
    public T remove(int index) {
        if (index < 0 || isEmpty()){
            return null;
        }
        int curIndex = 0;
        DNode<T> cur = this.head.next;
        while (cur != this.head && curIndex < index){
            curIndex++;
            cur = cur.next;
        }
        cur.prev.next = cur.next;
        cur.next.prev = cur.prev;
        return cur.data;
    }

    public void remove(){

    }

    @Override
    public boolean removeAll(ILinkedList data) {
        boolean isRemoveAll = false;
        DNode<T> cur = this.head.next;
        while (cur != this.head){
            if (data.contains(cur.data)){
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                isRemoveAll = true;
            }
            cur = cur.next;
        }
        return isRemoveAll;
    }

    @Override
    public void clear() {
        this.head.prev = this.head;
        this.head.next = this.head;
    }

    @Override
    public boolean contains(T data) {
        if (data == null || isEmpty()){
            return false;
        }
        DNode<T> cur = this.head.next;
        while (cur != this.head){
            if (data.equals(cur.data)){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值