# Stack Using Array

<details>

<summary>Stack implementation using STL</summary>

<pre class="language-cpp"><code class="lang-cpp"><strong>#include &#x3C;iostream>
</strong>#include &#x3C;stack>
using namespace std;

int main()
{
      // stack container declaration
	stack&#x3C;int> s1;
	stack&#x3C;int> s2;
	
	// pushing elements into first stack
	s1.push(1);
	s1.push(2);
	s1.emplace(3);
	s1.push(4);
	
	// pushing elements into 2nd stack
	s2.push(3);
	s2.emplace(5);
	s2.push(7);
	s2.push(9);
    
        cout &#x3C;&#x3C; "Size of the Stack is: " &#x3C;&#x3C; s1.size() &#x3C;&#x3C; ", " &#x3C;&#x3C; s2.size() &#x3C;&#x3C; endl;

        cout &#x3C;&#x3C; "Top Elements of both stacks are: " &#x3C;&#x3C; s1.top() &#x3C;&#x3C; ", " &#x3C;&#x3C; s2.top()&#x3C;&#x3C; endl; 

        // using swap() function to swap elements of stacks
        s1.swap(s2);

        cout &#x3C;&#x3C; "After Swapping both stacks!!" &#x3C;&#x3C; endl; 
        // printing the first stack
        cout&#x3C;&#x3C;"s1 = ";
	while (!s1.empty()) {
		cout&#x3C;&#x3C;s1.top()&#x3C;&#x3C;" ";
		s1.pop(); // deleting
	}

	// printing the second stack
	cout&#x3C;&#x3C;endl&#x3C;&#x3C;"s2 = ";
	while (!s2.empty()) {
		cout&#x3C;&#x3C;s2.top()&#x3C;&#x3C;" ";
		s2.pop(); // deleting
	}
	return 0;
}
</code></pre>

</details>

<details>

<summary><a href="https://www.codingninjas.com/studio/problems/stack-implementation-using-array_3210209?leftPanelTab=1">Stack Implementation using arrays</a></summary>

```cpp
// Stack class.
class Stack {
    int *arr;
    int qtop;
    int size;
public:
    
    Stack(int capacity) {
        size = capacity;
        arr = new int [size] ;
        qtop = -1;
    }

    void push(int num) {
        if(qtop != size-1) 
            arr[++qtop] = num;
    }

    int pop() {
        if(isEmpty()) {
            return -1;
        }
        int ans = arr[qtop];
        qtop--;
       return ans;
    }
    
    int top() {
        if(isEmpty())
            return -1;
        return arr[qtop];
    }
    
    int isEmpty() {
        return qtop == -1;
    }
    
    int isFull() {
        
        return qtop == size-1; 
    }
    
};

```

</details>

<details>

<summary>Easy to Understand</summary>

```cpp
#include<iostream>
#include<stack>
using namespace std;

class Stack {
    //properties
    public:
        int *arr;
        int top;
        int size;

    // behaviour
    Stack(int size) {
        this -> size = size;
        arr = new int[size];
        top = -1;
    }

    void push( int element) {
        if(size - top > 1) {
            top++;
            arr[top] = element;
        }
        else{
            cout << "Stack OverFlow" << endl;
        }
    }

    void pop() {
        if(top >=0 ) {
            top--;
        }
        else{
            cout << "Stack UnderFlow" << endl;
        }
    }

    int peek() {
        if(top >=0 )
            return arr[top];
        else
        {
            cout << "Stack is Empty" << endl;
            return -1;
        }
    }

    bool isEmpty() {
        if( top == -1) {
            return true;
        }
        else{
            return false;
        }
    }

};


int main() {


    Stack st(5);

    st.push(22);
    st.push(43);
    st.push(44);
    st.push(22);
    st.push(43);
    st.push(44);

    cout << st.peek() << endl;

    st.pop();

    cout << st.peek() << endl;

    st.pop();

    cout << st.peek() << endl;

    st.pop();

    cout << st.peek() << endl;

    if(st.isEmpty()) {
        cout << "Stack is Empty mere dost " << endl;
    }
    else{
        cout << "Stack is not Empty mere dost " << endl;
    }

    
    /*
    //creation of stack using STL
    stack<int> s;

    //push operation
    s.push(2);
    s.push(3);

    //pop
    s.pop();

    cout << "Printing top element " << s.top() << endl;

    if(s.empty()){
        cout << "Stack is empty " << endl;
    }
    else{
        cout << "stack is not empty " << endl;
    }

    cout << "size of stack is " << s.size() << endl;

    */

    return 0;
}
```

</details>

```cpp
#include<iostream>
using namespace std;

class Stack{
    int *arr;
    int top;
    int size;
    public:
        Stack(int size) {
            this -> size = size;
            arr = new int[size];
            top = -1;
        }

        void push(int element) {
            if(size - top > 1)
                arr[++top] = element;
            else
                cout << "<< Stack Overflow!! \nBecause you're trying to push <" << element << "> into the stack >>" << endl;
        }

        void pop() {
            if(isEmpty()) 
                cout << "Stack Underflow !" << endl;
            else
                top--;
        }
        
        void peak () {
            if(isEmpty()) 
                cout << "Stack is Empty !" << endl;
            else
                cout << arr[top] << endl;
        }


        bool isEmpty() {
            if(top == -1) 
                return true;
            else 
                return false;
        }
};

int main() {
    int size = 5;
    Stack s(size);
    s.push(100);

    // inserting/pushing elements
    for (int i = 0; i < size; i++)
        s.push(i+1);
        // 100,1,2,3,4,5 -> beacuse of 5 else statment will be printed  
   
    // printng & popping 
    cout << endl << "printing & popping elements! " << endl;
    while(!s.isEmpty()) {
        s.peak(); // 
        s.pop();
    }
    s.peak();
    s.pop();
}
```
