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() }
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
IF U TRY TO RUN THE CODES U WILL SEE AN ERROR IN STACKTEST.JAVA, AS IT ASKS FOR METHOD SHOWSTACK TO BE IMPLEMENTED IN STACKX.JAVA
Program Code [JAVA]
StackX.java
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() // shows contents of stack without deleting them
{
// Just Iterating over array and showing elements in it
for (int i=0; i<=top; i++)
System.out.print(stackArray[i] + " ");
}
//--------------------------------------------------------------
} // 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();
} // end main()
}
Sample Output:-
-------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!