Untitled

 avatar
sangngoc
plain_text
2 months ago
996 B
2
Indexable
Never
public class StudyStack {

	
	private int maxSize;
	private int[] array;
	private int top;
	
	public StudyStack(int maxSize){
		this.maxSize = maxSize;
		this.array = new int[maxSize];
		this.top = -1;
	}
	
	public void push(int number){
		if(isFull()){
			System.out.println("Stack Overflow");
		}
		array[++top] = number;
	}
	
	public int pop() {
		if(isEmpty()){
			System.out.println("Stack empty");
			return 0;
		}
		return array[top--];
	}
	
	
	public boolean isEmpty(){
		return top == -1;
	}
	
	public boolean isFull() {
		return top == maxSize-1;
	}
	
	public int heightStack() {
		return top + 1;
	}
	
	public int peek() {
		if(isEmpty()) return 0;
		return array[top];
		
	}
	
	
	public static void main(String[] args) {
		
		StudyStack stackOne = new StudyStack(5);
		
		stackOne.pop();
		stackOne.push(10);
		stackOne.push(15);
		System.out.println(stackOne.pop());
		System.out.println(stackOne.heightStack());
	}
}



Leave a Comment