Complete the code below, test cases to use are at the bottom
package Labs;
public class ResizingQueue {
private final int INITIAL_SIZE = 10;
private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0]
// and rear at A[next-1]
int next = 0; // location of next available unused slot
// interface methods
public void enqueue(int key) {
//TODO: push the key onto the back of the queue
// YOUR CODE HERE!
}
private void resize() {
// this method resizes an array A to twice as big:
// TODO: create a new array B twice as big as A, transfer everything from A over to the same slots
// in B (i.e. A[0] goes to B[0], A[1] goes to B[1] ect)
// YOUR CODE HERE!
}
public int dequeue() {
// TODO: remove the top integer and return it -- return 'Integer.MIN_VALUE' if queue is empty
//YOUR CODE HERE!
}
public boolean isEmpty() {
return (next == 0);
}
public int size() {
// TODO: how many integers in the queue
return next;
}
public String toString() {
String s = "[";
for(int i = 0; i < A.length; ++i) {
if(i == next)
s += " | " + A[i];
else if(i == 0)
s += A[i];
else
s += ", " + A[i];
}
s += "]";
return s;
}
public static void main(String[] args) {
}
}
Output of test cases should be the following:
public class lab8 {
public static void main(String[] args) {
ResizingQueue Q = new ResizingQueue();
System.out.println("\nTesting toString on empty queue....");
System.out.println("\n[1] Should be:\n[ | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\tnext = 0");
System.out.println(Q + "\tnext = 0");
System.out.println("\nTesting size and isEmpty() on empty queue....");
System.out.println("\n[2] Should be:\n0\ttrue");
System.out.println(Q.size() + "\t" + Q.isEmpty());
// UNCOMMENT THIS CODE TO TEST YOUR FUNCTIONS ENQUEUE, DEQUEUE, and RESIZE
/*
System.out.println("\nTesting enqueue...");
Q.enqueue(3);
Q.enqueue(5);
Q.enqueue(7);
System.out.println("\n[3] Should be:\n[3, 5, 7 | 0, 0, 0, 0, 0, 0, 0]\tnext = 3");
System.out.println(Q + "\tnext = " + Q.next);
System.out.println("\nTesting size and isEmpty on non-empty queue....");
System.out.println("\n[4] Should be:\n3\tfalse");
System.out.println(Q.size() + "\t" + Q.isEmpty());
System.out.println("\nTesting dequeue...");
int n = Q.dequeue();
System.out.println("\n[5] Should be:\n[5, 7 | 7, 0, 0, 0, 0, 0, 0, 0]\tnext = 2\tQ.dequeue() => 3");
System.out.println(Q + "\tnext = " + Q.next + "\tQ.dequeue() => " + n);
n = Q.dequeue();
n = Q.dequeue();
System.out.println("\n[6] Should be:\n[ | 7, 7, 7, 0, 0, 0, 0, 0, 0, 0]\tnext = 0\tQ.dequeue() => 7");
System.out.println(Q + "\tnext = " + Q.next + "\tQ.dequeue() => " + n);
System.out.println("\nTesting resizing...");
for(int i = 10; i <= 25; ++i)
Q.enqueue(i);
System.out.println("\n[7] Should be:\n[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 | 0, 0, 0, 0]");
System.out.println(Q);
for(int i = 0; i < 13; ++i)
Q.dequeue();
System.out.println("\n[8] Should be:\n[23, 24, 25 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 0, 0, 0, 0]");
System.out.println(Q);
*/
//package Labs;
public class ResizingQueue {
private final int INITIAL_SIZE = 10;
private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0]
// and rear at A[next-1]
int next = 0; // location of next available unused slot
private int CAPACITY = INITIAL_SIZE;
// interface methods
public void enqueue(int key) {
//TODO: push the key onto the back of the queue
// YOUR CODE HERE!
if(this.next < CAPACITY)
{
//A[next++] = key;
}
else
{
resize();
}
A[next++] = key;
}
private void resize() {
// this method resizes an array A to twice as big:
// TODO: create a new array B twice as big as A, transfer everything from A over to the same slots
// in B (i.e. A[0] goes to B[0], A[1] goes to B[1] ect)
// YOUR CODE HERE!
int NEW_INITIAL_SIZE = CAPACITY *2;
if(A.length > 0)
{
int [] B = new int[NEW_INITIAL_SIZE];
int [] temp = new int[NEW_INITIAL_SIZE];
for(int i = 0;i < A.length; i++)
{
temp[i] = A[i];
}
A = null;
A = new int[NEW_INITIAL_SIZE];
for(int i = 0;i < temp.length; i++)
{
A[i] = temp[i];
}
CAPACITY = NEW_INITIAL_SIZE;
}
/*else
{
}
*/
}
public int dequeue() {
// TODO: remove the top integer and return it -- return 'Integer.MIN_VALUE' if queue is empty
//YOUR CODE HERE!
int [] B = new int[A.length];
int val = 0;
if(next > 0)
{
//int [] B = new
int[A.length-1];
//val = A[next-1];
val = A[0];
for(int j = 0, k = 0;j <
B.length && k<=this.next -2; j++,k++)
{
B[j] =
A[k+1];
}
A = null;
A = B;
this.next--;
}
else
{
val = Integer.MIN_VALUE;
}
return val;
}
public boolean isEmpty() {
return (next == 0);
}
public int size() {
// TODO: how many integers in the queue
return next;
}
public String toString() {
String s = "[";
for(int i = 0; i < A.length; ++i) {
if(i == next)
s += " | " + A[i];
else if(i == 0)
s += A[i];
else
s += ", " + A[i];
}
s += "]";
return s;
}
/*
public static void main(String[] args) {
}
*/
}
public class lab8 {
public static void main(String[] args) {
ResizingQueue Q = new ResizingQueue();
System.out.println("\nTesting toString on empty queue....");
System.out.println("\n[1] Should be:\n[ | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\tnext = 0");
System.out.println(Q + "\tnext = 0");
System.out.println("\nTesting size and isEmpty() on empty queue....");
System.out.println("\n[2] Should be:\n0\ttrue");
System.out.println(Q.size() + "\t" + Q.isEmpty());
// UNCOMMENT THIS CODE TO TEST YOUR FUNCTIONS ENQUEUE, DEQUEUE, and RESIZE
System.out.println("\nTesting enqueue...");
Q.enqueue(3);
Q.enqueue(5);
Q.enqueue(7);
System.out.println("\n[3] Should be:\n[3, 5, 7 | 0, 0, 0, 0, 0, 0, 0]\tnext = 3");
System.out.println(Q + "\tnext = " + Q.next);
System.out.println("\nTesting size and isEmpty on non-empty queue....");
System.out.println("\n[4] Should be:\n3\tfalse");
System.out.println(Q.size() + "\t" + Q.isEmpty());
System.out.println("\nTesting dequeue...");
int n = Q.dequeue();
System.out.println("\n[5] Should be:\n[5, 7 | 7, 0, 0, 0, 0, 0, 0, 0]\tnext = 2\tQ.dequeue() => 3");
System.out.println(Q + "\tnext = " + Q.next + "\tQ.dequeue() => " + n);
n = Q.dequeue();
n = Q.dequeue();
System.out.println("\n[6] Should be:\n[ | 7, 7, 7, 0, 0, 0, 0, 0, 0, 0]\tnext = 0\tQ.dequeue() => 7");
System.out.println(Q + "\tnext = " + Q.next + "\tQ.dequeue() => " + n);
System.out.println("\nTesting resizing...");
for(int i = 10; i <= 25; ++i)
Q.enqueue(i);
System.out.println("\n[7] Should be:\n[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 | 0, 0, 0, 0]");
System.out.println(Q);
for(int i = 0; i < 13; ++i)
Q.dequeue();
System.out.println("\n[8] Should be:\n[23, 24, 25 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 0, 0, 0, 0]");
System.out.println(Q);
}
}
//Output of test cases should be the following:
Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...
Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...
HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD
COMMENTS:
stackARRAY:
#include<iostream>
#define SIZE 100
#define NO_ELEMENT -999999
using namespace std;
class Stack {
int arr[SIZE]; // array to store Stack elements
int top;
public:
Stack() {
top = -1;
}
void push(int); // push an element into Stack
int pop(); // pop the top element from Stack
int topElement(); // get the top element
void display(); // display Stack elements from top to bottom
};
void Stack...
Study the "Queue as linked list simple example" posted in this module. Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. LListQueue.cpp ///--------------------------------------------------------------- /// File: LListQueue.cpp /// Purpose: Implementation file for a demonstration of a queue /// implemented as an array. Data type: Character /// Programming Language: C++ ///--------------------------------------------------------------- #include "LListQueue.h" ///-------------------------------------------- /// Function: LListQueue() ///...
lab 11 Do not change main.cpp, i need c++ code for queue.h and queue.cpp Given the complete main() function, partial queue class header queue.h, and queue.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor enqueue() dequeue() You may elect to create the following helper functions: isFull() isEmpty() A description of these ADT operations are available in this Zybook and in the textbook's chapter 17. Example: If the input is: 3 Led Zepplin...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...
In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods also listed below: Data Members: Declare and initialize, as needed, the data item(s) you will need to manage a queue of integers. You may only use arrays and primitives for your instance and/or static variables (I,e You can’t use Java defined Queue / Stack / List...
Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...
How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...
how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...
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...