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

Programming Problems: Simple Queue +--------------------------------------+ Your

ID: 3684698 • Letter: P

Question

 Programming Problems: Simple Queue         +--------------------------------------+         Your task in this assignment is complete Phase I of Project II. In Project II         with Phase I, you are asked to write a simple queue that handles integer data.         Please the skelton code that is included in this assignment for more detail.           Requirements         +--------------------------------------+          1. The queue should handle integer data.          2. The queue should be based on linked list.          3. Your code should demonistrate that you can perform the "enqueue" and             "dequeue" operation 

Explanation / Answer

QueueMain.java


public class QueueMain {

   public static void main(String[] args) {
       QueueLinkedList<Integer> queue = new QueueLinkedList<Integer>();
       //Queue<Integer> queue = new Queue<Integer>(2);
       queue.enQueue(2);
       queue.deQueue();
       queue.enQueue(3);
       queue.enQueue(31);
       queue.deQueue();
       queue.deQueue();
       queue.enQueue(4);
       queue.deQueue();
       queue.enQueue(41);
       queue.enQueue(43);
       queue.deQueue();
       queue.deQueue();
       queue.enQueue(5);
       queue.enQueue(2);
       queue.deQueue();
       queue.enQueue(3);
       queue.deQueue();
       queue.deQueue();
       queue.deQueue();
       queue.enQueue(3);
       queue.printQueue();
      
      
   }

}

QueueLinkedList.java

public class QueueLinkedList<T> {

   LinkedList<T> list = new LinkedList<>();
   public void enQueue(T t) {
       list.addRear(t);
   }
   public T deQueue() {
       return list.getFront();
   }
  
   public int queueSize() {
       return list.getTotal();
   }
  
   public T front() {
       return list.Front();
   }
  
   public void printQueue() {
       list.printList();
   }
  
   /*LinkedList<Integer> list = new LinkedList<>();
   list.addRear(2);
   list.addRear(3);
   list.addRear(4);
   list.getFront();
   list.getFront();
   //list.getFront();
   //list.getFront();
   list.addRear(5);
   //list.getFront();
   list.printList();*/
}

Queue.java

import java.util.ArrayList;

public class Queue<T> {
   private int size = -1;
   private int total=0;;
   private int front;//it act as rear.
   private int rear;//it act as front.
   ArrayList<T> data = null;

   public Queue() {
       this.data = new ArrayList<T>();
       this.front = -1;
       this.rear = -1;
   }

   public Queue(int size) {
       this.data = new ArrayList<T>();
       this.size = size;
       this.front = -1;
       this.rear = -1;
   }

   public void enQueue(T t) {
       if (this.size != -1 && queueSize() == this.size) {
           System.out.println("Queue is full!!");
       } else {
           if (this.size != -1) {
               this.front = (this.front+1)%this.size;
               /*if(this.front==this.rear&&this.total!=0){
                   System.out.println("Queue is full.");
               }else{*/
                   this.data.add(front+1, t);
                   this.total+=1;
               /*}*/
           } else {
               this.front += 1;
               this.total+=1;
               this.data.add(front, t);
              
           }

       }
   }

   public T deQueue() {
       T dt = null;
       if (queueSize() == 0) {
           System.out.println("Queue is empty");
       } else {
           if (this.size != -1) {
               this.rear = (this.rear+1)%this.size;
               /*if(this.front==this.rear&&this.total==0){
                   System.out.println("Queue is Empty");
               }else{*/
                   dt = this.data.remove(rear);
                   this.total-=1;
               /*}*/
           } else {
               this.rear += 1;
               this.total-=1;
               dt = this.data.get(rear);
              
           }

       }
       return dt;

   }

   public T front() {
       T dt = null;
       if (queueSize() == 0) {
           System.out.println("Queue is empty");
       } else {
           dt = this.data.get(rear);
       }
       return dt;
   }

   public int queueSize() {
       //implement considering circular queue as well.
       return this.total;
   }

   public void printQueue() {

       System.out.println("Data in Queue is: ");
       if(this.rear<this.front){
           for (int i = rear+1; i < front+1; i++) {
               System.out.println(data.get(i));

           }
       }else{
           do {
               this.rear=(this.rear+1)%this.size;
               System.out.println(data.get(this.rear));
           } while (this.rear!=this.front);
       }
      
   }

}

QueueArray.java

import java.util.ArrayList;

public class Queue<T> {
   private int size = -1;
   private int total=0;;
   private int front;//it act as rear.
   private int rear;//it act as front.
   ArrayList<T> data = null;

   public Queue() {
       this.data = new ArrayList<T>();
       this.front = -1;
       this.rear = -1;
   }

   public Queue(int size) {
       this.data = new ArrayList<T>();
       this.size = size;
       this.front = -1;
       this.rear = -1;
   }

   public void enQueue(T t) {
       if (this.size != -1 && queueSize() == this.size) {
           System.out.println("Queue is full!!");
       } else {
           if (this.size != -1) {
               this.front = (this.front+1)%this.size;
               /*if(this.front==this.rear&&this.total!=0){
                   System.out.println("Queue is full.");
               }else{*/
                   this.data.add(front+1, t);
                   this.total+=1;
               /*}*/
           } else {
               this.front += 1;
               this.total+=1;
               this.data.add(front, t);
              
           }

       }
   }

   public T deQueue() {
       T dt = null;
       if (queueSize() == 0) {
           System.out.println("Queue is empty");
       } else {
           if (this.size != -1) {
               this.rear = (this.rear+1)%this.size;
               /*if(this.front==this.rear&&this.total==0){
                   System.out.println("Queue is Empty");
               }else{*/
                   dt = this.data.remove(rear);
                   this.total-=1;
               /*}*/
           } else {
               this.rear += 1;
               this.total-=1;
               dt = this.data.get(rear);
              
           }

       }
       return dt;

   }

   public T front() {
       T dt = null;
       if (queueSize() == 0) {
           System.out.println("Queue is empty");
       } else {
           dt = this.data.get(rear);
       }
       return dt;
   }

   public int queueSize() {
       //implement considering circular queue as well.
       return this.total;
   }

   public void printQueue() {

       System.out.println("Data in Queue is: ");
       if(this.rear<this.front){
           for (int i = rear+1; i < front+1; i++) {
               System.out.println(data.get(i));

           }
       }else{
           do {
               this.rear=(this.rear+1)%this.size;
               System.out.println(data.get(this.rear));
           } while (this.rear!=this.front);
       }
      
   }

}

LinkedList.java

public class LinkedList<T> {
   private Node<T> list;
   private Node<T> head;
   private int total=0;
   //private Node<T> temp;
  
   public void addRear(Node<T> node){
       if(list==null||head==null){
           this.list=node;
           this.head=node;
           this.total+=1;
       }else{
           this.list.setNext(node);
           this.list=node;
           this.total+=1;
       }
   }
  
   public void addRear(T data){
       Node<T> node = new Node<T>(data);
       if(list==null||head==null){
           this.list=node;
           this.head=node;
           this.total+=1;
       }else{
           this.list.setNext(node);
           this.list=node;
           this.total+=1;
       }
   }
  
   public T getFront(){
       T data = null;
       //now null is returned as we are not throwing any exception.
       if(head==null){
           System.out.println("List is empty.");
       }else{
           data = head.getData();
           this.total-=1;
           head = head.getNext();
       }
       return data;
   }
   public T Front(){
       T data = null;
       //now null is returned as we are not throwing any exception.
       if(head==null){
           System.out.println("List is empty.");
       }else{
           data = head.getData();
       }
       return data;
   }
   public int getTotal() {
       // TODO Auto-generated method stub
       return this.total;
   }
  
   public void printList(){
       Node<T> temp = head;
       while (temp!=null) {
           System.out.println(temp.getData());
           temp = temp.getNext();
       }
   }
}

Node.java
public class Node<T> {
private T data;
private Node<T> left;
private Node<T> right;
private Node<T> next;
private Node<T> back;
private Node<T> parent;
private Node<T> child;
private Node<T> sibling;

//default constructor//
public Node(){
  
}

//constructor initialized with data
public Node(T t){
   this.data = t;
}


public T getData() {
   return data;
}

public void setData(T data) {
   this.data = data;
}

public Node<T> getLeft() {
   return left;
}

public void setLeft(Node<T> left) {
   this.left = left;
}

public Node<T> getRight() {
   return right;
}

public void setRight(Node<T> right) {
   this.right = right;
}

public Node<T> getBack() {
   return back;
}

public void setBack(Node<T> back) {
   this.back = back;
}


public Node<T> getParent() {
   return parent;
}


public void setParent(Node<T> parent) {
   this.parent = parent;
}


public Node<T> getChild() {
   return child;
}


public void setChild(Node<T> child) {
   this.child = child;
}


public Node<T> getSibling() {
   return sibling;
}


public void setSibling(Node<T> sibling) {
   this.sibling = sibling;
}

public Node<T> getNext() {
   return next;
}

public void setNext(Node<T> next) {
   this.next = next;
}
}


sample output

List is empty.                                                                                                                                               
3