Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Computer Science 2 Data Structures V section 2 Recitation 1.
Advertisements

Chapter 3 1. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Infix, Postfix, Prefix.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
The If/Else Statement, Boolean Flags, and Menus Page 180
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
Data Structures and Algorithms
Data Structures and Algorithms
Basic concepts of C++ Presented by Prof. Satyajit De
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Revised based on textbook author’s notes.
Data Structures and Algorithms
Data Structures and Algorithms
Variables A piece of memory set aside to store data
Data Structures and Algorithms
Data Structures and Algorithms
Algorithms and Data Structures
Data Structures and Algorithms
Stack.
C++ Arrays.
Stack Data Structure, Reverse Polish Notation, Homework 7
COMPUTER 2430 Object Oriented Programming and Data Structures I
Data Structures and Algorithms
CS 1430: Programming in C++.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Lecture No.07 Data Structures Dr. Sohail Aslam
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CSCE 3110 Data Structures & Algorithm Analysis
CS150 Introduction to Computer Science 1
Programming Concepts and Database
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS148 Introduction to Programming II
Stack.
Fundamental Programming
CS150 Introduction to Computer Science 1
HNDIT11034 More Operators.
Stacks.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Postfix Evaluation (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Algorithm Input: Postfix Expression Output: Numerical value of the evaluated postfix expression Get values of all operands present in the postfix expression Traverse the postfix expression If it is an operand, push the value of that operand on the stack Else, it is an operator, so do the following: Assign the value from the top of stack to a variable ‘operand2’ Pop the element from the stack Assign the value from the top of stack to a variable ‘operand1’ Evaluate the result of operand1 and operand2 and push the result back on the stack Return the top value of the stack (answer) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program: Functions Name Parameter Returns Purpose getValues() Postfix expression, values (array) Called from evaluatePostfix(). Does not return any value To store the values of operands in the array ‘value’ which is passed from evaluatePostfix() isOperator() Current character Called from evaluatePostfix(). Returns ‘true’ or ‘false’ To check whether character is an operator or not calculateValues() operand1, operand2, operator Called from evaluatePostfix(). Returns numerical value Evaluate operand1 and operand2 evaluatePostfix() Postfix expression Called from ‘main()’. Returns numerical value To evaluate postfix expression Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program Examine the ‘currentChar’ If it is an operator, return true Else return false bool isOperator(char currentChar) { if (currentChar=='+' || currentChar=='-' || currentChar=='*' || currentChar=='/' || currentChar=='^') return true; else return false; } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program Traverse the postfix expression If the character is an operand: Accept the value of operand from the user and store it in the array ‘value’ at the index ‘operand’ Using Associative arrays for storing values For a few operands, we need a small array to store their values. In our examples, we are using operands represented by single character (a-z and A –Z). Their ASCII codes are within 0 to 255 We will use array ‘value’ of size 256 to store operand values, in locations corresponding to their ASCII codes, which can be used directly as an index to the array ‘value’ E.g. Postfix expression: ac+ Here, ‘a’ and ‘c’ are operands Assume that user has entered: number 5 for operand ‘a’ and number 7 for operand ‘c’. value[97] = 5 //ASCII value of ‘a’ is 97 value[99] = 7 //ASCII value of ‘c’ is 99 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program Traverse the postfix expression If the character is an operand: Accept the value of operand from the user and store it in the array ‘value’ at the index ‘operand’ void getValues(char postfix[], float value[]) { int i=0; char currentChar; while(postfix[i]!='\0') { currentChar=postfix[i]; if (!isOperator(currentChar)) { //Operand cout << "Enter value of " << currentChar << ": "; cin >> value[currentChar]; } i++; } //End of function E.g. Postfix expression: ac+ Here, ‘a’ and ‘c’ are operands Assume that user has entered: number 5 for operand ‘a’ and number 7 for operand ‘c’. value[97] = 5 //ASCII value of ‘a’ is 97 value[99] = 7 //ASCII value of ‘c’ is 99 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program Perform the operation on ‘operand1’ and ‘operand2’ Return the result to the function ‘evaluatePostfix’. float calculateValues(float operand1, float operand2, char Operator) { if (Operator == '+') return operand1 + operand2; if (Operator == '-') return operand1 - operand2; if (Operator == '*') return operand1 * operand2; if (Operator == '/') return operand1 / operand2; if (Operator == '^') return pow(operand1,operand2); } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program float evaluatePostfix(char postfix[]) { stack<float> Stack; //stack char currentChar; int i=0; float result, operand1, operand2, value[256]; getValues(postfix, value); while(postfix[i]!='\0') { currentChar = postfix[i]; i++; } //End of while (expression traversed) return Stack.top(); //Final answer } //End of function Code for evaluation Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program if (isOperator(currentChar)) { operand2 = Stack.top(); Stack.pop(); operand1 = Stack.top(); result = calculateValues (operand1, operand2, currentChar); Stack.push(result); } else { //It is an operand Stack.push(value[currentChar]); float evaluatePostfix(char postfix[]) { stack<float> Stack; //stack char currentChar; int i=0; float result, operand1, operand2, value[200]; getValues(postfix, value); while(postfix[i]!='\0') { currentChar = postfix[i]; i++; } //End of while (expression traversed) return Stack.top(); //Final answer } //End of function Code for evaluation Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay