< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page

Was this helpful?

  1. Stack
  2. What's Stack & Implementation?

Stack Using Array

PreviousWhat's Stack & Implementation?NextStack using LL

Last updated 10 months ago

Was this helpful?

Stack implementation using STL
#include <iostream>
#include <stack>
using namespace std;

int main()
{
      // stack container declaration
	stack<int> s1;
	stack<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 << "Size of the Stack is: " << s1.size() << ", " << s2.size() << endl;

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

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

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

	// printing the second stack
	cout<<endl<<"s2 = ";
	while (!s2.empty()) {
		cout<<s2.top()<<" ";
		s2.pop(); // deleting
	}
	return 0;
}
// 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; 
    }
    
};
Easy to Understand
#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;
}
#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();
}
📚
Stack Implementation using arrays