Check if the two given stacks are same
Given two Stacks, the task is to check if the given stacks are same or not.
Two stacks are said to be same if they contains the same elements in the same order.
Example:

Approach:
- Take a flag variable and set it to true initially, flag = true. This variable will indicate whether the stacks are same or not.
- First check if the size of given stack1 and stack2 are equal. If the size is not equal, set flag to false and return it.
- If the size is same, then compare the top elements of both of the given stacks.
- If the top of both stacks is NOT same, set flag to false and return it otherwise pop top elements of both stacks.
- Repeat step 3 and 4 until all elements are popped out from both of the stacks.
- If both stacks gets empty and the flag variable is still true, it means that the stacks are same.
Below is the implementation of the above idea:
C++
// C++ program to check if the given // stacks are equal or not #include <bits/stdc++.h> using namespace std; // Function to check if the two given // stacks are same bool isSameStack(stack<string> stack1, stack<string> stack2) { // Create a flag variable bool flag = true; // Check if size of both stacks are same if (stack1.size() != stack2.size()) { flag = false; return flag; } // Until the stacks are not empty // compare top of both stacks while (stack1.empty() == false) { // If the top elements of both stacks // are same if (stack1.top() == stack2.top()) { // Pop top of both stacks stack1.pop(); stack2.pop(); } else { // Otherwise, set flag to false flag = false; break; } } // Return flag return flag; } // Driver Code int main() { // Creating stacks stack<string> stack1; stack<string> stack2; // Inserting elements to stack1 stack1.push("Geeks"); stack1.push("4"); stack1.push("Geeks"); stack1.push("Welcomes"); stack1.push("You"); // Inserting elements to stack2 stack2.push("Geeks"); stack2.push("4"); stack2.push("Geeks"); stack2.push("Welcomes"); stack2.push("You"); if (isSameStack(stack1, stack2)) cout << "Stacks are Same"; else cout << "Stacks are not Same"; return 0; } |
chevron_right
filter_none
Java
// Java program to check if the given // stacks are equal or not import java.util.*; class GFG { // Function to check if the two given // stacks are same static boolean isSameStack(Stack<String> stack1, Stack<String> stack2) { // Create a flag variable boolean flag = true; // Check if size of both stacks are same if (stack1.size() != stack2.size()) { flag = false; return flag; } // Until the stacks are not empty // compare top of both stacks while (stack1.empty() == false) { // If the top elements of both stacks // are same if (stack1.peek() == stack2.peek()) { // Pop top of both stacks stack1.pop(); stack2.pop(); } else { // Otherwise, set flag to false flag = false; break; } } // Return flag return flag; } // Driver Code public static void main(String arr[]) { // Creating stacks Stack<String> stack1 = new Stack<String>(); Stack<String> stack2 = new Stack<String>(); // Inserting elements to stack1 stack1.push("Geeks"); stack1.push("4"); stack1.push("Geeks"); stack1.push("Welcomes"); stack1.push("You"); // Inserting elements to stack2 stack2.push("Geeks"); stack2.push("4"); stack2.push("Geeks"); stack2.push("Welcomes"); stack2.push("You"); if (isSameStack(stack1, stack2)) System.out.println("Stacks are Same"); else System.out.println("Stacks are not Same"); } } /* This code contributed by PrinciRaj1992 */ |
chevron_right
filter_none
Python3
# Python3 program to check if the given # stacks are equal or not # Function to check if the two given # stacks are same def isSameStack(stack1, stack2) : # Create a flag variable flag = True; # Check if size of both stacks are same if (len(stack1) != len(stack2)) : flag = False; return flag; # Until the stacks are not empty # compare top of both stacks while (len(stack1)) : # If the top elements of both stacks # are same if (stack1[0] == stack2[0]) : # Pop top of both stacks stack1.pop(); stack2.pop(); else : # Otherwise, set flag to false flag = False; break; # Return flag return flag; # Driver Code if __name__ == "__main__" : # Creating stacks stack1 = []; stack2 = []; # Inserting elements to stack1 stack1.append("Geeks"); stack1.append("4"); stack1.append("Geeks"); stack1.append("Welcomes"); stack1.append("You"); # Inserting elements to stack2 stack2.append("Geeks"); stack2.append("4"); stack2.append("Geeks"); stack2.append("Welcomes"); stack2.append("You"); if (isSameStack(stack1, stack2)) : print("Stacks are Same"); else : print("Stacks are not Same"); # This code is contributed by AnkitRai01 |
chevron_right
filter_none
C#
// C# program to check if the given // stacks are equal or not using System; using System.Collections.Generic; class GFG { // Function to check if the two given // stacks are same static Boolean isSameStack(Stack<String> stack1, Stack<String> stack2) { // Create a flag variable Boolean flag = true; // Check if size of both stacks are same if (stack1.Count != stack2.Count) { flag = false; return flag; } // Until the stacks are not empty // compare top of both stacks while (stack1.Count!=0) { // If the top elements of both stacks // are same if (stack1.Peek() == stack2.Peek()) { // Pop top of both stacks stack1.Pop(); stack2.Pop(); } else { // Otherwise, set flag to false flag = false; break; } } // Return flag return flag; } // Driver Code public static void Main(String []arr) { // Creating stacks Stack<String> stack1 = new Stack<String>(); Stack<String> stack2 = new Stack<String>(); // Inserting elements to stack1 stack1.Push("Geeks"); stack1.Push("4"); stack1.Push("Geeks"); stack1.Push("Welcomes"); stack1.Push("You"); // Inserting elements to stack2 stack2.Push("Geeks"); stack2.Push("4"); stack2.Push("Geeks"); stack2.Push("Welcomes"); stack2.Push("You"); if (isSameStack(stack1, stack2)) Console.WriteLine("Stacks are Same"); else Console.WriteLine("Stacks are not Same"); } } // This code has been contributed by 29AjayKumar |
chevron_right
filter_none
Output:
Stacks are Same
Recommended Posts:
- Queue using Stacks
- Sorting array using Stacks
- Implement two stacks in an array
- Bubble sort using two Stacks
- Find maximum sum possible equal sum of three stacks
- Sudo Placement[1.3] | Playing with Stacks
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Merging and Sorting Two Unsorted Stacks
- Infix to Prefix conversion using two stacks
- How to efficiently implement k stacks in a single array?
- Check if two expressions with brackets are same
- Check if two trees are Mirror
- Check mirror in n-ary tree
- Check if concatenation of two strings is balanced or not
- Check if any large number is divisible by 19 or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



