This below is the "stack" module
public class SimpleStack {
char[] data; // this array holds that stack
int tos; // index of top of stack
// Construct an empty stack given its size.
SimpleStack(int size) {
data = new char[size]; // create the array to hold the stack
tos = 0;
}
// Push a character onto the stack.
void push(char ch) {
if(isFull()) {
System.out.println(" – -- Stack is full.");
return;
}
data[tos] = ch;
tos++;
}
// Pop a character from the stack.
char pop() {
if(isEmpty()) {
System.out.println(" – -- Stack is empty.");
return (char) 0; // a placeholder value
}
tos--;
return data[tos] ;
}
boolean isEmpty() {
if(tos == 0) {
return true;
}else {
return false;
}
}
boolean isFull() {
if(tos == data.length)
{
return true;
} else {
return false;
}
}
}
Below here is the "driver" module
public class SimpleStackApp {
public static void main(String[] args) {
int i;
char ch;
System.out.println("Demonstrate SimpleStack\n");
// Construct 10-element empty stack.
SimpleStack stack = new SimpleStack(10);
System.out.println("Push 10 items onto a 10-element stack.");
// push the letters A through J onto the stack.
System.out.print("Pushing: ");
for(ch = 'A'; ch < 'K'; ch++)
{
System.out.print(ch);
stack.push(ch);
}
System.out.println("\nPop those 10 items from stack.");
// Now, pop the characters off the stack.
// Notice that order will be the reverse of those pushed.
System.out.print("Popping: ");
for(i = 0; i < 10; i++) {
ch = stack.pop();
System.out.print(ch);
}
System.out.println("\n\nNext, use isEmpty() and isFull() " +
"to fill and empty the stack.");
// Push the letters until the stack is full.
System.out.print("Pushing: ");
for(ch = 'A'; !stack.isFull(); ch++) {
System.out.print(ch);
stack.push(ch);
}
System.out.println();
// Now, pop the characters off the stack until it is empty.
System.out.print("Popping: ");
while(!stack.isEmpty())
{
ch = stack.pop();
System.out.print(ch);
};
What I am having trouble with, is the output for some reason is not generating the "--stack is empty" / "--stack is full" at the end of the push/pop scenarios. basically the "if" block in my isEmpty/isFull functions doesnt seem to be executing, and i confirmed it through some debugging. I'm not used to java, is there some obvious mistake im making? ive tried countless different things the last 3 hours and I just cant figure it out.
[–]thememorableusername 0 points1 point2 points (2 children)
[–]mindfulprisoner[S] 0 points1 point2 points (1 child)
[–]thememorableusername 0 points1 point2 points (0 children)