In: Computer Science
You may not use the java.util.Stack
class.
You may not use the java.util.Queue class.
Write method showStack(): It displays the contents of the stack
starting with the first inserted element to the last. The stack
would contain the same elements after showStack(). . If for example
if to an empty stackx we push(20) then push(30) showStack() would
print 20 30
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// stack.java
// demonstrates stacks
// to run this program: C>java StackApp
////////////////////////////////////////////////////////////////
class StackX
{
private int maxSize; // size of stack array
private long[] stackArray;
private int top; // top of stack
//--------------------------------------------------------------
public StackX(int s) // constructor
{
maxSize = s; // set array size
stackArray = new long[maxSize]; // create array
top = -1; // no items yet
}
//--------------------------------------------------------------
public void push(long j) // put item on top of stack
{
stackArray[++top] = j; // increment top, insert item
}
//--------------------------------------------------------------
public long pop() // take item from top of stack
{
return stackArray[top--]; // access item, decrement top
}
//--------------------------------------------------------------
public long peek() // peek at top of stack
{
return stackArray[top];
}
//--------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
//--------------------------------------------------------------
public boolean isFull() // true if stack is full
{
return (top == maxSize-1);
}
//--------------------------------------------------------------
} // end class StackX
////////////////////////////////////////////////////////////////
class StackApp
{
public static void main(String[] args)
{
StackX theStack = new StackX(10); // make new stack
theStack.push(20); // push items onto stack
theStack.push(40);
theStack.push(60);
theStack.push(80);
while( !theStack.isEmpty() ) // until it's empty,
{ // delete item from stack
long value = theStack.pop();
System.out.print(value); // display it
System.out.print(" ");
} // end while
System.out.println("");
} // end main()
} // end class StackApp
////////////////////////////////////////////////////////////////
TEST WITH, DONT CHANGE ANYTHING IN STACKTEST.JAVA
public class StackTest { public static void main(String[] args) { StackX theStack = new StackX(10); // make new stack theStack.push(20); // push items onto stack theStack.push(30); theStack.push(40); theStack.push(40); theStack.push(60); theStack.push(80); theStack.showStack(); System.out.println("removeDownTo(40)"); theStack.removeDownTo(40); theStack.showStack(); } // end main() }
StackX.java
// stack.java
// demonstrates stacks
// to run this program: C>java StackApp
class StackX {
private int maxSize; // size of stack array
private long[] stackArray;
private int top; // top of stack
public StackX(int s) // constructor
{
maxSize = s; // set array size
stackArray = new long[maxSize]; // create array
top = -1; // no items yet
}
public void push(long j) // put item on top of stack
{
stackArray[++top] = j; // increment top, insert item
}
public long pop() // take item from top of stack
{
return stackArray[top--]; // access item, decrement top
}
public long peek() // peek at top of stack
{
return stackArray[top];
}
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
public boolean isFull() // true if stack is full
{
return (top == maxSize - 1);
}
public void showStack() {
//taking temporary stack
StackX temporaryStack = new StackX(maxSize);
//until current stack is empty we are pushing elements to our
temporary stack
while(!this.isEmpty()){
temporaryStack.push(this.pop());
}
//displaying our temporary stack which is a reverse of stack
while(!temporaryStack.isEmpty()){
long x = temporaryStack.pop();
System.out.print(x + " ");
//adding again to original stack
this.push(x);
}
}
} // end class StackX
StackTest.java
public class StackTest {
public static void main(String[] args) {
StackX theStack = new StackX(10); // make new stack
theStack.push(20); // push items onto stack
theStack.push(30);
theStack.push(40);
theStack.push(40);
theStack.push(60);
theStack.push(80);
theStack.showStack();
// System.out.println("removeDownTo(40)");
//theStack.removeDownTo(40);
//theStack.showStack();
} // end main()
}