1

I am implementing a linked queue in Java. However, there is/are error/s when I run my code.

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();
    }
}

I've been configuring forever what's wrong in my LinkedQueue. Please help me fix the code. I'm new in Java and maybe the mistakes are caused by sytax errors.

2
  • 1
    What errors are you getting? And what code are you running to get them? Commented Jul 17, 2012 at 13:51
  • 1
    is this homework? in that case please tag it accordingly... Otherwise what error messages do you get? Commented Jul 17, 2012 at 13:52

5 Answers 5

2

You can use all the functionality of a Queue from the java.util.LinkedList generic class. Use the addLast method to enqueue an element, and the removeFirst method to dequeue an element. Since LinkedList is doubly linked, you should see all the performance benefits of a Queue.

Sign up to request clarification or add additional context in comments.

Comments

1

java.util.LinkedList already implements a Queue.
Why don't use that?

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

3 Comments

I am asked to use the long method linked queue.
what do you mean by long method linked queue?
By that, I mean not to use the built in Java queue commands or whatever you call that :)
0

Your enqueue() method does nothing with the element passed in. Probably you want to pass it on to the Node's constructor?

3 Comments

Hi Sir. Uhm, can you please teach me how to pass it on the Node? I'm confused with those generics.
simply pass the element to the Node constructor: Node<E> node = new Node<E> (element, null);. You should probably remove the constructor's second argument as is it always null.
After calling enqueue() for the first time, both frontand rear point to the node added. Your implementation of isEmpty() then returns true! You should change the implementation of isEmpty() to check count == 0.
0

This looks extreamly suspicious:

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

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

  rear = node;
  count++;

}

the parameter element is never used. Try

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

or something.

Comments

0

Here is the sample implementation of Linked list using Queues.

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();
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.