Java - BJP3 Exercise 14.2: stutter Write a method stutter that takes a stack of
ID: 654562 • Letter: J
Question
Java - BJP3 Exercise 14.2: stutter
Write a method stutter that takes a stack of integers as a parameter and replaces every value in the stack with two occurrences of that value. For example, suppose the stack stores these values:
Then the stack should store these values after the method terminates:
Notice that you must preserve the original order. In the original list the 9 was at the top and would have been popped first. In the new stack the two 9s would be the first values popped from the stack. You may use a single queue as auxiliary storage to solve this problem.
Explanation / Answer
public class MyStack {
private int maxSize;
private long[] buttom;
int top;
public MyStack(int s) {
maxSize = s;
buttom = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
}
public long pop() {
return stackArray[top--];
}
public long peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args) {
int aa[] = new int[10];
int i=0;
buttom theStack = new buttom(10);
theStack.push(10);
theStack.push(20);
theStack.push(30);
theStack.push(40);
theStack.push(50);
while (!theStack.isEmpty()) {
aa[i]= theStack.pop();
i++;
}
for(i=theStack.top;i>0;i--)
{
theStack.push(aa[i]);
theStack.push(aa[i]);
}
for(i=0;1<10;i++)
System.out.println(theStack.peek());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.