class myStack {
    //push
    //pop
    //peek
    //isEmpty
    static final int max = 100;
    int top;
    int[] nums = new int[max];
    myStack() {
        this.top = -1;
    }
    void push(int number) {
        if (top >= max) {
            System.out.println("The stack is full.");
            return;
        }else {
            nums[++top] = number;
        }
    }
    int pop() {
        if (top < 0) {
            return -1;
        }else {
            int ans = nums[top--];
            return ans;
        }
    }
    int peek(){
        if(top < 0) {
            return -1;
        }else {
            return nums[top];
        }
    }
    boolean isEmpty() {
        return top < 0;
    }
}
class Main {
    public static void main(String[] args) {
        Stack s : new Stack();
        s.push(10);
        s.push(20);

    }
}

c++

#includes <bits/stdc++.h>
using namespace std;
#define MAX 1000
class Stack{
    int top;
public:
    int nums[MAX];
    Stack() {
        top = -1;
    }
    bool push(int x);
    int pop();
    bool isEmpty();
};
bool Stack::push(int x) {
    if (top >= MAX) {
        cout<<"Stack overflow";
        return false;
    }else {
        a[++top] = x;
        return true;
    }
}
int Stack::pop() {
    if (top < 0) {
        cout << "Stack underflow";
        return 0;
    } else {
        int x = a[top--];
        return x;
    }
}
bool Stack::isEmpty() {
    return (top < 0);
}

results matching ""

    No results matching ""