166k views
5 votes
The input stream of a stack is a list of all the elements we pushed onto the stack, in the order that we pushed them. If the input stream is ZYXWVUTSR, create a sequence of pushes and pops such that the output stream is YXVUWZSRT.

User Musefan
by
4.1k points

1 Answer

5 votes

Answer:

Push Z

Push Y

Pop Y

Push X

Pop X

Push W

Push V

Pop V

Push U

Pop U

Pop W

Pop Z

Push T

Push S

Pop S

Push R

Pop R

Pop T

Step-by-step explanation:

A stack is a term in computer science that defines an abstract data type that acts as a collection of elements. It has two operations which are mainly push and pop.

Push is used in adding elements to the collection, while pop is used in removing elements from the collection.

If the element A has been pushed to the stack, you check if the top element in the pop[] sequence is A or not.

If it is A, you then pop it right, else top of the push[] sequence will be changed and make the sequences invalid.

So, similarly do the same for all the elements and check if the stack is empty or not in the last.

If empty you then print True else print False.

User Patrick Peters
by
3.2k points