0% found this document useful (0 votes)
8 views1 page

Push and Pop in Stacks and Queues

The document explains the implementation of stacks using arrays, detailing the push and pop operations. The push operation adds an element to the top of the stack while checking for overflow, and the pop operation removes the top element while checking for underflow. It emphasizes that popping does not physically remove the element from memory but updates the pointer to the new top element.

Uploaded by

Asif Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Push and Pop in Stacks and Queues

The document explains the implementation of stacks using arrays, detailing the push and pop operations. The push operation adds an element to the top of the stack while checking for overflow, and the pop operation removes the top element while checking for underflow. It emphasizes that popping does not physically remove the element from memory but updates the pointer to the new top element.

Uploaded by

Asif Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1/26/25, 4:12 PM Push and Pop in Stacks and Queues

Push and Pop


Let's use arrays to implement a Stack. We create an array with a max size
which decides the max capacity of our stack. To trace the top element of
stack we create a variable top which hold the position of array where the top
element is present. Initially the top variable will be -1.

Push Operation:
The push operation adds an element to the top of the stack. Here's a step-by-
step explanation of the push operation:
1. You have an element that you want to store in the stack.
2. You check if the stack is full. If the stack has a maximum size and it is
already full, this is usually called a stack overflow, and the element cannot
be added.
3. If the stack is not full, you place the element at the top of the stack.
4. You update the pointer that always points to the top of the stack (often
referred to as "top" in programming). After the push, "top" will be pointing
to the newly added element.

Pop Operation:
The pop operation removes the top element from the stack. Here's how the
pop operation works:
1. First, you check if the stack is empty. If it's empty, there is nothing to pop,
and this condition is known as a stack underflow.
2. If the stack is not empty, you proceed to remove the element that "top" is
currently pointing to.
3. After removing the element, you update the "top" pointer to point to the
new top element of the stack, which is the previous element before the
one you just popped.
4. The popped element is returned or kept for further processing, depending
on the implementation and usage.

It’s important to note that with the pop operation, you're typically not
physically removing the element from the memory; you're just logically
considering it removed by moving the "top" pointer.

https://www.codechef.com/learn/course/stacks-and-queues/LSTACKS/problems/STACK11 1/1

You might also like