#### Stack:

Stacking objects means putting them on top of one another in the English language. This data structure allocates memory in the same manner.

Data structures are essential for organizing storage in computers so that humans can access and edit data efficiently. Stacks were among the first data structures to be defined in computer science. In layman’s terms, a stack is a linear accumulation of items. It is a collection of objects that provides fast last-in, first-out (LIFO) insertion, and deletion semantics. It is a modern computer programming and CPU architecture array or list structure of function calls and parameters. Elements in a stack are added or withdrawn from the top of the stack is a “last in, first out” order, similar to a stack of dishes at a restaurant.

Unlike lists or arrays, the objects in the stack do not allow for random access

Given two stacks the task is to check whether the given two stacks are equal or not in Python.

**Examples:**

**Example1:**

**Input:**

Given First Stack = [1,3,4,5] Given Second Stack = [1,3,4,5]

**Output:**

The Given two stacks are equal

**Example2:**

**Input:**

Given First Stack = [1,3,4,3] Given Second Stack = [1,3,4,5]

**Output:**

The Given two stacks are not equal

## Program to How to Check if two Stacks are Equal in Python

Below are the ways to check the given two stacks are equal or not in Python:

### Method #1: Using While Loop (Static Input)

**Approach:**

- Give the first stack as static input and store it in a variable.
- Give the second stack as static input and store it in another variable.
- Create a function checkEqualStacks() which accepts the given two stacks and returns True if the given two stacks are equal else it returns False.
- Inside the checkEqualStacks().
- Check if the length of the first stack is not equal to the length of the second stack using the if conditional statement.
- If their lengths are not equal then return False.
- Loop till the length of the first stack using the while loop.
- Check if the top elements of the given two stacks are equal using the if conditional statement.
- If they are equal remove the top elements from the two stacks using the pop() function.
- Else return False.
- After the end of the while loop return True.
- Inside the main function
- Pass the given two stacks to the checkEqualStacks() function and check whether it returns True or False using the If conditional statement.
- If the above If conditional statement is true then the given two stacks are equal.
- Else the given two stacks are not equal.
- The Exit of the Program.

**Below is the implementation:**

# Create a function checkEqualStacks() which accepts the given two stacks # and returns True if the given two stacks are equal else it returns False. def checkEqualStacks(stck1, stck2): # Inside the checkEqualStacks(). # Check if the length of the first stack is not equal to the # length of the second stack using the if conditional statement. if(len(stck1) != len(stck2)): # If their lengths are not equal then return False. return False # Loop till the length of the first stack using the while loop. while(len(stck1)): # Check if the top elements of the given two stacks are equal # using the if conditional statement. if(stck1[-1] == stck1[-1]): stck1.pop() stck2.pop() # If they are equal remove the top elements from the two stacks # using the pop()function. else: # Else return False. return False # After the end of the while loop return True. return True # Inside the main function # Give the first stack as static input and store it in a variable. gvnstack1 = [4, 9, 1, 2, 3] # Give the second stack as static input and store it in another variable. gvnstack2 = [4, 9, 1, 2, 3] # Pass the given two stacks to the checkEqualStacks() # function and check whether it returns True or False using the If conditional statement. if(checkEqualStacks(gvnstack1, gvnstack2)): # If the above If conditional statement is true then the given two stacks are equal. print('The Given two stacks are equal') else: # Else the given two stacks are not equal. print('The Given two stacks are not equal')

**Output:**

The Given two stacks are equal

### Method #2: Using While loop (User Input)

**Approach:**

- Give the first stack as user input using map(),int(),list(),split() functions and store it in a variable.
- Give the second stack as user input using map(),int(),list(),split() functions and store it in another variable.
- Create a function checkEqualStacks() which accepts the given two stacks and returns True if the given two stacks are equal else it returns False.
- Inside the checkEqualStacks().
- Check if the length of the first stack is not equal to the length of the second stack using the if conditional statement.
- If their lengths are not equal then return False.
- Loop till the length of the first stack using the while loop.
- Check if the top elements of the given two stacks are equal using the if conditional statement.
- If they are equal remove the top elements from the two stacks using the pop() function.
- Else return False.
- After the end of the while loop return True.
- Inside the main function
- Pass the given two stacks to the checkEqualStacks() function and check whether it returns True or False using the If conditional statement.
- If the above If conditional statement is true then the given two stacks are equal.
- Else the given two stacks are not equal.
- The Exit of the Program.

**Below is the implementation:**

# Create a function checkEqualStacks() which accepts the given two stacks # and returns True if the given two stacks are equal else it returns False. def checkEqualStacks(stck1, stck2): # Inside the checkEqualStacks(). # Check if the length of the first stack is not equal to the # length of the second stack using the if conditional statement. if(len(stck1) != len(stck2)): # If their lengths are not equal then return False. return False # Loop till the length of the first stack using the while loop. while(len(stck1)): # Check if the top elements of the given two stacks are equal # using the if conditional statement. if(stck1[-1] == stck1[-1]): stck1.pop() stck2.pop() # If they are equal remove the top elements from the two stacks # using the pop()function. else: # Else return False. return False # After the end of the while loop return True. return True # Inside the main function # Give the first stack as user input using map(),int(),list(),split() functions and store it in a variable. gvnstack1 = list(map(int,input('Enter some random elements of first stack = ').split())) # Give the second stack as user input using map(),int(),list(),split() functions and store it in another variable. gvnstack2 = list(map(int,input('Enter some random elements of second stack = ').split())) # Pass the given two stacks to the checkEqualStacks() # function and check whether it returns True or False using the If conditional statement. if(checkEqualStacks(gvnstack1, gvnstack2)): # If the above If conditional statement is true then the given two stacks are equal. print('The Given two stacks are equal') else: # Else the given two stacks are not equal. print('The Given two stacks are not equal')

**Output:**

Enter some random elements of first stack = 1 3 1 4 5 6 Enter some random elements of second stack = 1 3 1 4 5 6 The Given two stacks are equal