//LinkedList implementation of a queue taking numbers from the user and enqueue dequeue within the program
//Having an error that i cannot find, thanks for the help in advance
import java.util.ArrayList; import java.util.Scanner;
import stdlib.StdIn; import stdlib.StdOut; import stdlib.Trace;
public class W2QueueProgram- { Node last; Node first; int N= 0; private static Scanner scanner; private class Node { private Item item; private Node next; } public boolean isEmpty() { return (N==0);} public void enqueue(Item item) { //when pushing items on a queue Node oldLast= last; last= new Node(); last.item= item; last.next= null; oldLast.next= last; N++; } public Item dequeue() { //dequeue pops the first item of the queue Item item= first.item; first=first.next; N--; return item; //dequeue is absolutely same as stack's pop }
//need to get arrayList from stack current condition: public ArrayList- getQueueAsArray() { Node temp= first; ArrayList
- ourQueue= new ArrayList<>(); while(temp != null) { ourQueue.add(temp.item); temp= temp.next; } return ourQueue; } public int size() { return N; } public static void main(String[] args) { W2QueueProgram queue= new W2QueueProgram(); Scanner ourScanner= new Scanner(System.in); System.out.println("Enter values to be entered and for end of numbers enter -1 "); int numero= ourScanner.nextInt(); while(numero!=-1) { queue.enqueue(numero); //populating the array with our numbers numero= ourScanner.nextInt(); } ArrayList currentQueue= queue.getQueueAsArray(); for(int curNum: currentQueue) { System.out.print(curNum+ " "); } StdOut.println(" <--- This is the original form of our number queue \t"); //now implementing our final queue: Integer ourInt0= queue.dequeue(); //this has number 9 in it Integer ourInt1= queue.dequeue(); //this has number 8 in it Integer ourInt2= queue.dequeue(); //this has number 7 in it Integer ourInt3= queue.dequeue(); //this has number 6 in it Integer ourInt4= queue.dequeue(); //this has number 5 in it Integer ourInt5= queue.dequeue(); //this has number 4 in it Integer ourInt6= queue.dequeue(); //this has number 3 in it Integer ourInt7= queue.dequeue(); //this has number 2 in it Integer ourInt8= queue.dequeue(); //this has number 1 in it Integer ourInt9= queue.dequeue(); //this has number 0 in it queue.enqueue(ourInt9); queue.enqueue(ourInt0); queue.enqueue(ourInt2); queue.enqueue(ourInt1); queue.enqueue(ourInt4); queue.enqueue(ourInt3); queue.enqueue(ourInt6); queue.enqueue(ourInt5); queue.enqueue(ourInt8); queue.enqueue(ourInt7); currentQueue= queue.getQueueAsArray(); for(int curNum:currentQueue) { System.out.print(curNum+" "); } System.out.println(" "); } }