Basic Operations in Stack Data Structure with Implementations
Last Updated :
04 Mar, 2024
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() + " " );
}
}
}
|
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();
|
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()
|
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();
}
}
|
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();
}
}
|
Javascript
let stack = [];
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
console.log(stack[stack.length - 1]);
stack.pop();
console.log(stack[stack.length - 1]);
stack.pop();
console.log(stack[stack.length - 1]);
stack.pop();
console.log(stack[stack.length - 1]);
stack.pop();
console.log(stack[stack.length - 1]);
stack.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()
|
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));
}
}
|
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));
}
}
|
Javascript
function topElement(s) {
return s[s.length - 1];
}
function main() {
let s = [];
s.push(1);
console.log(topElement(s));
s.push(2);
console.log(topElement(s));
s.push(3);
console.log(topElement(s));
}
main();
|
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))
|
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." );
}
}
}
|
C#
using System;
using System.Collections.Generic;
class Program
{
static bool IsEmpty(Stack< int > s)
{
return s.Count == 0;
}
static void Main()
{
Stack< int > s = new Stack< int >();
if (IsEmpty(s))
{
Console.WriteLine( "Stack is empty." );
}
else
{
Console.WriteLine( "Stack is not empty." );
}
s.Push(1);
if (IsEmpty(s))
{
Console.WriteLine( "Stack is empty." );
}
else
{
Console.WriteLine( "Stack is not empty." );
}
}
}
|
Javascript
function isEmpty(stack) {
return stack.length === 0;
}
function main() {
const s = [];
if (isEmpty(s)) {
console.log( "Stack is empty." );
} else {
console.log( "Stack is not empty." );
}
s.push(1);
if (isEmpty(s)) {
console.log( "Stack is empty." );
} else {
console.log( "Stack is not empty." );
}
}
main();
|
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());
}
}
|
C#
using System;
using System.Collections.Generic;
public class Program
{
public static void Main( string [] args)
{
Stack< int > s = new Stack< int >();
Console.WriteLine(s.Count);
s.Push(1);
s.Push(2);
Console.WriteLine(s.Count);
s.Push(3);
Console.WriteLine(s.Count);
}
}
|
Javascript
let stack = [];
console.log(stack.length);
stack.push(1);
stack.push(2);
console.log(stack.length);
stack.push(3);
console.log(stack.length);
|
Python3
stack = []
print ( len (stack))
stack.append( 1 )
stack.append( 2 )
print ( len (stack))
stack.append( 3 )
print ( len (stack))
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...