Question

Is for study for an exam, thanks.

Some Review Questions/Exercises • • Convert a * b/c - d to posfix notation Provide an algorithm and evaluate the following po

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Convert a * b / c - d to postfix notation

Algorithm to convert from infix(Q) to postfix expression (P) :

Step 1: Push "(" on the stack and add ")" to the end of Q

Step 2 : Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the Stack is empty.

Step 3 : If an operand is encountered add it to P.

Step 4: If a left parenthesis ("(") is encountered ,push it on to stack.

Step 5: If an operator is encountered then:

  • Repeatedly pop from stack and add to P each operator which has the same precedence as/higher precedence than this particular operator.
  • Add operator to stack

Step 6: If a right parenthesis (")") is encountered then

  • Repeatedly pop from stack and add to P each operator until a left parenthesis is encountered.
  • Remove the left parenthesis

Step 7 : Exit

Q = a * b / c - d )

Q Stack P
a ( a
* ( * a
b ( * ab
/ ( / ab*
c ( / ab*c
- ( - ab*c/
d ( - ab*c/d
) ab*c/d-

Postfix expression : a b * c / d -

2. Evaluation of Postfix expression : 8 3 / 4 +

Algorithm to evaluate a postfix expression(P) :

Step 1 : Add a right parenthesis at the end of P

Step 2: Scan P from left to right and repeat steps 3 and 4 for each element of P until ")" is encountered.

Step 3: If an operand is encountered push it to Stack.

Step 4 (a): If an operator is encountered, remove the top 2 elements of Stack where A is at the top and B is next to top.

(b) Evaluate B operator A

(c) Push the result back to stack

Step 5: Result of the expression is the value at the top of stack

Postfix expression : 8 3 / 4 + )

Iteration 1:

Current element = 8 which is an operand, push it to stack

Stack :

8

Iteration 2:

Current element = 3 which is an operand, push it to stack

Stack :

3
8

Iteration 3:

Current element = / which is an operator, pop the first two top elements from Stack

A = 3

B = 8

Result = B operator A = 8/3 = 2.67

Push the Result back to Stack

Stack :

2.67

Iteration 4:

Current Element = 4 , which is an operand, push it to stack

Stack :

4
2.67

Iteration 5:

Current Element = + which is an operator, pop the first two top elements from Stack

A = 2.67

B = 4

Result = B + A = 4 + 2.67 = 6.67

Push the Result back to stack

Stack :

6.67

Iteration 6:

Current element = ) , end the loop

Result = 6.67

Value of the expression = 6.67

3. Deque (Double ended queue) is a data structure in which elements can be inserted and deleted from either ends i.e from the front as well as from the back. We can use Deque to implement a Queue by restricting its insertion only at the end of Deque and deletion only from the front of Deque which is the case in Queue.

4. pop() operation is used to remove and return the top element of stack. The two possible prototypes of pop() operation are:

  • To pass the reference of the variable of type stored in stack to pop as an argument and return the top element in that argument and the function itself returns if the deletion was successful or not: bool pop(int &data) ; where the stack stores elements of type Integer

In this case the value of element deleted will be returned in the argument data passed to function and the function returns the result of the operation was successful or not. In this case if the stack is empty ,we do not need to return any dummy value in data or raise an exception, we can return false from the function to signal failure of the operation.

  • To remove and return the top element of the stack from the function: int pop() ; where stack stores elements of type Integer.

In this case the top element of the stack is removed and returned by the function.In case of empty stack, this function must return either a dummy value or raise exception to signal deletion from an empty stack.

5. Stack is a data structure in which both insertion and deletion of elements take place at one end i.e from the top of stack. So in stack , we only need to maintain a single pointer which points to the top of stack.

Queue is a data structure in which insertion take place at the end of the queue and deletion take place from the front of the queue. So we need to maintain 2 pointers pointing to the front and end of queue.

It is more difficult to implement Queue using an array than implementing a Stack using an array , because in Stack both insertion and deletion take place at top of the Stack , so in case of insertion the only operation is to increment the top pointer and place the item and similarly, in case of deletion the only operation is to decrement the top pointer. But in case of Queue since insertion takes place at the end and deletion at the front. In case of insertion , we only need to increment rear pointer and place the element. But in case of deletion, since deletion takes place at the front of the queue, in order to remove the first element all the elements are shifted by one position to the left to remove the front element. So the amount of operation in case of deletion for queue is much more as compared to Stack when implementing using the array.

Add a comment
Know the answer?
Add Answer to:
Is for study for an exam, thanks. Some Review Questions/Exercises • • Convert a * b/c...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...

  • Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expressi...

    Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...

  • You are to write a program that implements a Reverse Polish Notation Calculator in C using...

    You are to write a program that implements a Reverse Polish Notation Calculator in C using BISON and FLEX, You only have to edit the BISON and FLEX files. Link to the files to start and have a general view of the program: https://www.dropbox.com/sh/83yzs66jhftqj5b/AABZcY9Qwl84JdUFnYpQaZk9a?dl=0 Reverse Polish Notation is a mathematical notation in which every operator follows all of its operands. It is sometimes called postfix notation, and does not require any parentheses as long as each operator has a fixed...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • The following are questions from a study guide, could I get some good examples of c++...

    The following are questions from a study guide, could I get some good examples of c++ code with comments for a better understanding, thanks! 2.Be prepared to implement a stack, queue, or priority queue using a linked list (singly or doubly linked). 3.Be prepared to implement a “weird” method that you’ve never heard of before to prove you truly understand what you are doing.  Here is an example: a.Implement ‘void decimate()’ which deletes every 10th item from the list: https://en.wikipedia.org/wiki/Decimation_(Roman_army) Recurison:...

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

  • I'm having trouble writing this code, can some help me? Step 1: Capturing the input The...

    I'm having trouble writing this code, can some help me? Step 1: Capturing the input The first step is to write a functionGetInput()that inputs the expression from the keyboard and returns the tokens---the operands and operators---in a queue. You must write this function. To make the input easier to process, the operands and operators will be separated by one or more spaces, and the expression will be followed by #. For example, here’s a valid input to your program: 6...

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

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
ADVERTISEMENT
ADVERTISEMENT