Question

In: Computer Science

What is the entry returned by the peek() method after the following operations on a stack:...

What is the entry returned by the peek() method after the following operations on a stack:
push(A), push(R), pop(), push(D),  pop(), push(L), pop(), push(J), push(S), pop(), pop()

Group of answer choices

A

S

L

D

J

Which of the following is true about the Stack interface we implemented in class?

Group of answer choices

You can only pop from the top of the stack but you can peek at any entry on the stack

The bottom item in a stack is the last item added

The first item added to a stack is the first one removed

To access the item at the bottom of the stack we have to pop() all items on top of it

None of the above is true

The postfix expression corresponding to a + (c - d) / (b * r) is

Group of answer choices

a c d - b r * / +

a c d - + b r * /

a + c d - / b r *

a c + d * b r - /

none of the above

The postfix expression a b * c + d - is equivalent to the following infix expression

Group of answer choices

d - c + b * a

d - (c + b * a)

a * ( b + c - d)

a * b + c - d

None of the above

Solutions

Expert Solution

Question 1:

What is the entry returned by the peek() method after the following operations on a stack:
push(A), push(R), pop(), push(D),  pop(), push(L), pop(), push(J), push(S), pop(), pop()

Answer 1:

Stack abstract data type follows last in first out approach to process data.

The sequece of elements in the stack after each operations:

push(A)

Stack: A

push(R)

Stack: R A

pop()

Stack: A

push(D)

Stack: D A

pop()

Stack: A

push(L)

Stack: L A

pop()

Stack: A

push(J)

Stack: J A

push(S)

Stack: S J A

pop()

Stack: J A

pop()

Stack: A

Now on peek(), A will be returned.

-----------------------------------------------------------

Question 2:

Which of the following is true about the Stack interface we implemented in class?

You can only pop from the top of the stack but you can peek at any entry on the stack

The bottom item in a stack is the last item added

The first item added to a stack is the first one removed

To access the item at the bottom of the stack we have to pop() all items on top of it

None of the above is true

Answer 2:

True options is:

To access the item at the bottom of the stack we have to pop() all items on top of it

-----------------------------------------------------------

Question 3:

The postfix expression corresponding to a + (c - d) / (b * r)

Answer 3:

Sequence No. Input Expression Stack Postfix Expression
1 a a
2 + + a
3 ( +( a
4 c +( ac
5 - +(- ac
6 d +(- acd
7 ) + acd-
8 / +/ acd-
9 ( +/( acd-
10 b +/( acd-b
11 * +/(* acd-b
12 r +/(* acd-br
13 ) +/ acd-br*
14 + acd-br*/
15 acd-br*/+

So, the postfix expression will be: a c d - b r * / +

-----------------------------------------------------------

Question 4:

The postfix expression a b * c + d - is equivalent to the following infix expression

Answer 4:

Sequence No. Input Expression Stack Infix Expression
1 a a
2 b ab
3 * (a*b) (a*b)
4 c (a*b)c (a*b)
5 + ((a*b)+c) ((a*b)+c)
6 d ((a*b)+c)d ((a*b)+c)
7 - ((a*b)+c) - d ((a*b)+c) - d

So, the infix expression is a * b + c - d

-----------------------------------------------------------


Related Solutions

JAVA Stack - Implementation. You will be able to use the push, pop and peek of...
JAVA Stack - Implementation. You will be able to use the push, pop and peek of Stack concept. Post-Fix calculator - When an arithmetic expression is presented in the postfix form, you can use a stack to evaluate the expression to get the final value. For example: the expression 3 + 5 * 9 (which is in the usual infix form) can be written as 3 5 9 * + in the postfix. More interestingly, post form removes all parentheses...
Give the peek algorithm (code) for a Stack that can hold 10 nodes. Include error checking,...
Give the peek algorithm (code) for a Stack that can hold 10 nodes. Include error checking, and assume that the array theData and the integer top are data members of the class Stack. Give the algorithm for the structure Queue that keeps the front and rear “pointers” from progressing through all of memory (assume the Queue array has n elements).
When merchandise previously sold to a customer is returned, the journal entry is ...
When merchandise previously sold to a customer is returned, the journal entry is ...
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and...
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and returns number of elements in the stack. The elements in the stack should not be changed after calling this method. please do it in java
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack s using a Queue. Test your method using some example stacks. In java
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
what we can do after becoming a full stack developer.
what we can do after becoming a full stack developer.
write a c++ program to perform the following operations on stack of Integers (Array Implementation of...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of Stack with maximum size MAX) (i) Push an Element on to stack (ii) Pop an Element from stack (iii) Demonstrate how stack can be used to check Palindrome (iv) Display the status of stack (v) Exit
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack • When an operand is read, push it on statck • When an operator is read i.e +, *. /, - – Pop two from the top of the stack and apply the operator and push the result on stack if there is one value instead of two display an error message • Keep repeating until an equal sign, = is read; pop from...
Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements....
Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements. the values inside the stack must be changed, that the top will be the last and so on. please use java code to slove. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT