Question: Modifyyour stack implementation in such a way that a client application using your stack is unaware of the underlying array-based implementation. For instance, in a
Modifyyour stack implementation in such a way that a client application using your stack is unaware of the underlying array-based implementation. For instance, in a C or C++ implementation, you would define function prototypes in .h files and implementation details in .c files. In Java and C#,you use interfaces. The application usesthe interfaces (or .h files) with no knowledge of how the functions areimplemented. That way the array-based implementation could be replaced with a linked list-based one without any change to the client application.
here is my stack code:
public class Stack {
int size = 50; int top; int arr[]; public Stack() { top = -1; arr = new int[size]; } public void push(int value) { if (top == size) { System.out.println("Stack is full"); } else { top++; arr[top] = value; } }
public int pop(){ if (top == -1){ System.out.println("Stack is empty"); return -1; } else { top--; return arr[top + 1]; } } public int top() { if (top == -1) { System.out.println("Stack is empty"); return -1; } else{ return arr[top]; } } public int size(){ return top + 1; } public boolean isEmpty() { if (top == -1) { return true; } else { return false; } }
public boolean isFull() { if (top == size) { return true; } else { return false; } } public static void main(String args[]) { Stack cool = new Stack(); cool.push(5); cool.push(25); cool.push(50); System.out.println("The stack size is: " + cool.size()); System.out.println("The top element in the stack is: " + cool.top()); System.out.println("Is the stack empty:" + cool.isEmpty()); cool.pop(); System.out.println("The top element in the stack is: " + cool.top()); cool.pop(); System.out.println("The stack size is: " + cool.size()); System.out.println("Is the stack empty:" + cool.isEmpty()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
