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.
#include <bits/stdc++.h>
using namespace std;
bool isEmpty(stack<int>& s)
{
bool isStackEmpty
= s.empty(); // checking whether stack is empty or
// not and storing it into isStackEmpty
// variable
return isStackEmpty; // returning bool value stored in
// isStackEmpty
}
int main()
{
stack<int> s;
// The if - else conditional statements below prints
// "Stack is empty."
if (isEmpty(s)) {
cout << "Stack is empty." << endl;
}
else {
cout << "Stack is not empty." << endl;
}
s.push(1); // Inserting value 1 to the stack top
// The if - else conditional statements below prints
// "Stack is not empty."
if (isEmpty(s)) {
cout << "Stack is empty." << endl;
}
else {
cout << "Stack is not empty." << endl;
}
}
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.");
}
}
}
# Python Code:
def isEmpty(s):
isStackEmpty = len(s) == 0 # checking whether stack is empty or
# not and storing it into isStackEmpty variable
return isStackEmpty # returning bool value stored in isStackEmpty
s = []
# The if - else conditional statements below prints "Stack is empty."
if isEmpty(s):
print("Stack is empty.")
else:
print("Stack is not empty.")
s.append(1) # Inserting value 1 to the stack top
# The if - else conditional statements below prints "Stack is not empty."
if isEmpty(s):
print("Stack is empty.")
else:
print("Stack is not empty.")
# This code is contributed by Sakshi
using System;
using System.Collections.Generic;
class Program
{
// Function to check if a stack is empty
static bool IsEmpty(Stack<int> s)
{
return s.Count == 0;
}
static void Main()
{
Stack<int> s = new Stack<int>();
// Check if the stack is empty
if (IsEmpty(s))
{
Console.WriteLine("Stack is empty.");
}
else
{
Console.WriteLine("Stack is not empty.");
}
// Push a value (1) onto the stack
s.Push(1);
// Check if the stack is empty after pushing a value
if (IsEmpty(s))
{
Console.WriteLine("Stack is empty.");
}
else
{
Console.WriteLine("Stack is not empty.");
}
}
}
function isEmpty(stack) {
// checking whether stack is empty or not
return stack.length === 0;
}
function main() {
const s = [];
// The if - else conditional statements below prints "Stack is empty."
if (isEmpty(s)) {
console.log("Stack is empty.");
} else {
console.log("Stack is not empty.");
}
s.push(1); // Inserting value 1 to the stack top
// The if - else conditional statements below prints "Stack is not empty."
if (isEmpty(s)) {
console.log("Stack is empty.");
} else {
console.log("Stack is not empty.");
}
}
// Run the main function
main();
//This code is contributed by Monu.
Output
Stack is empty. Stack is not empty.
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.