Stacks

A Stack is a linear data structure that operates under the Last-In, First-Out (LIFO) principle. It has two primary operations: push and pop. The push operation adds an element to the top of the stack, and the pop operation removes an element from the top. The top of the stack is where elements are added and removed, while the other end, known as the base, remains stable. Stacks are used in various applications, including parsing, expression evaluation, and more.

Example

var stack =new Stack<int>();
stack.Push(1); //Add element.
int element = stack.Pop(); // Remove last element.

Operations of the Stacks in data structure

Here are the common operations performed on various data structures:

  • Insertion: This operation adds an element to a stack.

  • Deletion: This operation removes an element from a stack.

  • Update: This operation modifies the value of an element.

  • Access: This operation retrieves an element from a stack.

  • Search: This operation finds a particular element in a stack.

  • Sort: This operation arranges elements in a specific order.

Complexity Table

OperationBest CaseAverage CaseWorst Case

Access (Peek)

O(1)

O(1)

O(1)

Search

O(N)

O(N)

O(N)

Insertion (Push)

O(1)

O(1)

O(1)

Deletion (Pop)

O(1)

O(1)

O(1)

Update

O(N)

O(N)

O(N)

Last updated