Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Illustrate the result of each operation in the sequence POP(), PUSH(S, 0), PUSH(

ID: 3688110 • Letter: I

Question

Illustrate the result of each operation in the sequence POP(), PUSH(S, 0), PUSH(S, 9), POP(), PUSH(S, 2), POP(), PUSH(S, 7), PUSH(S, 1), PUSH(S, 3), and PUSH(S, 4) on an initially empty stack S stored in array S[1..4].

Use this as a model...

1 2 3 4 5 67 S 15 6 2 9 S 15 6 2 9 17 3 S 15 6 29 17 top[S] = 4 op[S] 6 top[S] = 5 Figure 10.1 An array implementation of a stack S. Stack elements appear only in the lightly shaded positions. (a) Stack S has 4 elements. The top element is 9. (b) Stack S after the calls PuSH(S, 17) and PUSH(S, 3). (c) Stack S after the call PoP(S) has returned the clement 3, which is the one most recently pushed. Although element 3 still appears in the array, it is no longer in the stack; the top is element 17.

Explanation / Answer

we can perfrom two operation on stack 1)push(x) 2)pop()

push(x)----placing the element into stack

pop()---removing the element from stack

top()----returns element which is at the top

Now,we are having stack elements s=[] represents empty stack

pop()---->s=[] no stack elements to pop

PUSH(S,0)---->s=[0, , , ] s with length 4 only one element is pushed other 3 are empty

PUSH(S,9)----->S=[0,9, , ]

POP()--->S=[0, , , ] 9 is popped

PUSH(S,2)---->S=[0,2, , ]

POP()----->S=[0, , , ] 2 is popped..

PUSH(S,7)--->s=[0,7, , ]

PUSH(S,1)--->s=[0,7,1, ]

PUSH(S,3)----->S=[0,7,1,3]

PUSH(s,4)=stack s is full