Usnig Java : Implement two versions of a generic queue: One using an array and a
ID: 3663676 • Letter: U
Question
Usnig Java :
Implement two versions of a generic queue: One using an array and another using a singly linked list. Remember queues are first in first out (FIFO). Use the driver to then test each of the queues.
Driver Link : https://cse.sc.edu/~shephejj/csce146/Labs/Lab03_files/QueuesTester.java
Create a class GenArrayQueue which has the following:
Instance Variables
queue which an array of the generic type T
Class Varaibles (IE constant)
DEFAULT_SIZE this integer value takes the value of 100
Constructors
A default constructor that initializes queue of the default size
A parameterized constructor which takes in a whole number that sets the size of the array
HINT: Both of these needs to be set up as type an Object array, and then cast to a generic T array. Look up type casting.
Methods
enqueue – This method returns no value and takes a variable of type T and adds it to the last available spot in the array.
dequeue – This method removes and returns the first element in the queue
peek – This method returns the first element of the queue without removing it
showQueue – Prints out the queue in order
Create another class GenLLQueue which has the following:
Internal class ListNode which contains:
Instance variable data of type T
Instance variable link of type ListNode
Default constructor that sets both instance variables to null
Instance Variables
head which is of type ListNode which points to the first element in the queue
tail which of type ListNode which points to the last element in the queue
Constructor
A default constructor that initializes head and tail to null
Methods
enqueue – This method returns no value and takes a variable of type T and adds it after the tail. The moves to tail to point to the newly added element.
dequeue – This method removes and returns the first element in the queue
peek – This method returns the first element of the queue without removing it
showQueue – Prints out the queue in order
Example Dialog:
Testing Generic Linked List Queue
Enqueue'ing 10 numbers 0-9
Dequeue'ing all numbers and printing them out.
0
1
2
3
4
5
6
7
8
9
Testing peek
5
Testing show queue
5
0
2
4
6
8
Testing Generic Array Queue
Enqueue'ing 10 numbers 0-9
Dequeue'ing all numbers and printing them out.
0
1
2
3
4
5
6
7
8
9
Testing peek
5
Testing show queue
5
0
2
4
6
8
Explanation / Answer
package com.home.chegg;
public class GenLLQueue<T> {
private ListNode<T> head;
private ListNode<T> tail;
private Integer currSize = 0;
private static final Integer DEFAULT_SIZE = 100;
public GenLLQueue() {
}
public void enqueue(T t) {
if (head == null) {
head = new ListNode<T>(t);
tail = head;
currSize++;
} else {
if (currSize != DEFAULT_SIZE) {
ListNode<T> temp = new ListNode<T>(t);
tail.setNext(temp);
tail = temp;
currSize++;
}
}
}
public T dequeue() {
ListNode<T> firstNode = null;
if (head != null) {
firstNode = head;
head = head.getNext();
if (head == null) {
tail = null;
}
}
return (firstNode == null ? null : firstNode.getT());
}
public T peek() {
return (head != null ? head.getT() : null);
}
public void showQueue() {
if (head != null) {
ListNode<T> currNode = head;
while (currNode != null) {
System.out.println(currNode.getT());
currNode = currNode.getNext();
}
}
}
static class ListNode<T> {
public ListNode(T t2) {
this.t = t2;
}
private T t;
private ListNode<T> next;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public ListNode<T> getNext() {
return next;
}
public void setNext(ListNode<T> next) {
this.next = next;
}
}
}
package com.home.chegg;
public class GenArrayQueue<T> {
private Object[] object;
private Integer currSize = 0;
private static final Integer DEFAULT_SIZE = 100;
private Integer maxSize;
public GenArrayQueue() {
this.object = new Object[DEFAULT_SIZE];
this.maxSize = DEFAULT_SIZE;
}
public GenArrayQueue(Integer maxSize) {
this.object = new Object[maxSize];
this.maxSize = maxSize;
}
public void enqueue(T t) {
if (currSize == 0) {
object[currSize++] = t;
} else {
if (currSize != maxSize) {
object[currSize++] = t;
}
}
}
public T dequeue() {
T value = null;
if (currSize != 0) {
value = (T) object[0];
for (int i = 0; i < currSize-1; i++) {
object[i] = object[i + 1];
}
currSize--;
}
return value;
}
public T peek() {
T value = null;
if (currSize != 0) {
value = (T) object[0];
}
return value;
}
public void showQueue() {
if (currSize != 0) {
for (int i = 0; i < currSize; i++) {
System.out.println(object[i]);
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.