Computer Science, asked by NININP9477, 9 months ago

Q. 7 if the sequence of operations- push (1), push (2), pop, push (1), push (2), pop, pop, pop, push (2), pop, are performed on a stack, the sequenceof popped out values are

Answers

Answered by bhagyashreechowdhury
16

Answer:

The sequence of popped out values is : 2,2,1,1,2.

Explanation:

In C++, a stack is considered as a container which can operated by Last In First Out method where the element added at the last is taken out first from the container. This is done by two methods: push() and pop().

  1. stack::push() – This method allows to enter an element on to the stack. Every time during the addition of each element, the size of the stack increases to the step of 1.
  2. stack::pop() – this method allows to take out an element from the top of the stack, which was the last placed. There is a decrease by a step of 1 with the popping out of an element from the stack.

Now let’s find the value of pop sequence based on the operations of push and pop done on to the stack:

  • push(1) : 1 is added to the stack. Stack sequence is 1 .
  • push(2) : 2 is added over 1 to the stack. Stack sequence is 1,2.
  • pop : 2 is removed from the stack. Therefore, first popped out element is 2. Stack sequence is 1 and pop sequence is 2.
  • push (1) : 1 is added over previous 1 to the stack. Stack sequence is 1,1.
  • push (2) : 2 is added over 1 on to the stack. Stack sequence is 1,1,2.
  • pop : 2 is removed from the stack. Stack sequence is 1,1 and pop sequence is 2, 2.
  • pop : 1 is removed from the stack. Stack sequence is 1 and pop sequence is 2, 2,1.
  • pop : 1 which was first inserted is now removed from the stack. Stack sequence is empty and pop sequence is 2, 2,1,1.
  • push (2) : 2 is freshly to the stack. Stack sequence is 2.
  • pop : The 2 is finally removed again from the stack. Stack sequence is empty again and the final pop sequence is 2, 2,1,1,2.
Answered by aryahikaul501
4

Answer:

Answer:

The sequence of popped out values is : 2,2,1,1,2.

Explanation:

In C++, a stack is considered as a container which can operated by Last In First Out method where the element added at the last is taken out first from the container. This is done by two methods: push() and pop().

stack::push() – This method allows to enter an element on to the stack. Every time during the addition of each element, the size of the stack increases to the step of 1.

stack::pop() – this method allows to take out an element from the top of the stack, which was the last placed. There is a decrease by a step of 1 with the popping out of an element from the stack.

Now let’s find the value of pop sequence based on the operations of push and pop done on to the stack:

push(1) : 1 is added to the stack. Stack sequence is 1 .

push(2) : 2 is added over 1 to the stack. Stack sequence is 1,2.

pop : 2 is removed from the stack. Therefore, first popped out element is 2. Stack sequence is 1 and pop sequence is 2.

push (1) : 1 is added over previous 1 to the stack. Stack sequence is 1,1.

push (2) : 2 is added over 1 on to the stack. Stack sequence is 1,1,2.

pop : 2 is removed from the stack. Stack sequence is 1,1 and pop sequence is 2, 2.

pop : 1 is removed from the stack. Stack sequence is 1 and pop sequence is 2, 2,1.

pop : 1 which was first inserted is now removed from the stack. Stack sequence is empty and pop sequence is 2, 2,1,1.

push (2) : 2 is freshly to the stack. Stack sequence is 2.

pop : The 2 is finally removed again from the stack. Stack sequence is empty again and the final pop sequence is 2, 2,1,1,2.

Similar questions