Vipque stack program. Must be written in java! Thank you. A VipQueueis a regular
ID: 3677001 • Letter: V
Question
Vipque stack program. Must be written in java! Thank you.
A VipQueueis a regular queue enhanced with an additional enqueue operation called anvipEnqueue, which allows an item to be added to the front of the queue. The following “main” class illustrates the use of a VipQueue of integers:
public class Tester {
public static void main(String args[]){
VipQueuevq= newVipQueue(10);
for (inti=0; i<5;i++){
if (!vq.isFull()) vq.enqueue((Integer)i); //a "regular" enqueue
if (!vq.isFull()) vq.vipEnqueue((Integer)(i*i)); //a vipenqueue
} while (!vq.isEmpty()) System.out.printf("->%d", vq.dequeue());
}
}
Executing the code above should yield output that looks like this:
->16->9->4->1->0->0->1->2->3->4
For this assignment you are to implement a VipQueueclass that supports the operations illustrated in the Tester class above. This is how you should proceed.
Implement a Stack class that supports the operations in Appendix A. This Stack should be implemented as a linked list.
Implement a Queue class that supports the operations in Appendix A. This Queue should be implemented as an array.
Implement your VipQueueclass such that each VipQueueobject is represented as a single Stack object and a single Queue object. That is, the only data members of your VipQueueclass should be one Stack, one Queue, and perhaps some constant number of additional primitive variables (integers, booleans, etc.).
You may assume that the enqueueand vipEnqueuemethods have the precondition that the vipQueue not be full, and that your dequeueoperation has the precondition that the vipQueue not be empty.
NOTE
Do not use any in-built Java implementation (e.g., Stack, Queue, Lists, ArrayList, etc) from the java.util.*
Your Stack class should support the following constructor and operations (T is a generic type):
public Stack(int capacity)
publicbooleanisEmpty()
publicbooleanisFull()
public T peek()
public T pop()
public void push(T element)
Your Queue class should support the following constructor and operations (T is a generic type):
public Queue(int capacity)
publicbooleanisEmpty() public booleanisFull()
public T peek()
public T dequeue()
public void enqueue(T element)
Your VipQueueclass should support the following constructor and operations (T is a generic type):
publicVipQueue(int capacity)
publicbooleanisEmpty()
publicbooleanisFull()
public T peek()
public T dequeue()
public void enqueue(T element)
public void vipEnqueue(Telement)
Explanation / Answer
test2.java
public class test2 {
public static void main(String args[]){
VipQueue<Integer> vq = new VipQueue<Integer>(10);
for (int i=0; i<5; i++){
if (!vq.isFull()) vq.enqueue((Integer)i); //a "regular" enqueue
if (!vq.isFull()) vq.vipEnqueue((Integer)(i*i)); //a vip enqueue
}
while (!vq.isEmpty()) System.out.printf("->%d", vq.dequeue());
}
}
Queue.java
public class Queue<T> {
private T[] queue;
private int front;
private int rear;
@SuppressWarnings("unchecked")
public Queue(int capacity){
queue = (T[])new Object[capacity + 1];
front = 0;
rear = 0;
}
public boolean isEmpty() {
return front == rear;
}
public boolean isFull() {
return front == (rear+1)%queue.length;
}
public T peek() {
return queue[front];
}
public T dequeue() {
if (isEmpty()) {
throw new RuntimeException("The queue is empty and can't dequeue.");
}
else {
T frontValue = queue[front];
front = (front + 1) % queue.length;
return frontValue;
}
}
public void enqueue(T element) {
if (isFull()) {
throw new RuntimeException("The queue is full and can't enqueue.");
}
else {
queue[rear] = element;
rear = (rear + 1) % queue.length;
}
}
}
Stack.java
public class Stack<T> {
public static class linkNode<T> {
private T data;
private linkNode<T> next;
//constructor for crating the first node
public linkNode(T data) {
this.data = data;
this.next = null;
}
//constructor for crating the next node
public linkNode(T data, linkNode<T> next) {
this.data = data;
this.next = next;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public linkNode<T> getNext() {
return next;
}
public void setNext(linkNode<T> next) {
this.next = next;
}
}
private linkNode<T> top;
private int currentSize;
private int size;
public Stack(int capacity) {
top = null;
currentSize = 0;
size = capacity;
}
public boolean isEmpty() {
if (currentSize == 0)
return true;
else
return false;
}
public boolean isFull() {
if (currentSize == size)
return true;
else
return false;
}
public T peek() {
if (isEmpty()) {
throw new RuntimeException("the stack is empty");
}
return top.getData();
}
public T pop() {
if (isEmpty()) {
throw new RuntimeException("the stack is empty");
}
T topValue = top.getData();
top = top.getNext();
currentSize --;
return topValue;
}
public void push(T element) {
if (isFull()) {
throw new RuntimeException("the stack is full");
}
linkNode<T> newNode = new linkNode<T>(element);
if (isEmpty()) {
top = newNode;
}
else {
newNode.setNext(top);
top = newNode;
}
currentSize += 1;
}
}
VipQueue.java
public class VipQueue<T> {
private Stack<T> frontStack;
private Queue<T> backQueue;
public VipQueue(int capacity) {
frontStack = new Stack<T>(capacity/2);
backQueue = new Queue<T>(capacity/2);
}
public boolean isEmpty() {
if (frontStack.isEmpty() && backQueue.isEmpty())
return true;
else
return false;
}
public boolean isFull() {
if (frontStack.isFull() && backQueue.isFull())
return true;
else
return false;
}
public T peek() {
if (isEmpty()) {
throw new RuntimeException("the VipQueue is empty");
}
else {
return frontStack.peek();
}
}
public T dequeue() {
if (isEmpty()) {
throw new RuntimeException("the VipQueue is empty");
}
else {
while (!frontStack.isEmpty()) {
return frontStack.pop();
}
return backQueue.dequeue();
}
}
public void enqueue(T element) {
if (isFull()) {
throw new RuntimeException("The VipQueue is full and can't enqueue.");
}
else {
backQueue.enqueue(element);
}
}
public void vipEnqueue(T element) {
if (isFull()) {
throw new RuntimeException("The VipQueue is full and can't enqueue.");
}
else {
frontStack.push(element);
}
}
}
sample output
->16->9->4->1->0->0->1->2->3->4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.