Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Queue Implemented with a Linked-List Write a queue class, using a singly linked-

ID: 3697704 • Letter: Q

Question

Queue Implemented with a Linked-List

Write a queue class, using a singly linked-list, that allows your queue to store type int. Your class should have the following methods:

Method Name

Return Type

Description

enque

none

removes the element at the front of the queue and returns it

deque

generic

inserts and element at the rear of the queue

isEmpty

boolean

returns true if the queue is empty, false otherwise

isFull

boolean

returns true if the queue is full, false otherwise

size

int

returns the number of elements in a queue

In your test class, create a queue and exercise all the methods.

The following code may be of help:  

}

}

data = d; }

}

Method Name

Return Type

Description

enque

none

removes the element at the front of the queue and returns it

deque

generic

inserts and element at the rear of the queue

isEmpty

boolean

returns true if the queue is empty, false otherwise

isFull

boolean

returns true if the queue is full, false otherwise

size

int

returns the number of elements in a queue

Explanation / Answer

public class LinkedIntQueue {
   private Node front, rear; // references for the ends of the queue
   private int count; // Node count, number of items in queue

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

   public int deque() {
       int rv = 0; // return value

       if (front == null) {
           System.out.println("List is empty");
       } else {
           rv = front.data;

           front = front.next;

           if (front == null)// only one node was there
               rear = null;

           count--;
       }

       return rv;
   }

   public void enque(int d) {
       Node newNode = new Node(d);

       if (front == null) {
           front = newNode;
           rear = front;
       } else {

           rear.next = newNode;
           rear = newNode;
       }

       count++;
   }

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

   public boolean isFull() {
       return false;
   }
  
   public int size(){
       return count;
   }

   private class Node {
       int data;
       Node next;

       public Node(int d) {
           data = d;
       }

   }// End class Node

   public static void main(String[] args) {
       // create a queue
       LinkedIntQueue iq = new LinkedIntQueue();
       iq.enque(22);
       iq.enque(1);
       iq.enque(94);
       System.out.println(iq.deque());
   }
}