package Stack;
public class Stack_Array {
int[] arr;
int topOfStack;
//Create Stack TC:O(1) SC:O(N)
public Stack_Array(int size) {
[Link] = new int[size];
[Link] = -1;
[Link]("The stack is created with size of: " +
size);
}
//isEmpty TC:O(1) SC:O(1)
public boolean isEmpty() {
if (topOfStack == -1) {
return true;
} else {
return false;
}
}
//isFull
public boolean isFull() {
if (topOfStack == [Link] - 1) {
[Link]("The Stack is full! ");
return true;
} else {
return false;
}
}
// PUSH TC:O(1) SC:O(1)
public void push(int value) {
if (isFull()) {
[Link]("The stack is full! ");
} else {
arr[topOfStack + 1] = value;
topOfStack++;
[Link]("The value is successfully inserted
" + value);
}
}
//pop TC:O(1) SC:O(1)
public int pop() {
if (isEmpty()) {
[Link]("The stack is empty ");
return -1;
} else {
int topStack = arr[topOfStack];
topOfStack--;
return topStack;
}
}
// Peek Method TC:O(1) SC:O(1)
public int peek() {
if (isEmpty()) {
[Link]("The Stack is empty ");
return -1;
} else {
return arr[topOfStack];
}
}
// Delete method TC:O(1) SC:O(1)
public void deleteStack() {
arr = null;
[Link]("The stack is successfully deleted ");
}
public static void main(String[] args) {
Stack_Array sa = new Stack_Array(4);
boolean result = [Link]();
[Link](result);
boolean result1 = [Link]();
[Link](result1);
[Link](1);
[Link](2);
[Link](3);
[Link](4);
int result3 = [Link]();
[Link]("The pop element is: " + result3);
int result4 = [Link]();
[Link]("The peek element is: " + result4);
[Link]();
}
}
OUTPUT: