{"id":25887,"date":"2021-12-16T09:20:52","date_gmt":"2021-12-16T03:50:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=25887"},"modified":"2021-12-16T09:20:52","modified_gmt":"2021-12-16T03:50:52","slug":"python-program-to-how-to-check-if-two-stacks-are-equal","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-how-to-check-if-two-stacks-are-equal\/","title":{"rendered":"Python Program to How to Check if two Stacks are Equal"},"content":{"rendered":"
Stacking objects means putting them on top of one another in the English language. This data structure allocates memory in the same manner.<\/p>\n
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\u2019s 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 \u201clast in, first out\u201d order, similar to a stack of dishes at a restaurant.<\/p>\n
Unlike lists or arrays, the objects in the stack do not allow for random access<\/p>\n
Given two stacks the task is to check whether the given two stacks are equal or not in Python.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check the given two stacks are equal or not in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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. …<\/p>\nGiven First Stack = [1,3,4,5]\r\nGiven Second Stack = [1,3,4,5]<\/pre>\n
The Given two stacks are equal<\/pre>\n
Given First Stack = [1,3,4,3]\r\nGiven Second Stack = [1,3,4,5]<\/pre>\n
The Given two stacks are not equal<\/pre>\n
Program to How to Check if two Stacks are Equal in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Create a function checkEqualStacks() which accepts the given two stacks\r\n# and returns True if the given two stacks are equal else it returns False.\r\n\r\n\r\ndef checkEqualStacks(stck1, stck2):\r\n # Inside the checkEqualStacks().\r\n # Check if the length of the first stack is not equal to the\r\n # length of the second stack using the if conditional statement.\r\n if(len(stck1) != len(stck2)):\r\n # If their lengths are not equal then return False.\r\n return False\r\n # Loop till the length of the first stack using the while loop.\r\n while(len(stck1)):\r\n # Check if the top elements of the given two stacks are equal\r\n # using the if conditional statement.\r\n if(stck1[-1] == stck1[-1]):\r\n stck1.pop()\r\n stck2.pop()\r\n # If they are equal remove the top elements from the two stacks\r\n # using the pop()function.\r\n\r\n else:\r\n # Else return False.\r\n return False\r\n # After the end of the while loop return True.\r\n return True\r\n\r\n\r\n# Inside the main function\r\n# Give the first stack as static input and store it in a variable.\r\ngvnstack1 = [4, 9, 1, 2, 3]\r\n# Give the second stack as static input and store it in another variable.\r\ngvnstack2 = [4, 9, 1, 2, 3]\r\n# Pass the given two stacks to the checkEqualStacks()\r\n# function and check whether it returns True or False using the If conditional statement.\r\nif(checkEqualStacks(gvnstack1, gvnstack2)):\r\n # If the above If conditional statement is true then the given two stacks are equal.\r\n print('The Given two stacks are equal')\r\nelse:\r\n # Else the given two stacks are not equal.\r\n print('The Given two stacks are not equal')\r\n<\/pre>\n
The Given two stacks are equal<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Create a function checkEqualStacks() which accepts the given two stacks\r\n# and returns True if the given two stacks are equal else it returns False.\r\n\r\n\r\ndef checkEqualStacks(stck1, stck2):\r\n # Inside the checkEqualStacks().\r\n # Check if the length of the first stack is not equal to the\r\n # length of the second stack using the if conditional statement.\r\n if(len(stck1) != len(stck2)):\r\n # If their lengths are not equal then return False.\r\n return False\r\n # Loop till the length of the first stack using the while loop.\r\n while(len(stck1)):\r\n # Check if the top elements of the given two stacks are equal\r\n # using the if conditional statement.\r\n if(stck1[-1] == stck1[-1]):\r\n stck1.pop()\r\n stck2.pop()\r\n # If they are equal remove the top elements from the two stacks\r\n # using the pop()function.\r\n\r\n else:\r\n # Else return False.\r\n return False\r\n # After the end of the while loop return True.\r\n return True\r\n\r\n\r\n# Inside the main function\r\n# Give the first stack as user input using map(),int(),list(),split() functions and store it in a variable.\r\ngvnstack1 = list(map(int,input('Enter some random elements of first stack = ').split()))\r\n# Give the second stack as user input using map(),int(),list(),split() functions and store it in another variable.\r\ngvnstack2 = list(map(int,input('Enter some random elements of second stack = ').split()))\r\n# Pass the given two stacks to the checkEqualStacks()\r\n# function and check whether it returns True or False using the If conditional statement.\r\nif(checkEqualStacks(gvnstack1, gvnstack2)):\r\n # If the above If conditional statement is true then the given two stacks are equal.\r\n print('The Given two stacks are equal')\r\nelse:\r\n # Else the given two stacks are not equal.\r\n print('The Given two stacks are not equal')\r\n<\/pre>\n
Enter some random elements of first stack = 1 3 1 4 5 6\r\nEnter some random elements of second stack = 1 3 1 4 5 6\r\nThe Given two stacks are equal<\/pre>\n","protected":false},"excerpt":{"rendered":"