简体   繁体   English

在java中实现循环链​​表

[英]Implementing Circular Linked List in java

I'm having a bit of an issue with implementing my circularly linked list. 我在实现我的循环链表时遇到了一些问题。 I'm working on a problem that requires you to implement any ADT yourself. 我正在研究一个需要您自己实施任何ADT的问题。 I seem to be okay with adding nodes to the list, however I'm in unfamiliar territory when it comes to removing. 我似乎可以将节点添加到列表中,但是在删除时我处于一个不熟悉的领域。 I included the first two remove methods to give you an idea of where my head is at, how would I go about removing the last node in the list? 我包含前两个删除方法,让您了解我的头部在哪里,如何删除列表中的最后一个节点?

public class LinkedList {
    private Node head;
    private Node tail;
    private int size = 0;
    LinkedList() {
        head = null;
        current = null;
        previous = null;
        tail = null;
        size = 0;
    }

    //checks if list is empty
    public boolean isEmpty() {
        return head == null;
    }
    //add new node to front of circularly linked list
    public void addToFront(E x) {
        if (head == null) {
            head = new Node(x);
        } else {
            Node n = new Node(x);
            x.next() = head;
            head = x;
        }
    }

    public void addtoMiddle(E x) {
        x.next = current.next();
        current.next = x;
        size = size + 1;
    }

    public void addToEnd(E x) {
        x.next = null;
        tail.next() = x;
        tail = x;
        size = size + 1;
    }

    public void removeFirst(E x) {
        if (head = null) {
            System.out.println("Error! List is empty!");
        } else {
            head = current.next();
            size = size + 1;
        }
    }

    public void removeMiddle(E x) {
        previous.next() = current.next();
        current.next() = null;
        size = size + 1;
    }

In a circular linked list the last node's next points to the head, so you loop through your nodes until node.next.equals( head ) . 在循环链表中,最后一个节点的next指向头部,因此您循环遍历节点,直到node.next.equals( head ) Note that next must never be null and if you have only one node then you have head.next = head . 注意, next必须永远不能为null,如果你只有一个节点,那么你有head.next = head

In a circular doubly linked list you also have a previous node, ie you can iterate backwards. 在循环双向链表中,您还有一个previous节点,即您可以向后迭代。 In that case your last node is just head.previous . 在这种情况下,您的最后一个节点只是head.previous

A small ascii picture: 一个小的ascii图片:

head -next---> node -next---> node -next---> last
 | ^  <---prev-      <---prev-      <---prev- | ^
 | |                                          | |
 | |_____________________________________next_| |
 |_prev_________________________________________|      

I know this is not directly an answer to your question but I feel like Thomas has given the needed explanation here. 我知道这不是你问题的直接答案,但我觉得托马斯在这里给出了必要的解释。

Since you have a lot of syntax or incompleteness errors in the example I would recommend to comment out all functions so it has no more errors. 由于示例中有很多语法或不完整错误,我建议将所有函数注释掉,以便它不再有错误。 Then comment them in again one by one correcting every error. 然后逐一评论它们,纠正每个错误。

Some advices: 一些建议:

You use class members which are not defined eg current , previous . 您使用未定义的类成员,例如currentprevious

Decide if next should be a member or function. 确定next是否应该是成员或函数。

You need to define a class for Node (containing its members and functions), like you did for LinkedList . 您需要为Node定义一个类(包含其成员和函数),就像您为LinkedList所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM