if (top == -1) { System.out.println("Stack is empty"); return -1; } else { return arr[top]; } }
public boolean isEmpty() {
return top == -1; }
public int getNthElementFromTop(int n) {
if (top - n + 1 < 0) { System.out.println("Invalid value of n"); return -1; } else { return arr[top - n + 1]; } }
public void display() {
if (top == -1) { System.out.println("Stack is empty"); } else { System.out.print("Stack elements: "); for (int i = top; i >= 0; i--) { System.out.print(arr[i] + " "); } System.out.println(); } }
public static void main(String[] args) {
System.out.println("Initialize a stack:"); Stack stack = new Stack(5); System.out.println("Input some elements on the stack:"); stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.push(5); stack.display(); int n = stack.getNthElementFromTop(2); System.out.println("\nThe 2nd element from the top is " + n); n = stack.getNthElementFromTop(3); System.out.println("\nThe 3rd element from the top is " + n); n = stack.getNthElementFromTop(4); System.out.println("\nThe 4th element from the top is " + n);