Data Structures & Algorithm CS-102

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
Stacks Example: Stack of plates in cafeteria.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Topic 15 Implementing and Using Stacks
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Infix, Postfix, Prefix.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Objectives of these slides:
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack Applications.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
CHAPTER 61 STACK, QUEUES, RECURSION. Introduction when one wants to restrict insertion and deletion so that they can take place only at the beginning.
Stacks, Queues & Recursion
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures & Algorithms
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Part 2. Deletion from a linked list Let LIST be a linked list with a node N between nodes A and B. suppose node N is to be deleted from the linked list.
CSCS-200 Data Structure and Algorithms Lecture
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
Part 2. Deletion from a linked list Let LIST be a linked list with a node N between nodes A and B. suppose node N is to be deleted from the linked list.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Review Use of Stack Introduction Stack in our life Stack Operations
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C, 2e
Lecture No.06 Data Structures Dr. Sohail Aslam
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks.
STACKS.
Stacks Stack: restricted variant of list
Data Structures – Week #3
Stack application: postponing data usage
Algorithms and Data Structures
Stack.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Lecture No.07 Data Structures Dr. Sohail Aslam
STACK, QUEUES, RECURSION
UNIT-I Topics to be covere d 1.Introduction to data structures.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Data Structures – Week #3
CSCS-200 Data Structure and Algorithms
More About Stacks: Stack Applications
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Presentation transcript:

Data Structures & Algorithm CS-102 Lecture 3 Circular Link List & Stack Lecturer: Syeda Nazia Ashraf

Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Cicularly Linked List Two views of a circularly linked list: 2 6 8 7 1 head current size=5 current 6 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 8 size=5 head 2 7 1

Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem N=10, M=3 4 3 5 2 6 1 7 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 8 9

Josephus Problem N=10, M=3 eliminated 4 3 5 2 6 1 7 10 8 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 8 9

Josephus Problem N=10, M=3 eliminated 4 3 5 8 2 6 1 7 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 3 5 8 6 2 1 7 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 3 5 8 6 2 7 1 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 5 8 6 2 7 1 3 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 5 8 6 2 7 1 3 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 5 8 6 2 7 1 3 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 5 8 6 2 7 3 10 9 1 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9 1

Josephus Problem N=10, M=3 eliminated 4 5 8 2 7 3 10 9 1 6 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9 1 6

STACKS, QUEUES, RECURSION Introduction The linear lists and linear arrays – allowed one to insert and delete elements at any place in the list At the beginning At the end, or In the middle To restrict insertion and deletions so that they can take place only at the beginning, or at the end of the list, not in the middle. Two of the data structures that are useful are: Stacks Queue

stack A stack is a linear structure in which items may be added or removed only at one end. Three everyday examples of such a structure Stack of dishes Stack of pennies Stack of folded towels An item may be added or removed only from the TOP of any stacks. This means, in particular that the last item to be added to stack is the first item to be removed Stacks are called Last-in-first-out (LIFO) Lists. STACKS are also called “PILES” AND “PUSH- DOWN”

STACKS A stack is a list of elements in which an element may be inserted or deleted only at one end, called the “TOS” of the stack. This means that elements are deleted from a stack in the reverse order of that in which they were inserted into the stack. Two basic operations associated with stacks PUSH is the term to insert an element into a stack POP is the item to delete an element from a stack Example Suppose the following 6 elements are pushes in order onto an empty stack AAA, BBB, CCC, DDD, EEE, FFF

Stack Operations top 1 top 7 7 top 5 5 5 top 2 2 2 2 push(2) push(5) A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training. top 21 top 7 7 top 7 5 5 5 top 5 2 2 2 2 top 2 1 pop() push(21) 21 pop() 7 pop() 5 pop()

Suppose we have three procedures in a project A,B, C POSTPONED DECISIONS Stacks are frequently used to indicate the order of the processing of data when certain steps of the processing must be postponed until other conditions are fulfilled. Suppose we have three procedures in a project A,B, C d e ARRAY REPRESENTATION OF STACKS STACKS may be represented by means of a linear array. Each of our STACKS will be maintained by linear array STACKS, A pointer variable TOS which contains the location of the top element of the stack, Variable MAXSTK, which gives the maximum number of elements that, can be held by the stack. The Condition TOP =0 or TOP= NULL indicate that stack is empty f XXX YYY ZZZ 1 2 3 4 5 6 7 8 Top=3 MAXSTK=8 Stack has 3 elements => since TOP =3 There is room for 5 more items since MAXSTK =8

Stack Implementation: Array Worst case for insertion and deletion from an array when insert and delete from the beginning: shift elements to the left. Best case for insert and delete is at the end of the array – no need to shift any elements. Implement push( ) and pop( ) by inserting and deleting at the end of an array. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack using an Array top 1 2 5 7 1 7 5 1 2 3 4 2 top = 3 1 2 3 4 2 top = 3 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack using an Array A quick examination shows that all five operations take constant time. In case of an array, it is possible that the array may “fill-up” if we push enough elements. Have a boolean function IsFull() which returns true is stack (array) is full, false otherwise. We would call this function before calling push(x). A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack Using Linked List We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements. As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest. End of lecture 5 You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack Using Linked List For a singly-linked list, insert at start or end takes constant time using the head and current pointers respectively. Removing an element at the start is constant time but removal at the end required traversing the list to the node one before the last. Make sense to place stack elements at the start of the list because insert and removal are constant time. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack Using Linked List No need for the current pointer; head is enough. 1 7 5 2 head top 1 7 5 2 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack Operation: List int pop() { int x = head->get(); Node* p = head; head = head->getNext(); delete p; return x; } top 2 5 7 1 head A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack Operation: List void push(int x) { Node* newNode = new Node(); newNode->set(x); newNode->setNext(head); head = newNode; } head A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training. top 9 7 5 2 7 5 newNode 9 2 push(9)

Stack Operation: List All four operations take constant time. int top() { return head->get(); } int IsEmpty() return ( head == NULL ); All four operations take constant time. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Stack: Array or List Since both implementations support stack operations in constant time, any reason to choose one over the other? Allocating and deallocating memory for list nodes does take more time than preallocated array. List uses only as much memory as required by the nodes; array requires allocation ahead of time. List pointers (head, next) require extra memory. Array has an upper limit; List is limited by dynamic memory allocation. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

PUSH: the operation of Adding (Pushing) an item onto a stack POP: the operation of removing (Popping) an item from a stack Operations of Stack

PUSH PUSH (STACK, TOS, MAXSTK, ITEM) In executing the procedure PUSH, one must first test whether there is ROOM in stack for the new item, if not, then we have the condition known as overflow. PUSH (STACK, TOS, MAXSTK, ITEM) This procedure pushes an ITEM onto a stack. Algorithm [Stack already filled?] If TOS= MAXSTK, then: Print OVERFLOW and Return 2. Set TOS := TOS+1. [Increasing TOS by 1] 3. Set Stack[TOS]:= ITEM . [Insert ITEM in New TOS position ] 4. Return

POP Example: POP (STACK, TOS, ITEM) In executing POP, one must first test whether there is an element in stack to be deleted, if not, then we have the condition known as underflow. POP (STACK, TOS, ITEM) This procedure deletes the top element of STACK and assigns it to the variable ITEM. Algorithm [Stack has an item to be removed] If TOS=0, then: Print: UNDER FLOW & Return. 2. Set ITEM := STACK[TOS]. [Assigns TOS element to ITEM] 3 Set TOS= TOS-1. [ Decrease TOS by 1] 4. Return Example: Consider the stack, the operation PUSH (STACK, WWW) Since TOP =3 control is transferred to Step 2. Top=3+1=4 Stack[Top]= Stack[4]=WWW

Arithmetic Expressions: Minimizing Overflow: Essential difference between underflow and overflow in dealing stacks Underflow Depends relatively upon the given algorithm and given input data and hence there is no direct control by the programmer Overflow Depends upon the arbitrary choice of the programmer for the amount of memory space reserved for each stack, and this choice does influence the number of times overflow may occur Arithmetic Expressions: Binary operations in an equation may have different levels of precedence. The following three levels of precedence: Highest: Exponentiation () Next Highest: Multiplication (*) and Division (/) Lowest: Addition (+) and subtraction (-)

Use of Stack Infix Notation: For most common arithmetic operations, the operator symbol is placed between its two operands, For example: A+B C-D E*F (G/H) +A This is called infix notation Polish Notation (Prefix Notation): Polish notation refers to the notation in which the operator symbol is placed before its two operands, for example: +AB -CD *EF [/GH] +A=+/GHA Some examples: The fundamental property of POLISH (PREFIX) NOTATION is that the order in which the operations are performed in completely determined by the positions of the operations and operands in the expression. One never needs parenthesis when resulting expressions in this notation. INFIX PREFIX (A+B)*C [+AB]*C =*+ABC A+(B*C) A+[*BC]=+A*BC (A+B)/(C-D) [+AB]/[-CD]= /+AB-CD

Precedence of Operators For operators of same precedence, the left-to-right rule applies: A+B+C means (A+B)+C. For exponentiation, the right-to-left rule applies A  B  C means A  ( B  C ) A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition A B C * + postfix form A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Prefix, Infix, Postfix Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication A B + C * postfix form A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * A  B * C – D + E/F A B  C*D – E F/+ End of lecture 6

Reverse Polish Notation Refers to the analogous notation in which the operator symbol is placed after its two operands AB+ CD- EF* GHA/+ This notation is called POSTFIX or suffix notation. The computer usually evaluates an arithmetic expression written in infix notation into steps: First converts the expression to postfix notation and Evaluates the postfix expression Stack is the main tool that is used to accomplish given task.

Evaluating Postfix Each operator in a postfix expression refers to the previous two operands. Each time we read an operand, we push it on a stack. When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluation of a Postfix Notation Suppose P is an arithmatic expression written in postfix notation. The following algorithm which uses a STACK to hold operands, evaluates P. Algorithm: This algorithm finds the VALUE of an arithmetic expression P written in Postfix notation. 1. Add a right parenthesis “)” at the end of P. [This acts as a sentinel] 2. Scan P from left to right and repeat step 3 and 4 for each element of P until the sentinel “)”is encountered. 3. If an operand is encountered, put it in STACK. 4. If an operator X is encountered, then: i) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element. ii) Evaluate B X A iii) Place the result of (b) back on STACK. [End of IF Structure] [End of STEP 2 loop] 5. Set VALUE equal to the top element on STACK. 6. Exit

Evaluating Postfix Stack s; while( not end of input ) { e = get next element of input if( e is an operand ) s.push( e ); else { op2 = s.pop(); op1 = s.pop(); value = result of applying operator ‘e’ to op1 and op2; s.push( value ); } finalresult = s.pop(); A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Equivalent INFIX EXPRESSION is: Q: 5* (6+2)-12/4 Example: Consider the following arithmetic expression P written in Postfix notation: P: 5, 6, 2, +,*, 12, 4, /,- Commas are used to separate the elements of P so that 5, 6, 2 is not interpreted as the number 562. Symbol Scanned STACK 5 6 5,6 2 5,6,2 + 5,8 * 40 12 40,12 4 40, 12, 4 / 40, 3 - 37 ) Equivalent INFIX EXPRESSION is: Q: 5* (6+2)-12/4 Parenthesis are necessary for the infix expression Q but not for the postfix expression P First we add a right parenthesis at the end of P. The elements are labeled from left to right The final number in STACK, 37 which is assigned to value when the sentinel “)” is scanned is the value of P.

Transforming Infix Expression into Postfix Expression The following algorithm transforms the infix expression Q into its equivalent postfix expression P. The algorithm uses a STACK to temporarily hold operators and left parentheses. The postfix expression P will be constructed from left to right using the operands and left parentheses. The postfix expression P will be constructed from left to right using the operands from Q and the operators which are removed from STACK. We begin by pushing a left parenthesis onto STACK and adding right parentheses at the end of Q. The algorithm is completed when STACK is empty. Algorithm: POLISH (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P. PUSH “(” onto STACK, and add “)” to the end of Q Scan Q from left to right and repeat step 3 to step 6 for each element of Q until the STACK is empty. If an operand is encountered, add it to P

4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator X is encountered then: i) Repeatedly POP from STACK and add to P each operator (on the top of STACK) which has the same precedence as or higher precedence than X ii) Add X to STACK [END of IF Structure] 6. If a right parenthesis is encountered then: i) Repeatedly POP from STACK and add to P each operator (on the top of STACK) until a left parenthesis is encountered. ii) Remove the left parenthesis. [Do not add the left parenthesis to P] [End of IF Structure] [End of STEP 2 loop] 7. Exit

Converting Infix to Postfix Example: A + B * C symb postfix stack A A A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + ABC * + A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix Consider the infix expressions ‘A+B*C’ and ‘ (A+B)*C’. The postfix versions are ‘ABC*+’ and ‘AB+C*’. The order of operands in postfix is the same as the infix. In scanning from left to right, the operand ‘A’ can be inserted into postfix expression. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix The ‘+’ cannot be inserted until its second operand has been scanned and inserted. The ‘+’ has to be stored away until its proper position is found. When ‘B’ is seen, it is immediately inserted into the postfix expression. Can the ‘+’ be inserted now? In the case of ‘A+B*C’ cannot because * has precedence. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix In case of ‘(A+B)*C’, the closing parenthesis indicates that ‘+’ must be performed first. Assume the existence of a function ‘prcd(op1,op2)’ where op1 and op2 are two operators. Prcd(op1,op2) returns TRUE if op1 has precedence over op2, FASLE otherwise. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

Converting Infix to Postfix prcd(‘*’,’+’) is TRUE prcd(‘+’,’+’) is TRUE prcd(‘+’,’*’) is FALSE Here is the algorithm that converts infix expression to its postfix form. The infix expression is without parenthesis. A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.

ASSIGNMENT 1 Convert given expression into Prefix(Polish) Notation. Write algorithm and Symbol Table which shows status of stack.