I want to know if this stack is implemented properly. Previously I was told a Linked List contains an inner class that represents a node and the outta class that represents the structure. Does that same idea apply to stacks and linked list? I am assuming not because they are so simple. Here is my implementation. Let me know if this is correct.
public class Stack{
private int size;
private int[] stack;
public Stack(int height){
stack = new int[height];
size = 0;
}
public void push(int value){
if(size == 0){
stack[size] = value;
size++;
}
else if(size == stack.length){
System.out.println("The stack is full. The value cannot be added to the stack");
}
else {
stack[size] = value;
size++;
}
}
public void pop(){
if(size == 0){
System.out.println("The stack is empty. Nothing can be removed from the stack");
}
else {
size--;
System.out.println(stack[size] + " was removed from the stack");
}
}
public void peek(){
if(size == 0){
System.out.println("There is nothing to peek. The stack is empty");
}
else
System.out.println(size);
System.out.println("The value: " + stack[size -1]+ " is currently at the top of the stack");
}
public void isEmpty(){
if(size == 0)
System.out.println("The stack is empty");
else
System.out.println("The stack is not empty");
}
}
height. It is a very intuitive name for describing the maximum size of a stack. Well done :) \$\endgroup\$