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()
|
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);
while (!s.empty()) {
cout << s.top() << " " ;
s.pop();
}
s.pop();
s.pop();
s.pop();
s.pop();
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()
|
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;
}
|
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." );
}
}
}
|
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!