Stack: Basic stack implementation using an array

Question: Write a class ‘Stack’ that has:
1. private data fields to store the stack, the top element, and the total capacity of the stack.
2. a constructor that accepts a capacity parameter and initializes the stack, sets the default value of the top element to -1, and sets the object’s capacity to the passed in parameter
3. a method to push elements into the stack
4. a method to pop elements from the stack
5. a method to return the top element of the stack (peek)
6. a method to return the length of the stack
7. a method to print the elements of the stack

package additional_problems.stack;

/**
 * ************** Stack Class **************
 *
 * -arrayStack[] : int
 * -top: int
 * -capacity: int
 *
 * ------------------------------
 *
 * +Stack(capacity: int)
 * +push(pushElement: int): void
 * +pop(): int
 * +isFull(): boolean
 * +isEmpty(): boolean
 *
 * Created by aarushi on 6/7/21.
 */
public class Stack {

    //datafields
    //arrayStack: stores the stack elements
    private int arrayStack[];
    //top: stores the index of the top element
    private int top;
    //capacity: stores the maximum capacity of the stack
    private int capacity;

    //constructor to create new stack
    public Stack (int capacity){
        arrayStack= new int[capacity];
        top=-1;
        this.capacity= capacity;
    }

    //method to push element to the stack
    public void push(int pushElement) throws Exception{
        if(isFull()){
            throw new Exception("Stack is full");
        } else {
            System.out.println(pushElement + " inserted");
            arrayStack[++top]=pushElement;
        }
    }

    //method to delete element from top of stack (pop)
    public int pop() throws Exception{
        if(isEmpty()){
            throw new Exception("Stack is empty");
        } else {
            System.out.println(arrayStack[top] + " deleted");
        }
        return arrayStack[top--];
    }

    //method to return length of stack
    public int length(){
        return (top+1);
    }

    //method to return the top element of the stack
    public int peek() throws Exception{
        if(isEmpty()){
            throw new Exception("Stack is empty");
        } else {
            return arrayStack[top];
        }
    }

    //method to check if stack is full
    public boolean isFull(){
        if(top==(capacity-1)){
            return true;
        } else {
            return false;
        }
    }

    //method to check if stack is empty
    public boolean isEmpty(){
        if(top==(-1)){
            return true;
        } else {
            return false;
        }
    }

    //method to print stack
    public void print() throws Exception{
        if(isEmpty()){
            throw new Exception("Stack is empty");
        } else {
            for (int i = 0; i <= top; i++) {
                System.out.print(arrayStack[i] + " ");
            }
            System.out.println();
        }
    }
}

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: