java - java中队列的链表实现中的大小无法正确计算

标签 java linked-list queue

我正在尝试用Java实现队列的链表,并且在我的驱动程序中将一些元素出队后,大小没有调整为应有的较小数字。这是代码,输出位于其下方。

这是链表队列的实现:

import java.util.LinkedList;

//implementation of a queue by using a linked list
public class Queue<T> {
    //declaring array list to store and manipulate data 
    //using predefined methods

    private LinkedList<T> list;
    int count, front, rear;

    public Queue() {
        list= new LinkedList<T>();
        count=front=rear=0;
    }

    //Adds given element to rear of queue
    public void enqueue (T element) {
        if(front==rear) {
            list.add(front, element);
            rear++;
        }
        else {
            list.add(rear, element);
            rear++;
        }
        count++;
    }

    //removes element at queue front
    public T dequeue() {
        if(list.isEmpty()) {
            System.out.println("Queue is empty");
            return null;
        }

        T result = list.get(front);
        front++;
        count--;
        return result;
    }

    //returns reference to element at queue front
    public T first() {
        return list.get(front);
    }

    //returns true if queue is empty
    public boolean isEmpty() {
        if(list.isEmpty())
            return true;
        else
            return false;
    }

    //returns number of elements in the queue
    public int size() {
        return list.size();
    }

    //returns string representation of queue
    public String toString() {
        String result = "";
        for(int i=front;i<rear;i++) 
            result+=list.get(i)+" ";
        return result;
    }

}

这是驱动程序类。

/*Demonstrates the use of a queue implemented by
 * using a linked list*/
public class QueueLinkedListDemo {

    public static void main(String[] args) {
        Queue<Character> charList = new Queue<Character>();

//display size of queue
        System.out.println("The size of the queue is " + charList.size());

        //adding elements to queue
        System.out.println("Calling enqueue() to add 'a' to the queue");
        charList.enqueue('a');
        System.out.println("Calling enqueue() to add 'b' to the queue");
        charList.enqueue('b');

        //display size of queue
        System.out.println("The size of the queue is " + charList.size());

        System.out.println("Calling dequeue() method to remove an element from the queue " + charList.dequeue());
        System.out.println("Calling toString() method to display queue elements " + charList.toString());
        //display first element of queue
        System.out.println("The first element in queue is " + charList.first());
        //display size of queue
        System.out.println("The size of the queue is " + charList.size());

    }

}

此代码的输出: 队列大小为0 调用 enqueue() 将 'a' 添加到队列中 调用 enqueue() 将 'b' 添加到队列中 队列大小为2 调用 dequeue() 方法从队列中删除一个元素 调用toString()方法显示队列元素b 队列中的第一个元素是 b 队列大小为2

请注意,当从队列中删除元素时,大小并未从 2 更改为 1。如何解决这个问题?

最佳答案

你的列表永远不会减少。要纠正此问题,您需要执行以下操作:

    public T dequeue() {
        if(list.isEmpty()) {
            System.out.println("Queue is empty");
            return null;
        }

        T result = list.remove(0);
        return result;
   }

这将使 count、rear、front 变得无用。

关于java - java中队列的链表实现中的大小无法正确计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43917191/

相关文章:

c - 反转链表的每 k 个节点

java - LinkedBlockingQueue 和原语

C - 使用出队进行回绕时队列的段错误

java - 无法使用 Sublime Text 2、Ubuntu 12.04 64 编译 Java

Java MVC,添加屏幕使用什么模型?

java - 通过正则表达式捕获多个组

c - C语言中任意非线性方程的辛普森1/3积分法积分?

java - 能够在没有权限的情况下删除文件

C:带有链表实现的奇怪的段错误

c - 如何使用链表正确实现队列