Wednesday, July 6, 2022
HomeWordPress DevelopmentWhat's the time effectivity of the push(), pop(), isEmpty() and peek() operations...

What’s the time effectivity of the push(), pop(), isEmpty() and peek() operations of Stacks?


View Dialogue

Enhance Article

Save Article

Like Article

Stack is a linear knowledge construction that follows the LIFO (final in first out) order i.e., the info are entered from one finish and whereas they’re eliminated, they are going to be faraway from the identical finish. 

The few most carried out operations in a stack are:

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

Now allow us to examine the time effectivity of the operations:

1. push():

This operate is named to insert a brand new aspect into the stack.

Syntax:

stack.push(worth)

Time Complexity: O(1)

Cause: When the operate is named a brand new aspect is entered into the stack and the highest is modified to level to the newly entered aspect. Additionally, a hyperlink between the brand new and the outdated high pointer is made. These are fixed time operations.

2. pull()

This operate is named to take away the topmost aspect of the stack.

Syntax:

stack.pop() 

Time Complexity: O(1)

Cause: On this operation, the highest aspect is eliminated and the pointer that was pointing to the topmost aspect now factors to the one slightly below it. The operations carried out on this case are all carried out in fixed time. 

3. isEmpty():

This operate is named to examine if the stack is empty or not.

Syntax:

stack.isEmpty() 

Time Complexity: O(1)

4. peek():

This operate is named to get the worth of the topmost aspect of the stack.

Syntax:

stack.peek()

Time Complexity: O(1)

Cause: This operate solely accesses the pointer pointing to the topmost aspect and will get the worth saved there.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments