< 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

What's Stack & Implementation?

PreviousProblemsNextStack Using Array

Last updated 10 months ago

Was this helpful?

Stacks are a type of container adaptor with LIFO(Last In First Out) type of work, where a new element is added at one end (top) and an element is removed from that end only. Stack uses an encapsulated object of either or (by default) or (sequential container class) as its underlying container, providing a specific set of member functions to access its elements.

If there is confusion in remembering the basic difference between stack and queue, then just have a real-life example for this differentiation, for the stack, stacking of books we can take the top book easily, and for queue remember when you have to stand in queue front of ATM for taking out the cash, then first person near to ATM has the first chance to take out the money from ATM. So, a queue is the FIFO (First In First Out) type working.

Stack Syntax:-

For creating a stack, we must include the <stack> header file in our code. We then use this syntax to define the std::stack:

The functions associated with stack are: – Returns whether the stack is empty – Time Complexity: O(1) – Returns the size of the stack – Time Complexity: O(1) – Returns a reference to the topmost element of the stack – Time Complexity: O(1) – Adds the element ā€˜g’ at the top of the stack – Time Complexity: O(1) – Deletes the most recent entered element of the stack – Time Complexity: O(1)

#include <iostream>
#include <stack>
using namespace std;
int main() {
	stack<int> stack;
	stack.push(21);// The values pushed in the stack should be of the same data which is written during declaration of stack
	stack.push(22);
	stack.push(24);
	stack.push(25);
	int num=0;
	stack.push(num);
	stack.pop();
	stack.pop();
	stack.pop();

	while (!stack.empty()) {
		cout << stack.top() <<" ";
		stack.pop();
	}
}

Output

22 21 

Time complexity: The time complexity of this program is O(N), where N is the total number of elements in the stack. The while loop iterates N times, popping and printing elements from the stack.

Space complexity: The space complexity of this program is O(N), where N is the total number of elements in the stack. The stack data structure uses space proportional to the number of elements stored. In this case, the maximum size of the stack is 5, so the space complexity is constant and can be considered as O(1) as well.

šŸ“š
vector
deque
list
empty()
size()
top()
push(g)
pop()
5MB
StacksAndQueues.pdf
pdf