Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithm CS-102

Similar presentations


Presentation on theme: "Data Structures & Algorithm CS-102"— Presentation transcript:

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

2 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.

3 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

4 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.

5 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

6 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

7 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

8 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

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

10 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

11 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

12 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

13 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

14 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

15 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

16 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”

17 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

18

19 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) pop() 7 pop() 5 pop()

20 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

21 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.

22 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.

23 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.

24 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.

25 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.

26 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.

27 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.

28 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)

29 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.

30 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.

31 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

32 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

33 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

34

35

36 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 (-)

37 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

38 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.

39 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.

40 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.

41 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.

42 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.

43 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.

44 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.

45 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.

46 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.

47 Infix to Postfix Infix Postfix A + B A B + 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

48 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.

49 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.

50 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

51 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.

52 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value 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.

53 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,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.

54 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,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.

55 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,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.

56 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,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.

57 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,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.

58 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,3 ,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.

59 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,3 ,3,8 ,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.

60 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,3 ,3,8 ,3,8,2 / ,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.

61 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,3 ,3,8 ,3,8,2 / ,3,4 ,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.

62 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,3 ,3,8 ,3,8,2 / ,3,4 ,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.

63 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,3 ,3,8 ,3,8,2 / ,3,4 ,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.

64 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,3 ,3,8 ,3,8,2 / ,3,4 ,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.

65 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,3 ,3,8 ,3,8,2 / ,3,4 ,7 * ,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.

66 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,3 ,3,8 ,3,8,2 / ,3,4 ,7 * ,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.

67 Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +
Input op1 op2 value stack ,2 ,2,3 ,5 ,3 ,3,8 ,3,8,2 / ,3,4 ,7 * ,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.

68 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: * (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.

69 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

70 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

71

72

73 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.

74 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.

75 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.

76 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.

77 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.

78 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.

79 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.

80 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.

81 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.

82 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.

83 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.

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


Download ppt "Data Structures & Algorithm CS-102"

Similar presentations


Ads by Google