Pop Operation in Stack
Pop operation is used to remove an item from the stack.
The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
Below is a sample program to show Pop operation in Stack.
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<int> s; // creating a stack of integers
s.push(1); // This pushes 1 to the stack top
s.push(2); // This pushes 2 to the stack top
s.push(3); // This pushes 3 to the stack top
s.push(4); // This pushes 4 to the stack top
s.push(5); // This pushes 5 to the stack top
// Now, let us remove elements from the stack using pop function
while (!s.empty()) {
cout << s.top() << " ";
s.pop(); // removes the top element from the stack
}
}
import java.util.Stack;
public class Main {
public static void main(String[] args)
{
Stack<Integer> s = new Stack<>(); // Creating a stack of integers
s.push(1); // Pushing 1 to the stack top
s.push(2); // Pushing 2 to the stack top
s.push(3); // Pushing 3 to the stack top
s.push(4); // Pushing 4 to the stack top
s.push(5); // Pushing 5 to the stack top
// Removing elements from the stack using the pop function
while (!s.empty()) {
System.out.print(s.peek() + " "); // Displaying the top element without removing it
s.pop(); // Removes the top element from the stack
}
}
}
stack = []
stack.append(1) # This pushes 1 to the stack top
stack.append(2) # This pushes 2 to the stack top
stack.append(3) # This pushes 3 to the stack top
stack.append(4) # This pushes 4 to the stack top
stack.append(5) # This pushes 5 to the stack top
# Now, let us remove elements from the stack using pop function
while stack:
print(stack[-1], end=" ")
stack.pop() # removes the top element from the stack
using System;
using System.Collections.Generic;
class Program {
static void Main()
{
// Creating a stack of integers
Stack<int> s = new Stack<int>();
// Pushing elements onto the stack
s.Push(1); // This pushes 1 to the stack top
s.Push(2); // This pushes 2 to the stack top
s.Push(3); // This pushes 3 to the stack top
s.Push(4); // This pushes 4 to the stack top
s.Push(5); // This pushes 5 to the stack top
// Removing elements from the stack using Pop function
while (s.Count > 0) {
Console.Write(s.Peek() + " "); // Displaying the top element without removing it
s.Pop(); // Removes the top element from the stack
}
}
}
// Creating a stack
let stack = [];
// Pushing elements to the stack
stack.push(1); // This pushes 1 to the stack top
stack.push(2); // This pushes 2 to the stack top
stack.push(3); // This pushes 3 to the stack top
stack.push(4); // This pushes 4 to the stack top
stack.push(5); // This pushes 5 to the stack top
// Removing elements from the stack using pop function
while (stack.length > 0) {
console.log(stack[stack.length - 1]); // Print the top element
stack.pop(); // Removes the top element from the stack
}
Output
5 4 3 2 1
Basic Operations in Stack Data Structure with Implementations
In order to make manipulations in a stack, there are certain operations provided to us for Stack, which include:
- push() to insert an element into the stack
- pop() to remove an element from the stack
- top() Returns the top element of the stack.
- isEmpty() returns true if the stack is empty else false.
- size() returns the size of the stack.
In this post, we will see how to perform these operations on Stack.