MHB Verifying Sequence of Push/Pop Ops in a Stack

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Sequence
Click For Summary
The discussion revolves around verifying the validity of two sequences of numbers generated by a series of push and pop operations on a stack. The sequence "2 5 6 7 4 8 9 3 1 0" is confirmed to be valid, as the operations can be arranged to allow this output. In contrast, the sequence "4 6 8 7 5 3 2 9 0 1" is deemed invalid because it violates the Last In, First Out (LIFO) principle of stack operations, making it impossible to pop the numbers in that order. The participants also noted a minor omission in the explanation of the first sequence, highlighting the collaborative nature of the discussion. Overall, the thread effectively clarifies the mechanics of stack operations in relation to the given sequences.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Smirk)

Consider that a sequence of 10 push and 10 pop operations is executed at an initially empty stack.
The ten push operations place the numbers $0,1, \dots, 9 $, in an ascending order, in the stack.
It is possible, that between the push operations we can have some pop operations. Each pop() operation prints the value, that is returns.

Which of the following sequence of numbers cannot be printed? For the sequences, that are valid, present the sequence push() and pop() that has to be done, and for the others justify why they cannot be printed.

  • $2 5 6 7 4 8 9 3 1 0$
  • $4 6 8 7 5 3 2 9 0 1$

That's what I have tried:

  • We push in the stack $0$, $1$, $2$, we pop $2$, we push $3$, $4$, $5$, we pop $5$, we push $6$, we pop $6$, we push $7$, we pop $7$, we pop $4$, we push $8$, we pop $8$, we push $9$, we pop $9$, we pop $1$, we pop $0$.

    So, the sequence $2 5 6 7 4 8 9 3 1 0$ can be printed.
    $$$$
  • We push in the stack $0$, $1$, $2$, $3$, $4$, we pop $4$, we push $5$, $6$, we pop $6$, we push $7$, $8$, we pop $8$, we pop $7$, $5$, $3$, $2$, we push $9$, we pop $9$, but, then, we cannot pop $0$, because of the fact that $1$ was put after $0$ and the method that we use to push/pop elements is LIFO.

    So, the second sequence is not valid.

Am I right? (Thinking)
 
Technology news on Phys.org
evinda said:
Hello! (Smirk)

Consider that a sequence of 10 push and 10 pop operations is executed at an initially empty stack.
The ten push operations place the numbers $0,1, \dots, 9 $, in an ascending order, in the stack.
It is possible, that between the push operations we can have some pop operations. Each pop() operation prints the value, that is returns.

Which of the following sequence of numbers cannot be printed? For the sequences, that are valid, present the sequence push() and pop() that has to be done, and for the others justify why they cannot be printed.

  • $2 5 6 7 4 8 9 3 1 0$
  • $4 6 8 7 5 3 2 9 0 1$

That's what I have tried:

  • We push in the stack $0$, $1$, $2$, we pop $2$, we push $3$, $4$, $5$, we pop $5$, we push $6$, we pop $6$, we push $7$, we pop $7$, we pop $4$, we push $8$, we pop $8$, we push $9$, we pop $9$, we pop $1$, we pop $0$.

    So, the sequence $2 5 6 7 4 8 9 3 1 0$ can be printed.
    $$$$
  • We push in the stack $0$, $1$, $2$, $3$, $4$, we pop $4$, we push $5$, $6$, we pop $6$, we push $7$, $8$, we pop $8$, we pop $7$, $5$, $3$, $2$, we push $9$, we pop $9$, but, then, we cannot pop $0$, because of the fact that $1$ was put after $0$ and the method that we use to push/pop elements is LIFO.

    So, the second sequence is not valid.

Am I right? (Thinking)
Sounds good to me, except that in explaining the first sequence you omitted to say "pop 3" after "pop 9".
 
Opalg said:
Sounds good to me, except that in explaining the first sequence you omitted to say "pop 3" after "pop 9".

Oh yes, I am sorry... (Blush) Thanks a lot! (Smile)
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 27 ·
Replies
27
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K