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
(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string...
(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string that represent their sum without using using SHIFT OR any operator +. (b) Analyze time and space of part (a) (c)Implement part (a) in your favorite programming language without using 0+0 operator. You should read the two numeric strings from a FILE input.in and output their addition to standard output. You Solution will be test on 10,000 test cases. Each test case will include...
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 =     
In C: Find a string within a string Given two strings S1 & S2, search for...
In C: Find a string within a string Given two strings S1 & S2, search for an occurrence of the second string within a first string. Note: Do not use system library for the implementation. Input: Code Zinger University Zinger where, First line represents string S1. Second line represents string S2. Output: 5 Here 'Zinger' word starts at 5th index within 'Code Zinger University’. Assume that, The length of strings S1 & S2 are within the range [1 to 10000]....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT