Overload == operator for Stack
Stack is a linear data structure that works in a model of LIFO ( last in first out ). In which, the element pushed at last will be deleted at first.
Insertion and deletion will happen on the same end. A real-life example of a stack would be a stack of books, a stack of plates, etc. The Insertion deletion diagram will look like this :
Stack insertion And deletion or push, pop operation
Overload ‘==’ operator to check equality of size:
Approach: The task for overloading ‘==’ operator can be performed as shown below:
- Initialize two stacks stk1 and stk2 of size N and check if size is equal or not.
- Then Implement a friend method out of class because.
- In the overloaded function for the operator, check if the size of stack1 is equal to stack2 then return true else return false.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
class Stack {
int * stk;
int length;
int size;
public :
Stack( int size)
{
this ->size = size;
this ->length = 0;
this ->stk = new int [size];
}
int getSize() { return this ->size; }
void push( int val)
{
if (length < size)
stk[length++] = val;
else
cout << "\nstack is full\n" ;
}
friend bool operator==(Stack&, Stack&);
};
bool operator==(Stack& stk1, Stack& stk2)
{
return (stk1.getSize() == stk2.getSize());
}
int main()
{
Stack stk1(10), stk2(10);
if (stk1 == stk2) {
cout << "Both stk1 and stk2 are equal (size).\n" ;
}
else {
cout << "stk1 and stk2 are not equal (size).\n" ;
}
return 0;
}
|
Java
import java.util.*;
class Stack {
private int [] stk;
private int length;
private int size;
public Stack( int size) {
this .size = size;
this .length = 0 ;
this .stk = new int [size];
}
public int getSize() {
return this .size;
}
public void push( int val) {
if (length < size)
stk[length++] = val;
else
System.out.println( "\nstack is full\n" );
}
public static boolean equals(Stack stk1, Stack stk2) {
return (stk1.getSize() == stk2.getSize());
}
}
public class Main {
public static void main(String[] args) {
Stack stk1 = new Stack( 10 );
Stack stk2 = new Stack( 10 );
if (Stack.equals(stk1, stk2)) {
System.out.println( "Both stk1 and stk2 are equal (size)." );
} else {
System.out.println( "stk1 and stk2 are not equal (size)." );
}
}
}
|
Python3
class Stack:
def __init__( self , size):
self .size = size
self .length = 0
self .stk = [ 0 ] * size
def getSize( self ):
return self .size
def push( self , val):
if self .length < self .size:
self .stk[ self .length] = val
self .length + = 1
else :
print ( "stack is full" )
def __eq__( self , other):
return self .getSize() = = other.getSize()
def main():
stk1 = Stack( 10 )
stk2 = Stack( 10 )
if stk1 = = stk2:
print ( "Both stk1 and stk2 are equal (size)." )
else :
print ( "stk1 and stk2 are not equal (size)." )
if __name__ = = '__main__' :
main()
|
C#
using System;
public class Stack
{
private int size;
private int length;
private int [] stk;
public Stack( int size)
{
this .size = size;
this .length = 0;
this .stk = new int [size];
}
public int GetSize()
{
return size;
}
public void Push( int val)
{
if (length < size)
{
stk[length] = val;
length++;
}
else
{
Console.WriteLine( "stack is full" );
}
}
public static bool operator ==(Stack s1, Stack s2)
{
return s1.GetSize() == s2.GetSize();
}
public static bool operator !=(Stack s1, Stack s2)
{
return !(s1 == s2);
}
}
public class Program
{
public static void Main()
{
Stack stk1 = new Stack(10);
Stack stk2 = new Stack(10);
if (stk1 == stk2)
{
Console.WriteLine( "Both stk1 and stk2 are equal (size)." );
}
else
{
Console.WriteLine( "stk1 and stk2 are not equal (size)." );
}
}
}
|
Javascript
class Stack {
constructor(size) {
this .stk = new Array(size);
this .length = 0;
this .size = size;
}
getSize() {
return this .size;
}
push(val) {
if ( this .length < this .size) {
this .stk[ this .length++] = val;
} else {
console.log( "\nstack is full\n" );
}
}
static isEqual(stk1, stk2) {
return stk1.getSize() === stk2.getSize();
}
}
const stk1 = new Stack(10);
const stk2 = new Stack(10);
if (Stack.isEqual(stk1, stk2)) {
console.log( "Both stk1 and stk2 are equal (size).\n" );
} else {
console.log( "stk1 and stk2 are not equal (size).\n" );
}
|
Output
Both stk1 and stk2 are equal (size).
Time Complexity : O(1), because all the operations performed are basic stack operations that take a constant amount of time, regardless of the input size.
Auxiliary Space : O(N), where N is the maximum size of the stack.
Overload ‘==’ operator for checking if elements of both stacks are equal:
- In this case in the overloaded function for == operator:
- If both stacks don’t have the same size, then return false.
- Check each element of both stacks and if both the stacks have the same elements then it should return true else false.
- To check that, run a loop till both are empty and compare the top elements.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
class Stack {
int * stk;
int length;
int size;
public :
Stack( int size)
{
this ->size = size;
this ->length = 0;
this ->stk = new int [size];
}
int getSize() { return this ->size; }
int getLength() { return this ->length; }
int * begin() { return this ->stk; }
void push( int val)
{
if (length < size)
stk[length++] = val;
else
cout << "\nstack is full\n" ;
}
friend bool operator==(Stack&, Stack&);
};
bool operator==(Stack& stk1, Stack& stk2)
{
int lenghtOfStack_1 = stk1.getLength();
int lenghtOfStack_2 = stk2.getLength();
if (lenghtOfStack_1 != lenghtOfStack_2) {
return false ;
}
for ( int i = 0; i < lenghtOfStack_1; i++) {
if (stk1.begin()[i] != stk2.begin()[i]) {
return false ;
}
}
return true ;
}
int main()
{
Stack stk1(10), stk2(10);
for ( int i = 0; i < 5; i++) {
stk1.push(i);
stk2.push(i);
}
if (stk1 == stk2) {
cout << "Both stk1 and stk2 are equal.\n" ;
}
else {
cout << "stk1 and stk2 are not equal.\n" ;
}
return 0;
}
|
Java
import java.util.Arrays;
class Stack {
private int [] stk;
private int length;
private int size;
Stack( int size) {
this .size = size;
this .length = 0 ;
this .stk = new int [size];
}
int getSize() {
return this .size;
}
int getLength() {
return this .length;
}
int [] begin() {
return this .stk;
}
void push( int val) {
if (length < size)
stk[length++] = val;
else
System.out.println( "\nstack is full\n" );
}
@Override
public boolean equals(Object obj) {
if (obj == this ) {
return true ;
}
if (!(obj instanceof Stack)) {
return false ;
}
Stack otherStack = (Stack) obj;
if ( this .length != otherStack.length) {
return false ;
}
for ( int i = 0 ; i < this .length; i++) {
if ( this .stk[i] != otherStack.stk[i]) {
return false ;
}
}
return true ;
}
}
public class Main {
public static void main(String[] args) {
Stack stk1 = new Stack( 10 );
Stack stk2 = new Stack( 10 );
for ( int i = 0 ; i < 5 ; i++) {
stk1.push(i);
stk2.push(i);
}
if (stk1.equals(stk2)) {
System.out.println( "Both stk1 and stk2 are equal." );
} else {
System.out.println( "stk1 and stk2 are not equal." );
}
}
}
|
Python3
class Stack:
stk = []
length = 0
size = 0
def __init__( self , size):
self .size = size
self .length = 0
self .stk = [ 0 ] * size
def getSize( self ):
return self .size
def getLength( self ):
return self .length
def push( self , val):
if self .length < self .size:
self .stk[ self .length] = val
self .length + = 1
else :
print ( "stack is full" )
def __eq__(stk1, stk2):
lenghtOfStack_1 = stk1.getLength()
lenghtOfStack_2 = stk2.getLength()
if lenghtOfStack_1 ! = lenghtOfStack_2:
return False
for i in range (lenghtOfStack_1):
if stk1.stk[i] ! = stk2.stk[i]:
return False
return True
if __name__ = = "__main__" :
stk1 = Stack( 10 )
stk2 = Stack( 10 )
for i in range ( 5 ):
stk1.push(i)
stk2.push(i)
if stk1 = = stk2:
print ( "Both stk1 and stk2 are equal." )
else :
print ( "stk1 and stk2 are not equal." )
|
C#
using System;
class Stack {
private int [] stk;
private int length;
private int size;
public Stack( int size)
{
this .size = size;
this .length = 0;
this .stk = new int [size];
}
public int GetSize() { return this .size; }
public int GetLength() { return this .length; }
public int [] Begin() { return this .stk; }
public void Push( int val)
{
if (length < size) {
stk[length++] = val;
}
else {
Console.WriteLine( "\nstack is full\n" );
}
}
public static bool operator == (Stack stk1, Stack stk2)
{
int lengthOfStack_1 = stk1.GetLength();
int lengthOfStack_2 = stk2.GetLength();
if (lengthOfStack_1 != lengthOfStack_2) {
return false ;
}
for ( int i = 0; i < lengthOfStack_1; i++) {
if (stk1.Begin()[i] != stk2.Begin()[i]) {
return false ;
}
}
return true ;
}
public static bool operator != (Stack stk1, Stack stk2)
{
return !(stk1 == stk2);
}
}
class Program {
static void Main( string [] args)
{
Stack stk1 = new Stack(10);
Stack stk2 = new Stack(10);
for ( int i = 0; i < 5; i++) {
stk1.Push(i);
stk2.Push(i);
}
if (stk1 == stk2) {
Console.WriteLine(
"Both stk1 and stk2 are equal." );
}
else {
Console.WriteLine(
"stk1 and stk2 are not equal." );
}
}
}
|
Javascript
class Stack {
constructor(size) {
this .stk = new Array(size);
this .length = 0;
this .size = size;
}
getSize() {
return this .size;
}
getLength() {
return this .length;
}
begin() {
return this .stk;
}
push(val) {
if ( this .length < this .size) {
this .stk[ this .length++] = val;
} else {
console.log( "\nstack is full\n" );
}
}
}
function equals(stk1, stk2) {
let lengthOfStack_1 = stk1.getLength();
let lengthOfStack_2 = stk2.getLength();
if (lengthOfStack_1 !== lengthOfStack_2) {
return false ;
}
for (let i = 0; i < lengthOfStack_1; i++) {
if (stk1.begin()[i] !== stk2.begin()[i]) {
return false ;
}
}
return true ;
}
let stk1 = new Stack(10);
let stk2 = new Stack(10);
for (let i = 0; i < 5; i++) {
stk1.push(i);
stk2.push(i);
}
if (equals(stk1, stk2)) {
console.log( "Both stk1 and stk2 are equal.\n" );
} else {
console.log( "stk1 and stk2 are not equal.\n" );
}
|
Output
Both stk1 and stk2 are equal.
Related Articles:
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...