Problem

Suppose that a client performs an intermixed sequence of (stack) push and pop operations....

Suppose that a client performs an intermixed sequence of (stack) push and pop operations. The push operations put the integers 0 through 9 in order onto the stack; the pop operations print out the return values. Which of the following sequence(s) could not occur?

a. 4 3 2 1 0 9 8 7 6 5


b. 4 6 8 7 5 3 2 9 0 1


C. 2 5 6 7 4 8 9 3 1 0


d. 4 3 2 1 0 5 6 7 8 9


e. 1 2 3 4 5 6 9 8 7 0


f. 0 4 6 5 3 8 1 7 2 9


g. 1 4 7 9 8 6 5 3 0 2


h. 2 1 4 3 6 5 8 7 9 0

Step-by-Step Solution

Solution 1

The given sequence 4 3 2 1 0 9 8 7 6 5 is possible because:

• First, push 0, 1, 2, 3, and 4 into the stack (stack: 4 3 2 1 0) and then pop these integers 4, 3, 2, 1, and 0 from stack then the following sequence will be printed 4 3 2 1 0.

• Now, push 5, 6, 7, 8, and 9 into the stack (stack: 9 8 7 6 5) and then pop these integers 9, 8, 7, 6, and 5 from stack then the following sequence will be printed 9 8 7 6 5.

• Thus, the complete sequence will be 4 3 2 1 0 9 8 7 6 5.

Hence, the sequence in option (a) is possible.

The given sequence 4 6 8 7 5 3 2 9 0 1 is not possible because:

• In this sequence, starting integer is 4 that means 0 to 4 elements have been pushed. According starting integer only 4 is popped

• In the end of the sequence, 1 is popped after 0 which is not possible because 1 is pushed after 0. So, 1 has to be popped before 0.

Hence, the sequence in option (b) is not possible.

The given sequence 2 5 6 7 4 8 9 3 1 0 is possible because:

• First, push 0, 1, and 2 into the stack (stack: 2 1 0) and then pop only 2 from stack (stack: 1 0).

• Now, push 5 into the stack (stack: 5 1 0) and then pop only 5 from stack (1 0).

• Now, push 6 into the stack (stack: 6 1 0) and then pop only 6 from stack (1 0).

• Now, push 7 into the stack (stack: 7 1 0) and then pop only 7 from stack (1 0).

• Now, push 4 into the stack (stack: 4 1 0) and then pop only 4 from stack (1 0).

• Now, push 8 into the stack (stack: 8 1 0) and then pop only 8 from stack (1 0).

• Now, push 9 into the stack (stack: 9 1 0) and then pop only 9 from stack (1 0).

• Now, push 3 into the stack (stack: 3 1 0) and then pop only 3 from stack (1 0).

• Now, pop 1 and 0 from the stack.

• Thus, the complete sequence will be 2 5 6 7 4 8 9 3 1 0.

Hence, the sequence in option (c) is possible.

The given sequence 4 3 2 1 0 5 6 7 8 9 is possible because:

• First, push 0-4 into the stack (stack: 4 3 2 1 0) and then pop 4, 3, 2, 1, and 0 from stack.

• Now, push 5 into the stack (stack: 5) and then pop only 5 from stack.

• Now, push 6 into the stack (stack: 6) and then pop only 6 from stack.

• Now, push 7 into the stack (stack: 7) and then pop only 7 from stack.

• Now, push 8 into the stack (stack: 8) and then pop only 8 from stack.

• Now, push 9 into the stack (stack: 9) and then pop only 9 from stack.

• Thus, the complete sequence will be 4 3 2 1 0 5 6 7 8 9.

Hence, the sequence in option (d) is possible.

The given sequence 1 2 3 4 5 6 9 8 7 0 is possible because:

• First, push 0-1 into the stack (stack: 1 0) and then pop 1 from stack (stack: 0).

• Now, push 2 into the stack (stack: 2 0) and then pop only 2 from stack (stack: 0).

• Now, push 3 into the stack (stack: 3 0) and then pop only 3 from stack (stack: 0).

• Now, push 4 into the stack (stack: 4 0) and then pop only 4 from stack (stack: 0).

• Now, push 5 into the stack (stack: 5 0) and then pop only 5 from stack (stack: 0).

• Now, push 6 into the stack (stack: 6 0) and then pop only 6 from stack (stack: 0).

• Now, push 7, 8, and 9 into the stack (stack: 9 8 7 0) and then pop 9, 8, 7, and 0 from stack.

• Thus, the complete sequence will be 1 2 3 4 5 6 9 8 7 0.

Hence, the sequence in option (e) is possible.

The given sequence 0 4 6 5 3 8 1 7 2 9 is possible because:

• First, push 0 into the stack and then pop 0 from stack.

• Now, push 1, 2, 3, and 4 into the stack (stack: 4 3 2 1) and then pop only 4 from stack (stack: 3 2 1).

• Now, push 5 and 6 into the stack (stack: 6 5 3 2 1) and then pop 6, 5, and 3 from stack (stack: 2 1).

• Now, push 7 and 8 into the stack (stack: 8 7 2 1) and then pop only 8 from stack (stack: 7 2 1).

• Now, if 7 will be popped from the stack then the given sequence will not be possible.

Hence, the sequence in option (f) is not possible.

The given sequence 1 4 7 9 8 6 5 3 0 2 is not possible because:

• In the end of the sequence, 2 is popped after 0 which is not possible because 2 is pushed after 0. So, 2 has to be popped before 0.

Hence, the sequence in option (g) is not possible.

The given sequence 2 1 4 3 6 5 8 7 9 0 is possible because:

• First, push 0, 1, and 2 into the stack (stack: 2 1 0) and then pop 2 and 1 from stack (stack: 0).

• Now, push 3 and 4 into the stack (stack: 4 3 0) and then pop 4 and 3 from stack (stack: 0).

• Now, push 5 and 6 into the stack (stack: 6 5 0) and then pop 6 and 5 from stack (stack: 0).

• Now, push 7 and 8 into the stack (stack: 8 7 0) and then pop 8 and 7 from stack (stack: 0).

• Now, push 9 into the stack (stack: 9 0) and then pop 9 and 0 from stack.

• Thus, the complete sequence will be 2 1 4 3 6 5 8 7 9 0.

Hence, the sequence in option (h) is possible.

Hence, the sequences in option ( b ), ( f ), and ( g ) are not possible.

Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT