Course contentsShow
← Computer Science
Lesson 357 of 2,8729. Stacks and QueuesFree lesson

Push Operation

Learn how the push operation adds an element to the top of the stack and its time complexity.

Push Operation

What you'll learn: You'll learn how the push operation adds an element to the top of a stack and understand its time complexity.

What is Push?

The push operation adds a new element to the top of a stack. Remember that a stack follows the Last-In-First-Out (LIFO) principle, so the most recently pushed element is always the one sitting at the top, ready to be accessed or removed first.

Think of it like stacking plates: when you add a new plate, you place it on top of the pile. You don't dig into the middle or slide it underneath—you simply put it on top. That's exactly what push does.

How Push Works

When you push an element onto a stack:

  1. The stack creates space for the new element
  2. The new element is placed at the "top" position
  3. The stack's size increases by one

Whether your stack is implemented using an array or a linked list, push always adds to the designated top position.

Time Complexity

The push operation runs in O(1) constant time. This means no matter how many elements are already in the stack—whether it's 10 or 10,000—adding one more element takes the same amount of time.

Why? Because you're not searching through the stack or moving other elements around. You're simply placing the new item in the top position, which is a single, direct operation.

Key Takeaway: The push operation adds an element to the top of a stack in O(1) constant time, making it extremely efficient regardless of stack size.