Push Operation in Stack
Push operation adds an item to the stack.
If the stack is full, then it is said to be an Overflow condition.
Below is a sample program to show Push 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
// printing the stack
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}
// The above loop prints "5 4 3 2 1"
}
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>(); // 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
// Printing the stack in reverse order
while (!s.isEmpty()) {
System.out.print(s.pop() + " ");
}
// The above loop prints "5 4 3 2 1"
}
}
# Python Code:
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
# printing the stack
while stack:
print(stack[-1], end=" ")
stack.pop()
# The above loop prints "5 4 3 2 1"
# This code is contributed by Sakshi
using System;
using System.Collections.Generic;
class Program {
static void Main()
{
Stack<int> s = new Stack<int>(); // 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
// Printing the stack
while (s.Count > 0) {
Console.Write(
s.Peek()
+ " "); // Peek() gets the top element
// without removing it
s.Pop(); // Pop() removes the top element
}
// The above loop prints "5 4 3 2 1"
}
}
class Stack {
constructor() {
this.stack = [];
}
push(value) {
this.stack.push(value); // Pushes the value to the stack top
}
top() {
return this.stack[this.stack.length - 1]; // Returns the element at the top of the stack
}
pop() {
return this.stack.pop(); // Removes and returns the top element of the stack
}
isEmpty() {
return this.stack.length === 0; // Checks if the stack is empty
}
}
function main() {
const s = new Stack(); // Creating a stack
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
// Printing the stack
while (!s.isEmpty()) {
console.log(s.top() + " "); // Outputting the top element
s.pop(); // Removing the top element
}
// The above loop prints "5 4 3 2 1"
}
main(); // Calling the main function
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.
Contact Us