简体   繁体   English

将队列实现为列表有什么问题?

[英]What is wrong with the implementation of my queue as a list?

I started with creating the variables head and tail of the queue, which are elements of a list themselves of course, and I create an empty list in the constructor by writing head = tail = null . 我开始创建队列的变量headtail ,当然这是列表的元素,我通过编写head = tail = null在构造函数中创建一个空列表。 In enq , I create a new element of the list called help , and it is supposed to save the new element that I want to put at the end of the list (since it is a queue). enq ,我创建了一个名为help的列表的新元素,它应该保存我想要放在列表末尾的新元素(因为它是一个队列)。 Of course the tail of the queue is always the element that we just put at the end of the list before. 当然,队列的tail始终是我们刚才放在列表末尾的元素。 Then I check whether head is empty or not (either because the list itself is empty or because we deleted the first element of it, which should be always identical with head ), and, if necessary, I define head as help too. 然后我检查head是否为空(或者因为列表本身是空的,或者因为我们删除了它的第一个元素,它应该总是与head相同),并且,如果有必要,我也将head定义为help Then help is supposed to show to a new empty element of the list. 然后help应该显示到列表的新空元素。

The problem is the following: When I test my program, it stops after the very first delete and tells me that the list is already empty. 问题如下:当我测试我的程序时,它在第一次删除后停止并告诉我列表已经是空的。

I guess the mistake lies within the method deq . 我猜错误在于方法deq I want to delete the first element by writing head = head.next , but we defined head as help before, thus, head.next is identical with help.next , which is identical with null , so the boolean -method returns that the list is empty, which stops the programm. 我想通过编写head = head.next来删除第一个元素,但我们之前将head定义为help ,因此, head.nexthelp.next相同,与null相同,因此boolean -method返回列表是空的,这会停止程序。

I might have to change the the "direction" of help.next , but I don't see how. 我可能要改变help.next的“方向”,但我不知道怎么做。 How can make this whole thing work? 怎么能让整个事情发挥作用?

Creating the elements of the list: 创建列表的元素:

public class Entry {
  Object content;
  Entry next;
}

Implementation of List: 清单的实施:

public class QueueList implements List {

  private Entry head;
  private Entry tail;

  public QueueList() {
    head = tail = null;
  }

  public boolean empty() {
    return head == null;
  }

  public void enq(Object x) {
    Entry help = new Entry();
    help.content = x;
    tail = help;

    if (head == null) {
      head = help;
    }

    help.next = null;
  }

  public Object front() {
    return head.content;
  }

  public void deq() {
    head = head.next;
  }
}
  public void enq(Object x) {
    Entry help = new Entry();
    help.content = x;
    **tail = help;**

    if (head == null) {
      head = help;
    }

    help.next = null;
  }

There always will be only one element in the queue at any time. 任何时候队列中始终只有一个元素。

This can be solved using the below enq() method 这可以使用下面的enq()方法解决

public void enq(Object x) {
        Entry help = new Entry(x);

        if (head == null) {

            head = help;

            tail = head;

        } else {
            if (head == tail) {

                head.next = help;
                tail = help;



            } else {

                this.tail.next = help;
                this.tail = help;
            }
        }

    }

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

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