Question

In: Computer Science

Implement the \void enqueue(int val)" function in the QueueUsingStack.java/QueueUsingStack.h file (ATTACHED BELOW). Here are some hints...

Implement the \void enqueue(int val)" function in the QueueUsingStack.java/QueueUsingStack.h file (ATTACHED BELOW). Here are some hints on how to implement this using two stacks:

- Create a stack \tempStack", which has the same maximum capacity as the mainStack.

- Pop all numbers from mainStack and push them onto tempStack.

- Push the new number onto mainStack.

- Pop all numbers from tempStack and push them onto mainStack.


Implement the \int dequeue()" function in the QueueUsingStack.java/QueueUsingStack.h le. Recall that this is nothing but a pop on the mainStack.


Implement the \int getSize()" function in the QueueUsingStack.java/QueueUsingStack.h le. Recall that this is nothing but the size of mainStack.

public class QueueUsingStack {
  
Stack mainStack;
int maxQueueSize;
  
public QueueUsingStack(int maxQueueSize) {
this.maxQueueSize = maxQueueSize;
mainStack = new Stack(maxQueueSize);
}
  
public void enqueue(int val) { // complete this function
}
  
public int dequeue() { // complete this function
}
  
public int getSize() { // complete this function
}
  
public String toString() {
if (getSize() == 0) {
return "[]";
} else {
String output = "[";
int n = getSize();
for (int i = 0; i < n - 1; i++) {
int x = dequeue();
output += x + ", ";
enqueue(x);
}
int x = dequeue();
output += x + "]";
enqueue(x);
return output;
}
}
}


Solutions

Expert Solution

import java.util.Stack;

public class QueueUsingStack {

Stack mainStack;

Stack s2;

int maxQueueSize;

public QueueUsingStack(int maxQueueSize) {

this.maxQueueSize = maxQueueSize;

mainStack = new Stack();
s2 = new Stack();

}

public void enqueue(int val) { // complete this function

while (!mainStack.isEmpty()) {

s2.push(mainStack.pop());

}

mainStack.push(val);

while (!s2.isEmpty()) {

mainStack.push(s2.pop());

}

}

public int dequeue() { // complete this function

if (mainStack.isEmpty()) {

System.out.println("Queue is Empty");

return -1;

}

int answer = (int) mainStack.peek();

mainStack.pop();

return answer;

}

public int getSize() { // complete this function

return mainStack.size();

}

public String toString() {

if (getSize() == 0) {

return "[]";

} else {

String output = "[";

int n = getSize();

for (int i = 0; i < n - 1; i++) {

int x = dequeue();

output += x + ", ";

enqueue(x);

}

int x = dequeue();

output += x + "]";

enqueue(x);

return output;

}

}

}


Related Solutions

/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the function void getDigit( intnum) so that it displays the digits in a given number. For example, the number, 1234 consist of 1, 2, 3 and 4. This is an exercise to demonstate the use of a recursive function and the use of recusrion in C++ */ #include #include using namespace std; int main() {      int num;      cout <<"Enter an integer: ";     ...
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate...
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate memory for at least 3 double elements //ensure all elements have been initialized //call function, replace 0 with the proper number of elements pointed to by dd ddAns = lookUpFirstNeg(dd, 0); if(ddAns == NULL) cout << "No negatives found.\n"; else cout << "First negative value is:" << ddAns << endl; //properly deallocate memory allocated above return 0; }
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int...
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int *q ) that takes two pointers to integer variables and exchanges the values in these variables. Part 1.2 Write a function whose prototype is char lastChar ( /*in*/ const char *str ) that takes a nonempty C-String as parameter and returns the last character in the string. For example the call lastChar ("srjc") will return the character c. Part 1.3 Define an array of...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that generates and stores a specified number of random numbers each of whose values are between 0 and 1000 in an array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT