Question

In: Computer Science

Given the two stacks S1 and S2 each with a set of numbers, write an algorithm...

Given the two stacks S1 and S2 each with a set of numbers, write an algorithm to check if the two stacks are identical (have the same information). Use only the Push and Pop operations. The original stacks should be kept. Briefly explain the time complexity of your algorithm.

Solutions

Expert Solution

Algorithm:

  1. Take a flag variable and set it to true initially, flag = true. This variable will indicate whether the stacks are same or not.
  2. 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.
  3. If the size is same, then compare the top elements of both of the given stacks.
  4. If the top of both stacks is NOT same, set flag to false and return it otherwise pop top elements of both stacks.
  5. Repeat step 3 and 4 until all elements are popped out from both of the stacks.
  6. If both stacks gets empty and the flag variable is still true, it means that the stacks are same.

Function implementing whether two stacks are equal or not:

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;

}


Related Solutions

Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2...
Write pseudocode for an algorithm that calculates the Hamming distance between two strings s1 and s2 of the same length n. What is the complexity of your algorithm?
The intersection (∩) of two sets (s1, s2) is the set of all elements that are...
The intersection (∩) of two sets (s1, s2) is the set of all elements that are in s1 and are also in s2. Write a function (intersect) that takes two lists as input (you can assume they have no duplicate elements), and returns the intersection of those two sets (as a list) without using the in operator or any built-in functions, except for range() and len(). Write some code to test your function, as well. Note: Do not use the...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists some probability for an atom in S2 to decay to S1. What actually causes the atom to decay to the lower energy state? is it the fact that the lower state is more probable for the atom to be in as given by the Boltzmann Factor? so since it is more probable, it has more microstates and entropy causes it to decay? Please help...
For exam review: Given a stack S1 with n numbers and an empty stack S2, design...
For exam review: Given a stack S1 with n numbers and an empty stack S2, design an algorithm (and write psudeocode for it) to sort all the numbers (from small on top to large on bottom) and store them in the originally empty stack S2, using only the stack ADT functions with the two given stacks, and a fixed number (independent of n) of int and char variables. What is the time complexity of your algorithm (related to n) in...
Let S1 and S2 be any two equivalence relations on some set A, where A ≠...
Let S1 and S2 be any two equivalence relations on some set A, where A ≠ ∅. Recall that S1 and S2 are each a subset of A×A. Prove or disprove (all three): The relation S defined by S=S1∪S2 is (a) reflexive (b) symmetric (c) transitive
Write each vector as a linear combination of the vectors in S. (Use s1 and s2,...
Write each vector as a linear combination of the vectors in S. (Use s1 and s2, respectively, for the vectors in the set. If not possible, enter IMPOSSIBLE.) S = {(1, 2, −2), (2, −1, 1)} (a)    z = (−8, −1, 1) z = (b)    v = (−2, −5, 5) v = (c)    w = (1, −23, 23) w = (d)    u = (2, −6, −6) u =
Write each vector as a linear combination of the vectors in S. (Use s1 and s2,...
Write each vector as a linear combination of the vectors in S. (Use s1 and s2, respectively, for the vectors in the set. If not possible, enter IMPOSSIBLE.) S = {(1, 2, −2), (2, −1, 1)} (a)    z = (−13, −1, 1) z =      (b)    v = (−1, −5, 5) v =      (c)    w = (−2, −14, 14) w =      (d)    u = (1, −4, −4) u =     
Let S = (s1, s2, . . . , sn) be a given sequence of integer...
Let S = (s1, s2, . . . , sn) be a given sequence of integer numbers. The numbers can be positive or negative. We define a slice of S as a sub- sequence (si,si+1,...,sj) where 1 ≤ i < j ≤ n. The weight of a slice is defined as the sum of its elements. Provide efficient algorithms to answer each of the following questions: a)Is there any slice with zero weight ? b)Find the maximum weight slice in...
A chemical company based in Pahang makes two types of industrial solvents, S1 and S2. Each...
A chemical company based in Pahang makes two types of industrial solvents, S1 and S2. Each solvent is a mixture of three chemicals. Each kL of S1 requires 12L of chemical K, 9L of chemical L, and 30L of chemical M. Each kL of S2 requires 24L of chemical K, 5L of chemical L, and 30L of chemical M. The profit per kL of S1 is $100, and the profit per kL of S2 is $85. The inventory of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT