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.
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.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}
}
Java
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
while (!s.isEmpty()) {
System.out.print(s.pop() + " ");
}
}
}
Python3
stack = []
stack.append(1)
stack.append(2)
stack.append(3)
stack.append(4)
stack.append(5)
while stack:
print(stack[-1], end=" ")
stack.pop()
C#
using System;
using System.Collections.Generic;
class Program {
static void Main()
{
Stack<int> s = new Stack<int>();
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(5);
while (s.Count > 0) {
Console.Write(
s.Peek()
+ " ");
s.Pop();
}
}
}
Javascript
class Stack {
constructor() {
this.stack = [];
}
push(value) {
this.stack.push(value);
}
top() {
return this.stack[this.stack.length - 1];
}
pop() {
return this.stack.pop();
}
isEmpty() {
return this.stack.length === 0;
}
}
function main() {
const s = new Stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
while (!s.isEmpty()) {
console.log(s.top() + " ");
s.pop();
}
}
main();
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.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
cout << s.top() << " ";
s.pop();
cout << s.top() << " ";
s.pop();
cout << s.top() << " ";
s.pop();
cout << s.top() << " ";
s.pop();
cout << s.top() << " ";
s.pop();
}
Java
import java.util.Stack;
public class Main {
public static void main(String[] args)
{
Stack<Integer> s
= new Stack<>();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
System.out.print(
s.peek() + " ");
s.pop();
System.out.print(s.peek() + " ");
s.pop();
System.out.print(s.peek() + " ");
s.pop();
System.out.print(s.peek() + " ");
s.pop();
System.out.print(s.peek() + " ");
s.pop();
}
}
Python3
stack = []
stack.append(1)
stack.append(2)
stack.append(3)
stack.append(4)
stack.append(5)
print(stack[-1], end=" ")
stack.pop()
print(stack[-1], end=" ")
stack.pop()
print(stack[-1], end=" ")
stack.pop()
print(stack[-1], end=" ")
stack.pop()
print(stack[-1], end=" ")
stack.pop()
C#
using System;
using System.Collections.Generic;
class Program {
static void Main()
{
Stack<int> s = new Stack<int>();
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(5);
Console.Write(s.Peek() + " ");
s.Pop();
Console.Write(s.Peek() + " ");
s.Pop();
Console.Write(s.Peek() + " ");
s.Pop();
Console.Write(s.Peek() + " ");
s.Pop();
Console.Write(s.Peek() + " ");
s.Pop();
}
}
Top Operation in Stack:
Top operation is used to return the top element of the stack .
Below is a sample program to show Pop operation in Stack.
C++
#include <bits/stdc++.h>
using namespace std;
int topElement(stack<int>& s) { return s.top(); }
int main()
{
stack<int> s;
s.push(1);
cout << topElement(s)
<< endl;
s.push(2);
cout << topElement(s)
<< endl;
s.push(3);
cout << topElement(s)
<< endl;
}
Java
import java.util.Stack;
public class StackExample {
public static int topElement(Stack<Integer> s) {
return s.peek();
}
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push(1);
System.out.println(topElement(s));
s.push(2);
System.out.println(topElement(s));
s.push(3);
System.out.println(topElement(s));
}
}
Python3
def topElement(s):
return s[-1]
s = []
s.append(1)
print(topElement(s))
s.append(2)
print(topElement(s))
s.append(3)
print(topElement(s))
C#
using System;
using System.Collections.Generic;
class Program
{
static int TopElement(Stack<int> s)
{
return s.Peek();
}
static void Main()
{
Stack<int> s = new Stack<int>();
s.Push(1);
Console.WriteLine(TopElement(s));
s.Push(2);
Console.WriteLine(TopElement(s));
s.Push(3);
Console.WriteLine(TopElement(s));
}
}
isEmpty Operation in Stack:
isEmpty operation is a boolean operation that is used to determine if the stack is empty or not .
This operation will return true if the stack is empty, else false.
Below is a sample program to show Pop operation in Stack.
C++
#include <bits/stdc++.h>
using namespace std;
bool isEmpty(stack<int>& s)
{
bool isStackEmpty
= s.empty();
return isStackEmpty;
}
int main()
{
stack<int> s;
if (isEmpty(s)) {
cout << "Stack is empty." << endl;
}
else {
cout << "Stack is not empty." << endl;
}
s.push(1);
if (isEmpty(s)) {
cout << "Stack is empty." << endl;
}
else {
cout << "Stack is not empty." << endl;
}
}
Java
import java.util.Stack;
public class Main {
public static boolean isEmpty(Stack<Integer> s) {
return s.empty();
}
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
if (isEmpty(s)) {
System.out.println("Stack is empty.");
} else {
System.out.println("Stack is not empty.");
}
s.push(1);
if (isEmpty(s)) {
System.out.println("Stack is empty.");
} else {
System.out.println("Stack is not empty.");
}
}
}
Python3
def isEmpty(s):
isStackEmpty = len(s) == 0
return isStackEmpty
s = []
if isEmpty(s):
print("Stack is empty.")
else:
print("Stack is not empty.")
s.append(1)
if isEmpty(s):
print("Stack is empty.")
else:
print("Stack is not empty.")
Output
Stack is empty.
Stack is not empty.
size() Operation in Stack:
Size operation in Stack is used to return the count of elements that are present inside the stack .
Below is a sample program to show Pop operation in Stack.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<int> s;
cout << s.size()
<< endl;
s.push(1);
s.push(2);
cout << s.size() << endl;
s.push(3);
cout << s.size() << endl;
}
Java
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
System.out.println(s.size());
s.push(1);
s.push(2);
System.out.println(s.size());
s.push(3);
System.out.println(s.size());
}
}
Python3
stack = []
print(len(stack))
stack.append(1)
stack.append(2)
print(len(stack))
stack.append(3)
print(len(stack))
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
06 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...