Open In App

What is the time efficiency of the push(), pop(), isEmpty() and peek() operations of Stacks?

Last Updated : 08 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Stack is a linear data structure that follows the LIFO (last in first out) order i.e., the data are entered from one end and while they are removed, they will be removed from the same end. 

The few most performed operations in a stack are:

  1. push()
  2. pop()
  3. isEmpty()
  4. peek()

Now let us check the time efficiency of the operations:

1. push():

This function is called to insert a new element into the stack.

Syntax:

stack.push(value)

Time Complexity: O(1)

Reason: When the function is called a new element is entered into the stack and the top is changed to point to the newly entered element. Also, a link between the new and the old top pointer is made. These are constant time operations.

2. pop()

This function is called to remove the topmost element of the stack.

Syntax:

stack.pop() 

Time Complexity: O(1)

Reason: In this operation, the top element is removed and the pointer that was pointing to the topmost element now points to the one just below it. The operations performed in this case are all performed in constant time. 

3. isEmpty():

This function is called to check if the stack is empty or not.

Syntax:

stack.isEmpty() 

Time Complexity: O(1)

4. peek():

This function is called to get the value of the topmost element of the stack.

Syntax:

stack.peek()

Time Complexity: O(1)

Reason: This function only accesses the pointer pointing to the topmost element and gets the value stored there.

For more details, please refer: Design and Analysis of Algorithms.


Previous Article
Next Article

Similar Reads

Check if the given push and pop sequences of Stack is valid or not
Given push[] and pop[] sequences with distinct values. The task is to check if this could have been the result of a sequence of push and pop operations on an initially empty stack. Return "True" if it otherwise returns "False". Examples: Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 5, 3, 2, 1 }Output: TrueFollowing sequence can be performed:pus
11 min read
Name some Queue implementations and compare them by efficiency of operations
A queue is a linear data structure in which insertion is done from one end called the rear end and deletion is done from another end called the front end. It follows FIFO (First In First Out) approach. The insertion in the queue is called enqueue operation and deletion in the queue is called dequeue operation. A queue can be implemented in two ways
10 min read
Count the number of pop operations on stack to get each element of the array
Prerequisite: Stack, Hashing Given a stack of N numbers and an array of numbers. Count the number of pop operations required to get each element of the array. Once an element is popped then it's not pushed back again. Assume that all the elements from the array are present inside the stack initially. Examples: Input: N = 5, Stack: 6 4 3 2 1 , Array
6 min read
Maximize product of sum of speeds of K workers and their minimum efficiency
Given an integer N, representing the number of workers, an array speed[ ], where speed[i] represents the speed of the ith worker, and an array efficiency[ ], where efficiency[i] represents the efficiency of the ith worker, and an integer K, the task is to select K workers such that the ( Sum of speeds of all the workers ) * ( Minimum efficiency of
9 min read
Understanding "Efficiency" when working with Data Structures and Algorithms
What is Efficient Programming?Efficient programming is programming in a manner that, when the program is executed, uses a low amount of overall resources pertaining to computer hardware.  Practicing to create a small file size and low resource algorithm results in an efficient program. Below are some important concepts you should know to understand
8 min read
Changing One Clock Time to Other Time in Minimum Number of Operations
Given two clock times (in HH:MM:SS format), change one time to other time in minimum number of operations. Here one operation is meant by shifting the hour, minute or second clock hand by 1 unit in either directions. The program follows all the basic rules of a clock for eg.: changing second's hand from 59 to 0 will make 1 unit change for minute ha
15 min read
Time difference between expected time and given time
Given the initial clock time h1:m1 and the present clock time h2:m2, denoting hour and minutes in 24-hours clock format. The present clock time h2:m2 may or may not be correct. Also given a variable K which denotes the number of hours passed. The task is to calculate the delay in seconds i.e. time difference between expected time and given time. Ex
5 min read
Merging and Sorting Two Unsorted Stacks
Given 2 input stacks with elements in an unsorted manner. Problem is to merge them into a new final stack, such that the elements become arranged in a sorted manner. Examples: Input : s1 : 9 4 2 1 s2: 8 17 3 10 Output : final stack: 1 2 3 4 8 9 10 17 Input : s1 : 5 7 2 6 4 s2 : 12 9 3 Output : final stack: 2 3 4 5 6 7 9 12 Create an empty stack to
6 min read
Sorting array using Stacks
Given an array of elements, the task is to sort these elements using a stack. Prerequisites: Stacks Examples: Input : 8 5 7 1 9 12 10 Output : 1 5 7 8 9 10 12 Explanation : Output is sorted element set Input : 7 4 10 20 2 5 9 1 Output : 1 2 4 5 7 9 10 20 We basically use Sort a stack using a temporary stack. Then we put sorted stack elements back t
6 min read
Infix to Prefix conversion using two stacks
Infix: An expression is called the Infix expression if the operator appears in between the operands in the expression. Simply of the form (operand1 operator operand2). Example : (A+B) * (C-D) Prefix: An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 ope
13 min read
Article Tags :
Practice Tags :