• recategorized by
286 views
7 7 votes

A stack client reads tokens from left to right. If the token is a word, it is pushed onto the stack. If the token is $\texttt{-}$, one item is popped and printed.

Consider the following input:

$\texttt{it was - the best - of times - - - it was - the - -}$

What is printed?

  1. $\texttt{was best times of the was the it}$

  2. $\texttt{it was the best of times was the}$

  3. $\texttt{was best of times the was it the}$

  4. $\texttt{best was times of the was the it}$

3 Answers

1 1 vote

In a stack, the most recently pushed item is popped first.

it       -> push it
was      -> push was
-        -> pop was

the      -> push the
best     -> push best
-        -> pop best

of       -> push of
times    -> push times
-        -> pop times
-        -> pop of
-        -> pop the

it       -> push it
was      -> push was
-        -> pop was

the      -> push the
-        -> pop the
-        -> pop it

Printed sequence:

$\texttt{was best times of the was the it}$

One item remains on the stack at the end.

Correct Option: A

Answer:
Position:
Show:

Related questions

6 6 votes
3 3 answers
321
321 views
GO Classes asked Jul 8
321 views
Suppose an intermixed sequence of stack push and pop operations is performed. The push operations push the integers $0$ through $9$ in order. The pop operations print the...
5 5 votes
2 2 answers
246
246 views
GO Classes asked Jul 9
246 views
A stack is used to check whether parentheses, braces, and brackets are properly balanced.Consider the following two inputs:Input $1: \texttt{[()]\{\}\{[()()]()\}}$Input $...
7 7 votes
1 1 answer
313
313 views
GO Classes asked Jul 10
313 views
Assume there are $n$ elements in the data structure. Consider the following statements:$\text{S1}:$ A stack can be implemented using a linked list such that each individu...
4 4 votes
1 1 answer
195
195 views
GO Classes asked Jul 10
195 views
Suppose a client performs an intermixed sequence of $\texttt{enqueue}$ and $\texttt{dequeue}$ operations on a queue. The enqueue operations put the integers $0$ through $...