0
0
FKFelix Kaboto
The stack class keeps a list of integers, with the bottom of the list representing the beginning of the current line. When the vehicle begins the line, the code pushes a value of -1 to the stack. The code then pops three integers from the stack, printing the values of the integers as they are popped. If the stack is not full, the code increases the stack size to three, and then pop three integers. If the stack is full, the code displays an error message and returns.
public class Stack {
//top of the stack represents beginning of the line, represented with the value -1 which is characterized as an empty stack
public static int autostation = -1;
//array stack size is 3
public static int stack[] = new int[3];
public static void main() {
// this pushes button 3 times as vehicle begins the line
push(2);
push(1);
push(0); //vehicle begins the line
//Popping three items out of the stack
pop(); //First Testing Station
pop(); //Second Testing Station
pop(); //Third Testing Station
}
public static void push(int a){//method for push operation
if(autostation>=2){//checks if stack is full or not
System.out.println("Line heap overflow");
return;
}
//increasing top of the stack by 1
autostation++;
//inserting an item to the top of the stack
stack[autostation] = a;
}
public static void pop(){//method for pop operation
if(autostation<=-1){//checking if the stack is empty
System.out.println("Line heap underflow");
return;
}
//displaying the item being popped
System.out.println("Item popped: " + stack[autostation]);
autostation--; //decreasing the top of stack by 1
}
}