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.
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<int> s; // creating a stack of integers
cout << s.size()
<< endl; // Prints 0 since the stack is empty
s.push(1); // This pushes 1 to the stack top
s.push(2); // This pushes 2 to the stack top
cout << s.size() << endl; // Prints 2 since the stack
// contains two elements
s.push(3); // This pushes 3 to the stack top
cout << s.size() << endl; // Prints 3 since the stack
// contains three elements
}
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>(); // creating a stack of integers
System.out.println(s.size()); // Prints 0 since the stack is empty
s.push(1); // This pushes 1 to the stack top
s.push(2); // This pushes 2 to the stack top
System.out.println(s.size()); // Prints 2 since the stack contains two elements
s.push(3); // This pushes 3 to the stack top
System.out.println(s.size()); // Prints 3 since the stack contains three elements
}
}
# PYthon Code:
stack = [] # creating an empty list as a stack
print(len(stack)) # Prints 0 since the stack is empty
stack.append(1) # This appends 1 to the stack
stack.append(2) # This appends 2 to the stack
print(len(stack)) # Prints 2 since the stack contains two elements
stack.append(3) # This appends 3 to the stack
print(len(stack)) # Prints 3 since the stack contains three element
# This code is contributed by Sakshi
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
Stack<int> s = new Stack<int>(); // creating a stack of integers
Console.WriteLine(s.Count); // Prints 0 since the stack is empty
s.Push(1); // This pushes 1 to the stack top
s.Push(2); // This pushes 2 to the stack top
Console.WriteLine(s.Count); // Prints 2 since the stack contains two elements
s.Push(3); // This pushes 3 to the stack top
Console.WriteLine(s.Count); // Prints 3 since the stack contains three elements
}
}
//This code is contribiuted by Kishan.
let stack = []; // Creating an array to simulate a stack
console.log(stack.length); // Prints 0 since the stack is empty
stack.push(1); // This pushes 1 to the stack top
stack.push(2); // This pushes 2 to the stack top
console.log(stack.length); // Prints 2 since the stack contains two elements
stack.push(3); // This pushes 3 to the stack top
console.log(stack.length); // Prints 3 since the stack contains three elements
//This code is contributed by Aman.
Output
0 2 3
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