In: Computer Science
This is my current code for a question that asks: "Implement the stack methods push(x) and pop() using two queues". It works well, other than I want it to work for data of type T, not just Int's. (EXCUSE THE INDENTING... CHEGG POSTING DOESNT ALLOW PROPER CODE TO BE UPLOADED... JUST COPY PASTE IT INTO ECLIPSE IDE, HIGHLIGHT ALL CODE, PRESS COMMAND+SHIFT+F AND SHOULD AUTO-FORMAT)
MY QUESTION: How could one modify this code in order to take data of type T, rather than only Int's?
PS: the code must continue to use the data structure provided.... must use the Queue class built... (can't use built-in ArrayList from Java).
HERE IS MY CODE CURRENTLY
//author: Nicolas Wills
package ass1Q1B;
public class StackUsingTwoQueues {
public Queue queue1 = new Queue();
public Queue queue2 = new Queue();
// public int s = queue1.size()+queue2.size(); //dont really use this
// variable... ignore it
public boolean inQueue1 = false; // inQueue1 is true when elements are in queue1. set to false to start means
// first add(x) will add to queue1 first
public void push(int x) { // queue1 holds data when there's an odd number of elements, queue2 holds data
// when even number of elements
if (!inQueue1) { // elements not in queue1, therefore will push to queue1, then transfer queue2
// elements to queue1
queue1.add(x); // add pushed element to previously empty queue1
int queue2StartSize = queue2.size(); // use this variable for "for-loop"... cant put "for(int i=0;
// i
// changes the return value of queue2.size().
for (int i = 0; i < queue2StartSize; i++) {
try {
queue1.add(queue2.remove()); // remove beginning element of queue1, add it to queue2
} catch (NoSuchElementException e) {
}
;
} // end for loop
inQueue1 = true;
} // end else if
else if (inQueue1) { // previous push() added to queue1, therefore add "x" to queue2.
queue2.add(x); // add pushed element to previously empty queue2
int queue1StartSize = queue1.size(); // use this variable for "for-loop"... cant put "for(int i=0;
// i
// change the return value of queue1.size().
for (int i = 0; i < queue1StartSize; i++) {
try {
queue2.add(queue1.remove()); // remove beginning element of queue1, add it to queue2
} catch (NoSuchElementException e) {
}
;
} // end for loop
inQueue1 = false;
} // end else if
}// end push method
// the running time of push O(n+1) time. n for the loop, 1 for the resize()
// operation
public Integer pop() {
Integer x = null; // very sketchy way to get around error
try {
if (inQueue1) {
x = queue1.remove(); // if the elements are currently in queue1, remove first element from queue1
} else {
x = queue2.remove(); // if the elements are currently in queue2, remove first element from queue2
}
} catch (NoSuchElementException e) {
}
;
return x;
}// end pop() method
// the running time of the pop() method is O(1)
public String printArray() {
if (inQueue1) {
return (queue1.stringedArray()); // if the elements are currently in queue1, printArray() will return a
// string version of the contents of queue1
} else
return (queue2.stringedArray()); // if the elements are currently in queue2, printArray() will return a
// string version of the contents of queue2
}
public static void main(String[] args) {
StackUsingTwoQueues myStack = new StackUsingTwoQueues();
// push elements
myStack.push(1);
System.out.println(myStack.printArray());
myStack.push(2);
System.out.println(myStack.printArray());
myStack.push(3);
System.out.println(myStack.printArray());
myStack.push(4);
System.out.println(myStack.printArray());
myStack.push(5);
System.out.println(myStack.printArray());
myStack.push(6);
System.out.println(myStack.printArray());
myStack.push(7);
System.out.println(myStack.printArray());
myStack.push(8);
System.out.println(myStack.printArray());
myStack.push(9);
System.out.println(myStack.printArray());
myStack.push(10);
System.out.println(myStack.printArray());
// pop elements
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
}
}
package ass1Q1B;
//import ass1Q1.illegalOperation;
//import ass1Q1.priorityQ;
public class Queue {
int[] arr = new int[10];
int j=0; // keeps track of index of front of array
int n = 0; //number of elements in array
public int size() {
return n;
}
public boolean add(int x) { // why cant i just return void
if ((n + 1) > arr.length)
resize();
arr[(j + n) % arr.length] = x;
n++;
return true;
}
private void resize() { // only function in queue class that can be private right?
int[] b = new int[(Math.max(1, n * 2))];
for (int k = 0; k < n; k++)
b[k] = arr[(j + k) % arr.length];
arr = b;
j = 0;
}
public int remove() throws NoSuchElementException {
if (n == 0)
throw new NoSuchElementException("cant remove if theres no elements to remove");
int x = arr[j];
j = (j + 1) % arr.length;
n--;
if (arr.length >= 3 * n)
resize();
return x;
}
public String stringedArray() {
String returnString = "";
for(int i=0; i
returnString+= arr[(i+j)%(arr.length)];
}
return returnString;
}
}
// Queue.java
package ass1Q1B;
import java.util.NoSuchElementException;
public class Queue<T> {
T[] arr = (T[])(new Object[10]); //
change it to generic type
int j=0; // keeps track of index of
front of array
int n = 0; //number of elements in
array
public int size() {
return n;
}
// change the input parameter type
to generic
public boolean add(T x) { // why
cant i just return void
if ((n + 1)
> arr.length)
resize();
arr[(j + n) %
arr.length] = x;
n++;
return
true;
}
private void resize() { // only
function in queue class that can be private right?
T[] b = (T[])(new Object[(Math.max(1, n * 2))]);
for (int k =
0; k < n; k++)
b[k] = arr[(j + k) % arr.length];
arr =
b;
j = 0;
}
// change return data type to
generic
public T remove() throws
NoSuchElementException {
if (n ==
0)
throw new NoSuchElementException("cant remove if
theres no elements to remove");
T x =
arr[j];
j = (j + 1) %
arr.length;
n--;
if
(arr.length >= 3 * n)
resize();
return x;
}
public String stringedArray() {
String returnString = "";
for(int i=0;
i<n ;i++) {
returnString+= arr[(i+j)%(arr.length)];
}
return
returnString;
}
}
//end of Queue.java
// StackUsingTwoQueues.java
package ass1Q1B;
import java.util.NoSuchElementException;
public class StackUsingTwoQueues<T> {
// generic queue
public Queue<T> queue1 = new
Queue<>();
public Queue<T> queue2 = new
Queue<>();
// public int s = queue1.size()+queue2.size(); //dont
really use this
// variable... ignore it
public boolean inQueue1 = false; // inQueue1 is true
when elements are in queue1. set to false to start means
// first add(x) will add to queue1 first
// change the data type of the input parameter to
generic
public void push(T x) { // queue1 holds data when
there's an odd number of elements, queue2 holds data
// when even number of
elements
if (!inQueue1) { // elements not in
queue1, therefore will push to queue1, then transfer queue2
// elements to
queue1
queue1.add(x);
// add pushed element to previously empty queue1
int
queue2StartSize = queue2.size(); // use this variable for
"for-loop"... cant put "for(int i=0;
// i
// changes
the return value of queue2.size().
for (int i = 0;
i < queue2StartSize; i++) {
try {
queue1.add(queue2.remove());
// remove beginning element of queue1, add it to queue2
} catch (NoSuchElementException e) {
}
} // end for loop
inQueue1 =
true;
} // end else if
else if (inQueue1) { // previous
push() added to queue1, therefore add "x" to queue2.
queue2.add(x);
// add pushed element to previously empty queue2
int
queue1StartSize = queue1.size(); // use this variable for
"for-loop"... cant put "for(int i=0;
// i
// change the
return value of queue1.size().
for (int i = 0;
i < queue1StartSize; i++) {
try {
queue2.add(queue1.remove());
// remove beginning element of queue1, add it to queue2
} catch (NoSuchElementException e) {
}
} // end for
loop
inQueue1 =
false;
} // end else if
}// end push method
// the running time of push O(n+1) time. n for the
loop, 1 for the resize()
// operation
// change the data type of return to generic
public T pop() {
T x = null; // very sketchy way to
get around error
try {
if (inQueue1)
{
x = queue1.remove(); // if the elements are
currently in queue1, remove first element from queue1
} else {
x = queue2.remove(); // if the elements are
currently in queue2, remove first element from queue2
}
} catch (NoSuchElementException e)
{
}
return x;
}// end pop() method
// the running time of the pop() method is O(1)
public String printArray() {
if (inQueue1) {
return
(queue1.stringedArray()); // if the elements are currently in
queue1, printArray() will return a
// string
version of the contents of queue1
} else
return
(queue2.stringedArray()); // if the elements are currently in
queue2, printArray() will return a
// string version of the contents
of queue2
}
public static void main(String[] args) {
StackUsingTwoQueues<Integer>
myStack = new StackUsingTwoQueues<>();
// push elements
myStack.push(1);
System.out.println(myStack.printArray());
myStack.push(2);
System.out.println(myStack.printArray());
myStack.push(3);
System.out.println(myStack.printArray());
myStack.push(4);
System.out.println(myStack.printArray());
myStack.push(5);
System.out.println(myStack.printArray());
myStack.push(6);
System.out.println(myStack.printArray());
myStack.push(7);
System.out.println(myStack.printArray());
myStack.push(8);
System.out.println(myStack.printArray());
myStack.push(9);
System.out.println(myStack.printArray());
myStack.push(10);
System.out.println(myStack.printArray());
// pop elements
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
myStack.pop();
System.out.println(myStack.printArray());
}
}
//end of StackUsingTwoQueues.java
Output: