logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
instagramtwitterfacebookyoutubelinkedinmailgithubrss
Copyright © 2026 All rights reserved.
•
VIKI
Built with Vikramaditya Singh
Built with
VIKI - A posts created with Next.js and Tailwind.css
About
arrays
Arrays
Boyer-Moore Majority Vote Algorithm
Kadane’s Algorithm
Triple Reverse algorithm aka Reversal Algorithm
graph
Cycle detection
Graph
Topological Sort
Tree Traversal
heap
Binary Heap
Binomial Heap
Heap
linked-list
Cycle Detection
Linked List
Sorting
miscellaneous
Finding the majority element
Rotate array by K steps
numerical-algorithms
Finding If Number is Prime
GCD of two numbers
Performing Exponentiation
Prime Numbers
Randomising Data
partitioning
3. Max Consecutive Ones
Hoare'S Partition Scheme
Lomuto Partition Scheme
queue
Deques
Priority Queue
Queue
search
The Binary Search Master Guide
The Rotated Sorted Array Master Guide
sort
Bubble Sort
Counting Sort
Sorting
Insertion Sort
Merge Sort
Pigeon Hole Sort
Quick Sort
Selection Sort
stack
Monotonic Stack
Monotonic Stack
Stack
Reversing an Array
Stack
Tower of Hanoi
tree
Binary Tree Traversal
Centre of gravity
Diameter of Tree
Shortest Path
Algorithmic Pattern›stack

Stack

A stack is a data structure where items are added and removed in last-in-first-out order ( sometimes called LIFO lists or LIFOs )

Adding an object to a stack is called pushing the object onto the stack, and removing an object from the stack is called popping the object off of the stack.

# Linked-List Stacks

Implementing a stack is easy using a linked list, with a linked list, pushing and popping items both have O(1)\text{O}(1)O(1) run times.

  • the Push method simply adds a new cell to the top of the list
  • the Pop method removes the top cell from the list.

# Array Stacks

Allocate space for an array enough to hold the data expected to put in the stack. Then use a variable to keep track of the next empty position in the stack.

\

With an array-based stack, Push and Pop have O(1)\text{O}(1)O(1) run times.

!!! info Setting and getting a value from an array generally is faster than creating a new cell in a linked list, so this method may be slightly faster than using a linked list.

An array-based stack requires extra space to hold new items, resizing the array will take extra time. If the array holds N items when you need to resize it, it will take O(n)\text{O}(n)O(n) steps to copy those items into the newly resized array.

# Double Stacks

In case an algorithm needs to use two stacks whose combined size is bounded by some amount. Then can store both stacks in a single array, with one at each end and both growing toward the middle,

PreviousMonotonic StackNextReversing an Array
CONTENTS
Linked-List Stacks <a href="#head-3-55" id="head-3-55"></a>Array StacksDouble Stacks <a href="#head-3-57" id="head-3-57"></a>