Question: Write a reference-based implementation of a queue that uses a linear linked list to represent the items in a queue. You will need both a
Write a reference-based implementation of a queue that uses a linear linked list to represent the items in a queue. You will need both a head reference and a tail reference. Be sure that the project includes all of the following: A Node class. Your queue ADT will need this class in order to instantiate the Node objects which make up the linear linked list.
A Queue ADT. This is a class which contains global, private head and tail reference variablesas well as the Queues wall of operations. This wall of operations are the methods which are responsible for creating and managing the ADTs Linked List which. Queue ADT will use this Linked List to represent the queue.
A Test class. This class will contain a main method which instantiates an object from your Queue ADT class and invokes every Queue ADT method. What are some ways you can invoke the Queue ADT methods to demonstrate that those methods work in the way that they are intended to?
The following are my Node method, my List method, my Queue method, and my Queue tester.
public class Node { public int item; public Node next; Node(int newItem){ item = newItem; next = null; } Node(int newItem, Node nextNode){ item = newItem; next = nextNode; } }
public class List { private Node head; private int length; public List(){ length = 0; head = null; } public boolean isEmpty(){ return length == 0; } public int size(){ return length; } private Node find(int index){ Node curr = head; for(int skip = 0; skip < index; skip++){ curr = curr.next; } return curr; } public int get(int index) throws Exception{ if(index >= 0 && index < length){ Node curr = find(index); int dataItem = curr.item; return dataItem; } else { throw new Exception("Index is out of bounds"); } } public void add(int index, int item) throws Exception{ if(index >= 0 && index < length + 1){ if(index == 0){ Node newNode = new Node(item, head); head = newNode; } else { Node prev = find(index - 1); Node newNode = new Node(item, prev.next); prev.next = newNode; } } else { throw new Exception("List index is out of bounds"); } } public void remove(int index) throws Exception{ if(index >= 0 && index < length){ if(index == 0){ head = head.next; } else { Node prev = find(index - 1); Node curr = prev.next; prev.next = curr.next; } } else { throw new Exception("List index out of bounds"); } } public void removeAll(){ head = null; length = 0; } }
public class Queue { private final List aList; List newList = new List(); public Queue(){ aList = new List(); } public boolean isEmpty(){ return aList.isEmpty(); } public void enqueue(int newItem) throws Exception{ aList.add(aList.size(), newItem); } public int dequeue() throws Exception{ if (!isEmpty()){ int queueFront = aList.get(0); aList.remove(0); return queueFront; } else { throw new Exception("Queue exception on dequeue"); } } public void dequeueAll() { aList.removeAll(); } public int peek() throws Exception{ if(!isEmpty()){ return aList.get(0); } else { throw new Exception("queue Exception on peek"); } } }
public class QueueTester {
public static void main(String[] args) throws Exception { Queue newQueue = new Queue(); for(int i = 0; i < 5; i++){ newQueue.enqueue(i); } newQueue.dequeue(); System.out.println("is the list empty? " + newQueue.isEmpty()); System.out.println(newQueue.peek()); } }
This is all I have so far and when I enqueue an int, it doesn't add it to the queue, because the is empty method still returns as true, and the dequeue method will return an exception. Please help, I am not sure where I went wrong.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
