Java实现循环双链表

本文使用Java语言详细介绍了如何实现循环双链表,包括数据元素的增、删、查操作。通过编写实现类并进行功能测试,确认代码无误,测试运行成功。
摘要由CSDN通过智能技术生成

使用Java语言实现了一个双链表,可进行数据元素的增、删、查。

代码实现如下:

import org.jetbrains.annotations.NotNull;

import java.util.Iterator;

public class DoubleLink<T> implements Iterable<T> {

    //创建一个双链表类型的结点匿名内部类
    private class Node {
        //数据域,存放T类型的数据
        T data;
        //地址域prev->前驱结点,next->后继结点
        Node prev, next;

        //构造方法重载
        public Node(T data, Node prev, Node next) {
            this.data = data;
            this.prev = prev;
            this.next = next;
        }
    }

    //声明头结点
    private Node head;
    //声明尾结点
    private Node rear;
    //记录链表长度
    private int L;

    //构建循环空链表
    public DoubleLink() {
        //使头结点的3个域均为null
        this.head = new Node(null, null, null);
        this.rear = null;
        this.L = 0;
    }

    //构造isEmpty()判断链表是否为空
    public boolean isEmpty() {
        return L == 0;
    }

    //构造length()方法获取链表长度
    public
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! Here's an example of Java code to implement a doubly circular linked list: ```java public class DoublyCircularLinkedList { private Node head; private int size; private class Node { private int data; private Node previous; private Node next; public Node(int data) { this.data = data; } } // Constructor public DoublyCircularLinkedList() { head = null; size = 0; } // Check if the list is empty public boolean isEmpty() { return head == null; } // Get the size of the list public int getSize() { return size; } // Add an element to the beginning of the list public void addFirst(int data) { Node newNode = new Node(data); if (isEmpty()) { newNode.next = newNode; newNode.previous = newNode; head = newNode; } else { newNode.next = head; newNode.previous = head.previous; head.previous.next = newNode; head.previous = newNode; head = newNode; } size++; } // Add an element to the end of the list public void addLast(int data) { Node newNode = new Node(data); if (isEmpty()) { newNode.next = newNode; newNode.previous = newNode; head = newNode; } else { newNode.next = head; newNode.previous = head.previous; head.previous.next = newNode; head.previous = newNode; } size++; } // Remove the first element from the list public void removeFirst() { if (isEmpty()) { System.out.println("List is empty!"); return; } if (size == 1) { head = null; } else { head.next.previous = head.previous; head.previous.next = head.next; head = head.next; } size--; } // Remove the last element from the list public void removeLast() { if (isEmpty()) { System.out.println("List is empty!"); return; } if (size == 1) { head = null; } else { head.previous.previous.next = head; head.previous = head.previous.previous; } size--; } // Display the elements of the list public void display() { if (isEmpty()) { System.out.println("List is empty!"); return; } Node current = head; do { System.out.print(current.data + " "); current = current.next; } while (current != head); System.out.println(); } // Main method to test the implementation public static void main(String[] args) { DoublyCircularLinkedList list = new DoublyCircularLinkedList(); list.addFirst(3); list.addFirst(2); list.addFirst(1); list.addLast(4); list.addLast(5); System.out.println("Elements of the list: "); list.display(); list.removeFirst(); list.removeLast(); System.out.println("Elements after removing first and last: "); list.display(); } } ``` This code creates a DoublyCircularLinkedList class with methods to add elements to the beginning and end, remove elements from the beginning and end, check if the list is empty, get the size of the list, and display the elements of the list. The main method is used to test the implementation by adding elements and then removing the first and last elements.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值