队列使用链接列表

时间:2012-07-17 13:50:00

标签: java linked-list queue

我在Java中实现了一个链接队列。但是,当我运行代码时会出现错误/。

public class LinkedQueue<E> implements Queue<E> {
   private int count;
   private Node<E> front, rear;

   public LinkedQueue() {
      count = 0;
      front = rear = null;
   }

   public void enqueue (E element) {
      Node<E> node = new Node<E> ();

      if (isEmpty())
         front = node;
      else
         rear.setNext (node);

      rear = node;
      count++;
   }

   public E dequeue() throws QueueEmptyException {
      if (isEmpty())
         throw new QueueEmptyException  ("queue");

      E result = front.getElement();
      front = front.getNext();
      count--;

      if (isEmpty())
         rear = null;

      return result;

   }

   public E first() throws QueueEmptyException {
      if (isEmpty())
         throw new QueueEmptyException ("queue"); 

      return front.getElement();
   }


   public boolean isEmpty() {
      return (front == rear);
   }


   public int size() {
      return count;
   }


    public E front() throws QueueEmptyException {       
        if (isEmpty())
            throw new QueueEmptyException("Queue underflow.");
        return (E) front.getNext();
    }
}

我一直在配置我的LinkedQueue中的错误。请帮我修复代码。我是Java的新手,也许错误是由sytax错误引起的。

5 个答案:

答案 0 :(得分:2)

您可以使用java.util.LinkedList泛型类中Queue的所有功能。使用addLast方法将元素排入队列,使用removeFirst方法使元素出列。由于LinkedList是双向链接的,因此您应该看到队列的所有性能优势。

答案 1 :(得分:1)

java.util.LinkedList已经实现了Queue 为什么不使用它?

Queue<T> queue = new LinkedList<>();

答案 2 :(得分:0)

你的enqueue()方法对传入的元素没有任何作用。可能你想把它传递给Node的构造函数?

答案 3 :(得分:0)

这看起来非常可疑:

public void enqueue (E element) {
  Node<E> node = new Node<E> ();

  if (isEmpty())
     front = node;
  else
     rear.setNext (node);

  rear = node;
  count++;

}

从不使用参数element。尝试

Node<E> node = new Node<E> (element);

或其他什么。

答案 4 :(得分:0)

以下是使用队列的链接列表的示例实现。

public class LinkedQueue<E> {

    private DoublyLinkedNode<E> head;
    private DoublyLinkedNode<E> tail;

    int size;

    public void enqueue(E item) {

        DoublyLinkedNode<E> oldTail = this.tail;

        DoublyLinkedNode<E> newTailnode = new DoublyLinkedNode<E>(item);

        if(oldTail != null){
            oldTail.setNextNode(newTailnode);
            newTailnode.setNextNode(null);
            this.tail = newTailnode;

        }else{
            this.tail = newTailnode;
            this.head = newTailnode;
        }
        size++;
    }

    public boolean isEmpty() {
        return this.head == null;
    }
    public int length(){
        return size;
    }

    public E deque() {
        if (isEmpty()) {
            throw new NoSuchElementException("Queue underflow");
        }

        E data = this.head.getData();

        this.head = this.head.getNextNode();

        size--;

        return data;
    }

    public E peek() {
        return this.head.getData();
    }

    public static void main(String[] args) {
        LinkedQueue<Double> queuelist = new LinkedQueue<Double>();
        queuelist.enqueue(60.0);
        queuelist.enqueue(12.0);
        queuelist.enqueue(16.4);
        queuelist.enqueue(26.5);

        queuelist.deque();
        System.out.println("queuelist.peek:"+ queuelist.peek());

        queuelist.deque();
        System.out.println("queuelist.length:"+queuelist.length());
        System.out.println("queuelist.peek:"+ queuelist.peek());
    }
}

public class DoublyLinkedNode<E> {
    private E data;
    private DoublyLinkedNode<E> nextNode;
    private DoublyLinkedNode<E> previousNode;

    public DoublyLinkedNode(E data) {
        this.data = data;
    }
    public E getData() {
        return data;
    }
    public DoublyLinkedNode<E> getNextNode() {
        return nextNode;
    }
    public void setNextNode(DoublyLinkedNode<E> nextNode) {
        this.nextNode = nextNode;
    }

    public DoublyLinkedNode<E> getPreviousNode() {
        return previousNode;
    }

    public void setPreviousNode(DoublyLinkedNode<E> prevNode) {
        this.previousNode = prevNode;
    }

    @Override
    public String toString() {
        return this.data.toString();
    }

}