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

In this programming assignment you are required to implement a queue data struct

ID: 664236 • Letter: I

Question

In this programming assignment you are required to implement a queue data structure using a singly linked list. Write a Java program to implement the queue using a simple linked list instead of a fixed size array. You can assume the items to be stored in the queue are positive integers. You do not need to incorporate any ordering. However, the queue must have a front and a rear. Any item inserted to the queue will be inserted at rear and items must be deleted from front.

Programming: Your main() method should be interactive (menu driven) and should give the following options to a user:

i) Insert an item to the queue (at rear)

ii) Delete an item from the queue (from front)

iii) Display the item at front

iv) Display the item at rear

v) Display total number of items currently present in the queue v

i) Print the items currently present in the queue

vii) Quit

The program should start with the menu and the user should be able to choose any option from the menu. The menu should keep coming back, after completing each user choice, until the user selects ‘Quit’ option. Also, include proper error checking (for example, when someone tries to delete item from an empty queue, or tries to display items of an empty queue) with appropriate message(s).

Explanation / Answer

/*Program to implement the queue linked list */


import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
import java.util.Scanner;

public class LinkedlistQue<T> implements Queue<T> {

   private int total;

   private Node front, rear;

   private class Node {
       private T ele;
       private Node next;
   }
/*Insert into linked list queue*/
   public LinkedlistQue<T> insert(T ele)
   {
       Node current = rear;
       rear = new Node();
       rear.ele = ele;

       if (total++ == 0) front = rear;
       else current.next = rear;

       return this;
   }
   /*Delete into linked list queue*/
   public T delete()
   {
       if (total != 0) {
       T ele = front.ele;
       front = front.next;
       if (--total == 0) rear = null;
       return ele;
       }
       else{
           System.out.println("there are no items in Queue to delete");
           return null;
       }
      
   }
/*Display from front*/
   public void displayFront()
   {
       if(total != 0){
       T ele = front.ele;
       System.out.println(ele +" ");
       }
       else{
           System.out.println("there are no items in Queue to Display");
       }
   }
/*Display all items*/
   public void displayFrontItems()
   {
       if(total != 0){
           while (total != 0){
               T ele = front.ele;
               System.out.println(ele +" ");
               front = front.next;
               total --;
           }
       }
       else{
           System.out.println("there are no items in Queue to Display");
       }
   }
  
/*Display rear element*/

   public void displayRear()
   {
       if(total != 0){
       T ele = rear.ele;
       System.out.println(ele +" ");
       }
       else{
           System.out.println("there are no items in Queue to Display");
       }

   }

   @Override
   public String toString()
   {
       StringBuilder sb = new StringBuilder();
       Node tmp = front;
       while (tmp != null) {
           sb.append(tmp.ele).append(", ");
           tmp = tmp.next;
       }
       return sb.toString();
   }

   @Override
   public boolean addAll(Collection<? extends T> arg0) {

       return false;
   }

   @Override
   public void clear() {


   }

   @Override
   public boolean contains(Object arg0) {

       return false;
   }

   @Override
   public boolean containsAll(Collection<?> c) {

       return false;
   }

   @Override
   public boolean isEmpty() {

       return false;
   }

   @Override
   public Iterator<T> iterator() {

       return null;
   }

   @Override
   public boolean remove(Object o) {

       return false;
   }

   @Override
   public boolean removeAll(Collection<?> c) {

       return false;
   }

   @Override
   public boolean retainAll(Collection<?> c) {

       return false;
   }

   @Override
   public int size() {

       return 0;
   }

   @Override
   public Object[] toArray() {

       return null;
   }

   @Override
   public <T> T[] toArray(T[] a) {

       return null;
   }

   @Override
   public boolean add(T arg0) {

       return false;
   }

   @Override
   public T element() {

       return null;
   }

   @Override
   public boolean offer(T arg0) {

       return false;
   }

   @Override
   public T peek() {

       return null;
   }

   @Override
   public T poll() {

       return null;
   }

   @Override
   public T remove() {

       return null;
   }

   public static void main(String[] args){
       Queue<String> que = new LinkedlistQue<>();
       Scanner s=new Scanner(System.in);
       char ch;
       do
       {
           System.out.println(" Queue Operations");
           System.out.println("1. Insert an item to the queue");
           System.out.println("2. Delete an item from the queue");
           System.out.println("3. Display the item at front");
           System.out.println("4. Display the item at rear");
           System.out.println("5. Display total number of items currently present in the queue");
           System.out.println("6. Print the items currently present in the queue");
           System.out.println("7. Quit");
           int choice = s.nextInt();
           switch (choice)
           {
           case 1 :
           {
               System.out.println("Please enter element into the queue ");
               ((LinkedlistQue) que).insert(s.nextInt());
               break;
           }
           case 2 :
           {
               System.out.println(" Deleted an item from the queue ");
               ((LinkedlistQue) que).delete();
               break;   
           }
           case 3 :
           {
               System.out.println(" Item at the front ");
               ((LinkedlistQue) que).displayFront();
               break;
           }
           case 4 :
           {
               System.out.println("Item at the rear ");
               ((LinkedlistQue) que).displayRear();
               break;
           }
           case 5 :
           {
               System.out.println(" total number of items currently present in the queue are ");
               System.out.println(((LinkedlistQue) que).total);
               break;
           }
           case 6 :
           {
               System.out.println("Items currently present in the queue are ");
               ((LinkedlistQue) que).displayFrontItems();
               break;
           }
           case 7 :
           {
               System.out.println(" Quit ");
               System.exit(0);
               break;
           }
           default :
           {
               System.out.println("Wrong Entry ");
               break;
           }
           }
           System.out.println(" Do you want to continue (Type y or n) ");
           ch = s.next().charAt(0);
       } while (ch == 'Y'|| ch == 'y');        
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote