用java泛型实现循环链​​表

时间:2021-03-04 15:51:39

I am learning java and i am still a beginner.i have written this code to implement a circular linked list and it keeps printing the numbers when i try to print the list. it looks like some kind of an infinite loop maybe. I even tried to use a debug but it didn't do much for me. I would very much appreciate it if you could review the code and see why this is happening. here is the code below. I would be also for giving me feedback on the code :) this is the class for the circular linked list

我正在学习java,我仍然是一名初学者。我已经编写了这段代码来实现循环链​​表,当我尝试打印列表时,它会不断打印数字。它看起来像某种无限循环。我甚至尝试使用调试,但它对我没有太大作用。如果你能查看代码并了解为什么会发生这种情况,我将非常感激。这是下面的代码。我也会给我反馈代码:)这是循环链表的类

public class CircularLinkedList<E> implements API<E> {
    private Node<E> head;
    private int size = 0;

    public void placeAtBeginning(E element) {
        Node<E> newNode = new Node<E>(element);
        if(head == null) {
            head = newNode;
            head.setNext(head);
        }else {
            Node<E> temp = head;
            head = newNode;
            newNode.setNext(temp);
        }
        size++;
    }

    public void placeAtEnd(E element) {
        Node<E> newNode = new Node<E>(element);
        if (head == null) {
            head = newNode;
        }else {
            Node<E> temp = head;
            while (temp.getNext() != head) {
                temp = temp.getNext();
            }
            temp.setNext(newNode);
        }

        newNode.setNext(head);
        size++;
    }

    public void deleteFromBeginning() {
        Node<E> temp = head;
        while (temp.getNext() != head) {
            temp = temp.getNext();
        }
        temp.setNext(head.getNext());
        head = head.getNext();
        size --;
    }

    public void deleteFromEnd() {
        Node<E> temp = head;
        while(temp.getNext().getNext() != head) {
            temp = temp.getNext();
        }
        temp.setNext(head);
        size--;
    }

    public void print() {
        Node<E> temp = head;
        while(temp.getNext()!= head) {
            System.out.print(temp.getValue() + " , ");
            temp = temp.getNext();
        }
        System.out.print(temp.getValue());
    }
}

this is the class for my node

这是我的节点的类

public class Node<T> {
    private Node<T> next;
    private T item;

    public Node(T item) {
        this.item = item;
    }

    public void setNext(Node<T> next) {
        this.next = next;
    }

    public Node<T> getNext() {
        return this.next;
    }

    public T getValue() {
        return this.item;
    }
}

this is my main where i tried to test it using int.

这是我尝试使用int测试它的主要部分。

public class Main {
    public static void main(String [] args) {
        API <Integer> list = new CircularLinkedList<Integer>();

        int a = 10;
        int b = 3;
        int c = 15;
        int d = 8;
        int f = 9;

        list.placeAtBeginning(a);
        list.placeAtEnd(b);
        list.print();
        System.out.println();
        list.placeAtBeginning(c);
        list.placeAtBeginning(d);
        list.print();
    }
}

this is my API which I used

这是我用过的API

public interface API <E> {
    public void placeAtBeginning(E element);
    public void placeAtEnd(E element);
    public void deleteFromBeginning();
    public void deleteFromEnd();
    public void print();
}

1 个解决方案

#1


1  

Your method placeAtBeginning() doesn't insert the new element in the circular list but simply lets the next of the new element refer to the original circular list.

您的方法placeAtBeginning()不会在循环列表中插入新元素,只是让新元素的下一个引用原始循环列表。

Try this:

public void placeAtBeginning(E element)
{
  Node<E> newNode = new Node<E>(element);
  if(head == null)
  {
    head = newNode;
    head.setNext(head);
  }
  else
  {
    Node<E> last = head;
    while (last.getNext() != head)
      last = last.getNext();
    newNode.setNext(head);
    head = newNode;
    last.setNext(head);
  }
  size++;
}

I didn't check the other methods. They might contain a similar error.

我没有检查其他方法。它们可能包含类似的错误。

#1


1  

Your method placeAtBeginning() doesn't insert the new element in the circular list but simply lets the next of the new element refer to the original circular list.

您的方法placeAtBeginning()不会在循环列表中插入新元素,只是让新元素的下一个引用原始循环列表。

Try this:

public void placeAtBeginning(E element)
{
  Node<E> newNode = new Node<E>(element);
  if(head == null)
  {
    head = newNode;
    head.setNext(head);
  }
  else
  {
    Node<E> last = head;
    while (last.getNext() != head)
      last = last.getNext();
    newNode.setNext(head);
    head = newNode;
    last.setNext(head);
  }
  size++;
}

I didn't check the other methods. They might contain a similar error.

我没有检查其他方法。它们可能包含类似的错误。