
e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } }
f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex 19 public Queue() { super(); } public Queue enqueue(E pData) { push(pData); return this; } public E dequeue() { return pop(); } public E peek() { return peek(); } }
Implementation of Option B is the correct one.
DList.java
import java.util.LinkedList;
public class DList<E> {
//intialize the list
LinkedList<E> list = new LinkedList<>();
//get the size of the list
public int getSize(){
return list.size();
}
//remove the elment from the list
public E remove(int index){
return list.remove(index);
}
//set the element at index
public void set(int index, E value){
list.set(index,value);
}
//get the elemnt from the index
public E get(int index){
return list.get(index);
}
}
Queue.java
public class Queue<E> {
private DList<E> mList;
public Queue(){
setList(new DList<>());
}
protected int indexOfFromt(){
return 0;
}
protected int indexOfRear(){
return getList().getSize() - 1;
}
public Queue<E> enqueue(E pData){
getList().set(indexOfFromt(),pData);
return this;
}
public E dequeue(){
E value = getList().remove(indexOfRear());
return value;
}
public E peek(){
E value = getList().get(indexOfFromt());
return value;
}
private DList<E> getList(){
return mList;
}
private void setList(DList<E> pList){
mList = pList;
}
}
e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...
JAVA PROGRAM: public class Stack { private DList mList; public Stack() { setList(new DList<>()); } private int indexOfTop() { return getList().isEmpty() ? -1 : getList().getSize() - 1; } public E peek() { return getList().get(indexOfTop()); } public E pop() { return getList().remove(indexOfTop()); } public Stack push(E pData) { getList().append(pData); return this; } private DList getList() { return mList; } private void setList(DList pList) { mList = pList; } Q1 Because Stack encapsulates an instance variable which is of the class DList,...
AQueue.java
class AQueue implements Queue {
private E queueArray[]; // Array holding queue elements
private static final int DEFAULT_SIZE = 10;
private int maxSize; // Maximum size of queue
private int front; // Index of front element
private int rear; // Index of rear element
// Constructors
@SuppressWarnings("unchecked") // Generic array allocation
AQueue(int size) {
//BUG #1: maxSize = size
maxSize = size+1; // One extra space is allocated
rear = 0; front = 1;
queueArray = (E[])new Object[maxSize]; //...
Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To get credit, DO NOT add or alter any data members/methods except the enqueue(int num). The NonZeroNumQueue will NOT accept zero, which mean you can enqueue any whole numbers EXCEPT 0(zero). You have already noticed that the queue is NEVER full, so the isFull() always returns false. given NonZeroNumQueue.txt public class NonZeroNumQueue{ private int[] data; private int total; private int front; private int tail; public...
public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count; } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...
Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the ADT -----ArrayQueue.java------- public class ArrayQueue { private static final int INITIAL_CAPACITY = 2; // to permit easier testing private Object[] contents; private int front, rear; /** * Create an empty queue with an initial capacity. */ public ArrayQueue() { contents = new Object[INITIAL_CAPACITY]; } /** * Add an element to...
help finish Queue, don't think
I have the right thing.
# 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...
Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: QueueNode<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Task: makes...
2. Consider a circular array based Queue as we have discussed in the lectures (class definition given below for reference) public class CircArrayQueue<E> implements Queue<E private EI Q private int front-0 indicates front of queue l indicates position after end of queue private int end-0: public CircArrayQueue( public int getSize (.. public boolean isEmpty ( public void enqueue (E e)... public E dequeue ) throws EmptyQueueException... Il constructor We are interested in implementing a Stack class based on the above...
Create a CircularArrayQueue<E> implementation of the Queue<E> interface defined in class. Hints: You will need two index variables to keep track of the start and the end of the queue. Consider the queue to be empty when the front/back index pointers are equal (which means that the 'back' pointer will always be pointing at the next place to add an item). Be careful of index increments and decrements; make sure you perform the operations necessary to make the pointers loop...
public class CharNode{ private CharNode link; private char info; public CharNode(char info) { this.info = info; this.link = null; } public CharNode(char c, CharNode link) { this.info = info; this.link = link; } public CharNode getLink() { return link; } public void setLink(CharNode link) { this.link = link; } public char getInfo() { return info; } public void setInfo(char info) { this.info = info; } } //Remember Queue is a first-in-first-out data structure public class CharQueue { // front is...