Download presentation
Presentation is loading. Please wait.
Published byIrwan Setiawan Modified over 6 years ago
1
11/25/2018 Smitha N Pai Stacks “Live as if you were to die tomorrow. Learn as if you were to live forever.” ― Mahatma Gandhi
2
Definition and examples Representing stacks in C
11/25/2018 Smitha N Pai Definition and examples Representing stacks in C Evaluation of expressions Multiple stacks and queues. Application : infix, postfix and prefix and their conversions (3.1 ,3.2, 3.4,3.5 of Text Book 3) (6 hrs) Ellis Horowitz, Sartaj Sahni , Anderson, “ Fundamentals of Data Structures in C”, Silicon Press, 2nd Edition, 2007.
3
Data structures allows programmers to write efficient programs.
11/25/2018 Smitha N Pai The Stack Data structure deals with the study of how data is organized in memory and how efficiently data can be retrieved and manipulated. Data structures allows programmers to write efficient programs. Data is efficiently retrieved ,manipulated and organized in memory.
4
Data structures(D.S) can be classified as 1.primitive D.S
11/25/2018 Smitha N Pai Data structures(D.S) can be classified as 1.primitive D.S 2.non primitive D.S Primitive D.S D.S that are manipulated directly by machine instructions. Ex: integers, floating point, characters and pointers. Non primitive D.S D.S that cannot be manipulated directly by machine instructions. ex: arrays, structures, stacks, queues, linked lists, files.
5
They are further classified as
11/25/2018 Smitha N Pai They are further classified as 1.Linear D.S - logical relationship between elements like stacks, queues, linked lists. 2.Non linear D.S: ex: trees, graphs, files Pointers are used extensively in implementing data structures.
6
Last item inserted will be on the top of stack.
11/25/2018 Smitha N Pai The Stack: Special type of D.S where items are inserted at one end called the top of stack and deleted from the same end. Last item inserted will be on the top of stack. Last item inserted will be the first one to be deleted. It is called Last In First Out(LIFO) or First In Last Out(FILO).
7
Various operations on stacks Insert an item into the stack.(Push)
11/25/2018 Smitha N Pai Various operations on stacks Insert an item into the stack.(Push) Delete an item from the stack.(pop) Display the contents of the stack.
8
An example with pictorial representation of stack
11/25/2018 Smitha N Pai An example with pictorial representation of stack Consider a stack S which is initially empty. Let the maximum size of stack be 8. Top points to the top of stack. Stack is empty, it is set to -1 initially. As an item is inserted, top is incremented. top=-1 top D top C C top B B B top A A A A After A After B After C After D
9
Pop removes the top item from stack and returns it.
11/25/2018 Smitha N Pai Pop removes the top item from stack and returns it. After the item is removed, top is decremented. L top top C K B top B B A A A After 1st pop After 2nd pop After inserting K and L
10
Primitive stack operations
11/25/2018 Smitha N Pai Primitive stack operations push( ): Increments top and adds item to the stack at top. push(S,A)– S is the stack and A is the item to be inserted. Displays message “stack full” when stack is full. pop( ): Removes the top item from stack and returns it. int i=pop(S); Displays message “stack empty” if we try to pop when there is no element in stack i.e when top==-1 3. empty( ): Returns true if stack is empty top==-1, false otherwise. boolean empty(S); isfull(): Displays message “stack full” if we try to pop when the top is pointing to stcksize-1. display( ): display contents of stack. These functions are user defined.
11
11/25/2018 Smitha N Pai Applications of stack 1.Conversion of expressions : Mathematical expressions represented using infix expressions will be converted into equivalent m/c instructions using stacks. 2. Evaluation of expressions Arithmetic expressions in the form of either prefix or postfix can be evaluated using stacks. 3.Recursion Stacks are used extensively in recursion. 4.Other applications Find whether a string is palindrome or not To check whether a given expression is valid or not.
12
Representing stacks in c To implement a stack, we need to have
11/25/2018 Smitha N Pai Representing stacks in c To implement a stack, we need to have An array to hold the elements of stack. An integer variable to indicate the top of stack. Stacks can be implemented in c by either Without using structures. Using structures.(most preferred)
13
11/25/2018 Smitha N Pai Implementing stack without structure #include<stdio.h> #define STACK_SIZE 10 //max size of stack void main() { int top; //top of stack; int s[10]; //hold stack elements int item; //item to be inserted int del_item; //popped item int choice; //user choice top=-1; //stack empty initially for(; ;) //loop until exit printf(“enter your choice”); printf(“1.push 2.pop 3.display 4.exit”); scanf(“%d”,&choice);
14
11/25/2018 Smitha N Pai switch(choice) { case 1: printf(“enter item to be pushed”); scanf(“%d”, &item); push(item,&top,s); //call push function break; case 2:del_item=pop(&top, s); printf(“deleted item is %d”,del_item); case 3:display(top,s); default: exit(0); } //end of switch } //end of for } //end of main
15
11/25/2018 Smitha N Pai /*push function*/ void push(int item, int *top, int s[]) { if(*top==STACK_SIZE-1) printf(“stack full”); return; } (*top)++; //increment top s[*top]=item; //insert the item
16
11/25/2018 Smitha N Pai /*pop*/ int pop(int *top, int s[]) { int del_item; if(*top==-1) printf(“stack empty”); return(0); } else del_item=s[*top]; //remove top element (*top)--; //decrement top return(del_item);
17
11/25/2018 Smitha N Pai void display(int top, int s[]) { int i; if(top==-1) printf(“stack empty”); exit(0); } printf(“contents is”); for(i=0;i<=t;i++) //start from bottom to top printf(“%d”, s[i]);
18
Implementing stack using structure #include<stdio
Implementing stack using structure #include<stdio.h> #define STACK_SIZE 5 //max size of stack struct stack { int top; int sarray[STACK_SIZE]; }; typedef struct stack STACK; void main() int item, choice; //item to be inserted and user choice STACK s; //stack variable s.top=-1; //stack empty initially for(; ;) //loop until exit printf(“enter your choice”); printf(“1.push 2.pop 3.display 4.exit”); scanf(“%d”,&choice); 11/25/2018 Smitha N Pai
19
switch(choice) { case 1: printf(“enter item to be pushed”); scanf(“%d”, &item); push(item,&s); //call push function break; case 2:item=pop(&s); if(item= = -1) printf(“empty stack”); else printf(“deleted item is %d”,item); case 3:display(s); default: exit(0); } //end of switch }//end of for } //end of main 11/25/2018 Smitha N Pai
20
11/25/2018 Smitha N Pai /*push function*/ void push(int item, STACK *stck) { if(stcktop==STACK_SIZE-1) printf(“stack full”); exit(0); } stcktop++; //increment top stcksarray[stcktop]=item; //insert the item
21
11/25/2018 Smitha N Pai /*pop*/ int pop(STACK stck) { int del_item; if(stcktop= =-1) return(-1); else del_item=stcksarray[stcktop];//remove top element stcktop--; //decrement top return(del_item); }
22
11/25/2018 Smitha N Pai void display(STACK stck) { int i; if(stck.top==-1) printf(“stack empty”); exit(0); } printf(“contents is”); for(i=0;i<=stck.top;i++) //start from bottom to top printf(“%d”, stck.sarray[i]);
23
11/25/2018 Smitha N Pai void display(STACK *stck) { int i; if(stcktop==-1) printf(“stack empty”); exit(0); } printf(“contents is”); for(i=0;i<=stcktop;i++) //start from bottom to top printf(“%d”, stcksarray[i]);
24
11/25/2018 Smitha N Pai Checking whether given expression is valid or not using stack Checking an expression is nothing but checking 1.Whether there are equal number of right and left parenthesis. 2.Whether right parenthesis is preceded by a matching left parenthesis. If both the above conditions are satisfied, expression is valid. Ex: ((A+B) or A+B( are not valid expressions because they violate condition 1. )a+b(-c violate condition 2. (a+b)) violate both the conditions. (a+b) * (c+d) is a valid expression. Stacks can be used to check this.
25
Algorithm for checking expression
11/25/2018 Smitha N Pai Algorithm for checking expression Scan the expression from left to right. Whenever a scope opener( ‘(‘,’{‘,’[‘ ) is encountered while scanning the expression, it is pushed to stack. Whenever as scope ender( ‘)’, ‘}’, ‘]’) is encountered, the stack is examined. If stack is empty, scope ender does not have a matching opener and hence string is invalid. If stack is non empty, we pop the stack and check whether the popped item corresponds to scope ender. If a match occurs, we continue. If it does not, the string is invalid. When the end of string is reached, the stack must be empty; otherwise one or more scopes have been opened which have not been closed and string is invalid.
26
Ex1:(a+b) * (c+d Ex2:(a+b)*c+d)
11/25/2018 Smitha N Pai Ex1:(a+b) * (c+d Ex2:(a+b)*c+d) ( ( ( ( (a+b) Pop ‘(‘ since there is ‘)’ & continue (a+b) *( Push ‘(‘ (a+b) *(c+d End of string reached but stack not empty. Hence invalid push ‘(‘ ( ( push ‘(‘ (a+b) pop‘(‘ and continue (a+b)*c+d) Stack empty when ‘)’ encountered.hence invalid
27
Ex3: (a+b)*({c*d) ( push ‘(‘ (a+b) pop‘(‘ and continue (a+b)*(
11/25/2018 Smitha N Pai Ex3: (a+b)*({c*d) ( ( ( push ‘(‘ (a+b) pop‘(‘ and continue (a+b)*( push‘(‘ and continue { { ( ( (a+b)*({ push ‘{ and continue‘ (a+b)*({c*d) No match between closing scope ‘)’ and opening scope ‘{‘. Hence invalid.
28
Ex4: (a+{b*c}+(c*d)) ( push ‘(‘ and continue (a+{ push‘{‘ and continue
11/25/2018 Smitha N Pai Ex4: (a+{b*c}+(c*d)) { ( ( ( ( push ‘(‘ and continue (a+{ push‘{‘ and continue (a+{b*c} pop‘{‘ and continue ( ( ( (a+{b*c}+( push ‘( and continue‘ (a+{b*c}+(c*d) Pop ‘(‘ (a+{b*c}+(c*d)) Pop ‘(‘. End of string and stack empty. Hence valid
29
Checking for palindrome
11/25/2018 Smitha N Pai Checking for palindrome Algorithm Scan the string from left to right till the end and push every char you encounter during the scan to the stack. Rescan the string left to right till end and do the following for every char you encounter during scan. Pop one char and compare current char with the popped one. If no match report immediately non palindrome other wise proceed to the next char.
30
Infix, postfix, prefix expressions and stacks Infix expression:
11/25/2018 Smitha N Pai Infix, postfix, prefix expressions and stacks Infix expression: Operators will be between two operands. ex: a+b*c, a+b*c+d, (a+b)*c, (a+b)*(c+d). Postfix expressions Operator follows two operands. ab+, ab*, abc*+ Prefix expressions Operators precedes two operands. These expressions can be converted to other form. i.e infix to prefix, infix to postfix and so on
31
Converting infix to postfix
11/25/2018 Smitha N Pai Converting infix to postfix Infix can be converted to postfix by placing the operator after two operands while scanning from left to right. a+bab+, a*bab* When there are multiple operators and parentheses, precedence of operators comes into picture. Operations with highest precedence is considered first for conversion and converted part is considered as single unit(operand) and process is continued. Operations within the parenthesis is considered first for conversion because parentheses have highest precedence. For operators, order precedence is given below 1.exponentiation($ or ^) 2.multiplication/division 3.addition
32
Ex1: a+b. c Here. is having higher precedence than +. So
Ex1: a+b*c Here * is having higher precedence than +. So * is considered first which gives bc*. Now bc* is considered as a single unit. i.e a + bc*. Here a and bc* are considered as separate units. Finally converting the intermediate expression gives abc*+ Ex2:(a+b)*c operation within parenthesis is considered first which gives ab+ . Now ab+ and c are 2 separate operands i.e ab+ * c Converting this intermediate expression will give ab+c* Ex3: (a+b)*(c+d) After converting first parenthesis we get ab+ *(c+d). After converting 2nd parenthesis we get ab+ * cd+. After converting the the above expression we get ab+cd+* 11/25/2018 Smitha N Pai
33
converting this intermediate expression will give us ab+c+ Ex2: a$b$c
When operators with same precedence are scanned, the order is assumed to be left to right, except in the case of exponentiation, where the order is right to left. Ex1: a+b+c Here both the operators are + and have same precedence. Hence considering from left to right we get ab+ + c. converting this intermediate expression will give us ab+c+ Ex2: a$b$c Here also $ have same precedence. In case of $, order is from right to left. Hence considering from right we get a $ bc$. Now convert this intermediate expression. Hence we get abc$$ Ex3:a*b/c * and / have same precedence. Order is from left to right. Hence we ab* / c. finally we get ab*c/ 11/25/2018 Smitha N Pai
34
11/25/2018 Smitha N Pai Converting infix with multiple operators to postfix Ex1: a$b*c-d+e/f/(g+h) 1.a$b * c - d + e / f / gh+ ab$*c – d + e / f / gh+ 2.ab$c* - d + e / f / gh+ 3.ab$c*d- + e / f / gh+ 4.ab$c*d- + ef/ / gh+ 5.ab$c*d- + ef/ / gh+ 6.ab$c*d- + ef/gh+/ 7.ab$c*d-ef/gh+/+
35
11/25/2018 Smitha N Pai Ex2: ((a+(b-c)*d)$e+f) 1.( ( a + bc- * d ) $ e + f ) 2.( ( a + bc-d* ) $ e + f ) 3.( abc-d*+ $ e + f ) 4.abc-d*+e$ + f 5.abc-d*+e$f+
36
Converting infix to prefix
11/25/2018 Smitha N Pai Converting infix to prefix Infix can be converted to prefix by placing the operator before two operands. Precedence rules is same as in the case infix to postfix. Ex:a+b+ab a+b*c+a*bc (a+b)*c*+ab a+b-c-+abc a*b/c/*abc a$b$c$a$bc (a+b)*(c+d)*+ab+cd
37
11/25/2018 Smitha N Pai Converting infix with multiple operators to prefix Ex1:((a+(b-c)*d)$e+f) ( ( a + -bc * d ) $ e + f ) ( ( a + *-bcd ) $ e + f ) ( +a*-bcd $ e + f ) ( $+a*-bcde + f ) +$+a*-bcdef Ex2:a-b/(c*d$e) a - b / ( c * $de ) a - b / *c$de a - /b*c$de -a/b*c$de
38
Another method a$b*c-d+e/f/(g+h)
11/25/2018 Smitha N Pai Another method a$b*c-d+e/f/(g+h) Put all implicit brackets.(According to precedence and associativity) ((((a$b)*c)-d)+((e/f)/(g+h))) Replace every ) with corresponding operator and remove ( ab$c*d-ef/gh+/+ 5. Converting to prefix requires change of roles of ( and ) in step 3. 6. +-*$abcd//ef+gh
39
11/25/2018 Smitha N Pai Evaluating postfix expression using stack Steps: 1.Scan the postfix expression from left to right 2.If an operand is encountered, push it on to the stack. 3.If an operator is encountered, pop the top two elements from the stack, perform the required operation based on operator and push the result back on stack. 4.Perform this operation until end of expression is reached. 5.At the end, stack contains the final result.
40
11/25/2018 /+*2$3+ Symbol op1 op2 op1 operator op2 stack ,2 3 6,2, , ,3 8 1,3,8 2 1,3,8,2 / ,3, ,7 * ,2 $ , Smitha N Pai
41
11/25/2018 Smitha N Pai 23+4* Symbol op1 op2 op1 operator op2 stack , ,4 *
42
11/25/2018 Smitha N Pai /*program to evaluate postfix expression*/ #include<stdio.h> #include<math.h> #include<string.h> struct stack { int top; int items[15]; }; typedef struct stack STACK; void main() int op1,op2,I,result; char postfix[15], symbol; STACK s; s.top=-1;
43
11/25/2018 Smitha N Pai printf(“enter postfix expression”); scanf(“%s”,postfix); for(i=0;i<strlen(postfix);i++) { symbol=postfix[i]; if(isdigit(symbol)) // call isdigit function push(symbol-’0’,&s); else op2=pop(&s); op1=pop(&s); result=op(symbol,op1,op2); //call op function push(result,&s); } result =pop(&s); printf(“result is %d”,result);
44
11/25/2018 Smitha N Pai /*function op for performing operation on op1 and op2*/ int op(char symbol, int opnd1, int opnd2) { switch(symbol) case ‘+’: return(opnd1+opnd2); break; case ‘-’: return(opnd1-opnd2); case ‘*’: return(opnd1*opnd2); case ‘/’: return(opnd1/opnd2); case’$’ case ‘^’: return(pow(opnd1,opnd2)); }
45
11/25/2018 Smitha N Pai void push(int symb, STACK *stk) //push to stack { stktop++; stkitems[stktop]=symb; } Int pop(STACK *stk) /pop from stack int item; item=stkitems[stktop--]; return item; Int isdigit(char symb) //check symbol is digit or not if(symb>=‘0’ && symb<=‘9’) return 1; else return 0;
46
11/25/2018 Smitha N Pai Converting infix to postfix Steps: 1.Scan the infix string from left to right. 2.If the symbol is operand, add it to postfix string. 3.Else 1. If current symbol is ‘(‘, push it to stack, whatever the top of stack may be. 2. If ‘(‘ is top of the stack and current symbol is any operator, push the symbol to stack. 3. If current symbol is ‘)’, pop the stack until the first ‘(‘ is encountered and add the popped elements to postfix string except ‘(‘.
47
11/25/2018 Smitha N Pai 4.If current symbol is an operator, compare the precedence of current symbol with the top of stack. If the precedence of current symbol is higher, push it to stack. Otherwise pop the stack until precedence of current symbol is greater than the top of stack and add all popped elements to postfix string and current symbol to stack. 5.Repeat this process until end of infix string is reached. 6.At the end, pop whatever is remained in stack to postfix string.
48
11/25/2018 Smitha N Pai Ex1: a+(b*c) Steps current symbol postfix str stack 1 a a 2 + a + 3 ( a +( 4 b ab +( 5 * ab +(* 6 c abc +(* 7 ) abc* + End of string Now pop whatever is remained on top of stack and to postfix string. Hence final postfix string is abc*+
49
11/25/2018 Smitha N Pai Ex2:((a-(b+c))*d)$(e+f) Steps current symbol postfix str stack 1. ( ( 2. ( (( 3. a a (( 4. - a ((- 5. ( a ((-( 6. b ab ((-( 7. + ab ((-(+ 8. c abc ((-(+ 9. ) abc+ ((- 10. ) abc+- ( 11. * abc+- (* 12. d abc+-d (*
50
11/25/2018 Smitha N Pai Steps current symbol postfix str stack 13. ) abc+-d* 14. $ abc+-d* $ 15. ( abc+-d* $( 16. e abc+-d*e $( abc+-d*e $(+ 18. f abc+-d*ef $(+ 19. ) abc+-d*ef+ $ End of string Pop whatever is remained on stack and add to postfix string. abc+-d*ef+$
51
11/25/2018 Smitha N Pai a*b+c*d+e*f Steps current symbol postfix str stack 1. a a 2. * a * 3. b ab * 4. + ab* + 5. c ab*c + 6. * ab*c +* 7. d ab*cd +* 8. + ab*cd* e ab*cd*+e * ab*cd*+e +* 11. f ab*cd*+ef +* End of string Pop * and + from stack and add to postfix string : ab*cd*+ef*+
52
Algorithm: Opstk=the empty stack While(not end of input) { symb=next input character. if(symb is an operand) add symb to postfix string. else while(!empty(opstk) && prcd(stacktop(opstk),symb)) topsymb=pop(opstk); add topsymb to postfix string; } if(empty(opstk)|| symb!=‘)’) push(symb,opstk); else //to discard open parenthesis 11/25/2018 Smitha N Pai
53
11/25/2018 Smitha N Pai /* whatever is remained in stack is popped and added to string*/ While(!empty(opstk)) { topsymb=pop(opstk); add topsymb to postfix string; } /*precedence function*/ prcd(‘(‘,op)=FALSE, for any operator. prcd(op,‘(‘)=FALSE, for any operator other than ‘)’. prcd(op,’)’)=TRUE, for any operator other than ‘(‘.
54
Converting infix to prefix: Reverse the given infix expression.
11/25/2018 Smitha N Pai Converting infix to prefix: Reverse the given infix expression. Convert the reversed expression to an intermediate form using stack.( algorithm in the next slide) Reverse the intermediate expression. This is the prefix of the given infix expression.
55
11/25/2018 Smitha N Pai Converting reversed infix expression to intermediate form: Steps: 1.Scan the reversed infix string from left to right. 2.If the symbol is operand, add it to intermediate string. 3.Else 1. if current symbol is ‘)‘, push it to stack, whatever the top of stack may be. 2. if ‘)‘ is top of the stack and current symbol is any operator, push the symbol to stack. 3. if current symbol is ‘(’, pop the stack until the first ‘)‘ is encountered and add the popped elements to intermediate string except ‘)‘.
56
11/25/2018 Smitha N Pai 4.If current symbol is an operator, compare the precedence of current symbol with the top of stack. If the precedence of current symbol is higher than or equal to, push it to stack. Otherwise pop the stack until precedence of current symbol is greater than top of stack or stack is empty and add all popped elements to intermediate string and current symbol to stack. 5.Repeat this process until end of reversed infix string is reached. 6.At the end, pop whatever is remained in stack to intermediate string.
57
Ex1:((a+(b-c)*d)$e+f) Reversed infix expression: )f+e$)d*)c-b(+a((
11/25/2018 Smitha N Pai Ex1:((a+(b-c)*d)$e+f) Reversed infix expression: )f+e$)d*)c-b(+a(( Steps current symbol intermediate str stack 1. ) ) 2. f f ) f )+ 4. e fe )+ 5. $ fe )+$ 6. ) fe )+$) 7. d fed )+$) 8. * fed )+$)* ) fed )+$)*) c fedc )+$)*) - fedc )+$)*)- b fedcb )+$)*)-
58
Steps current symbol intermediate str stack 13. ( fedcb- )+$)*
11/25/2018 Smitha N Pai Steps current symbol intermediate str stack 13. ( fedcb- )+$)* fedcb- * )+$)+ 15. a fedcb-*a )+$)+ 16. ( fedcb-*a+ )+$ ( fedcb-*a+$+ End of string Pop whatever is remained on stack and add to intermediate stringfedcb-*a+$+ After reversing the intermediate string, we get +$+a*-bcdef , which is the prefix.
59
Ex2: a$b*c-d+e/f/(g+h) Reversed infix expression: )h+g(/f/e+d-c*b$a
11/25/2018 Smitha N Pai Ex2: a$b*c-d+e/f/(g+h) Reversed infix expression: )h+g(/f/e+d-c*b$a Steps current symbol intermediate str stack 1. ) ) 2. h h ) h )+ 4. ( hg+ 5. / hg+ / 6. f hg+f / 7. / hg+f // 8. e hg+fe // + hg+fe// + d hg+fe//d + - hg+fe//d +- c hg+fe//dc +-
60
Steps current symbol intermediate str stack 13. * hg+fe//dc +-*
11/25/2018 Smitha N Pai Steps current symbol intermediate str stack 13. * hg+fe//dc +-* 14. b hg+fe//dcb +-* 15. $ hg+fe//dcb +-*$ a hg+fe//dcba +-*$ End of string Pop whatever is remained on stack and add to intermediate stringhg+fe//dcba$*-+ After reversing the intermediate string, we get +-*$abcd//ef+gh , which is the prefix.
61
Reversed infix expression: )e$d*c(/b-a
11/25/2018 Smitha N Pai Ex3: a-b/(c*d$e) Reversed infix expression: )e$d*c(/b-a Steps current symbol intermediate str stack 1. ) ) 2. e e ) 3. $ e )$ 4. d ed )$ 5. * ed$ )* 6. c ed$c )* 7. ( ed$c* 8. / ed$c* / b ed$c*b / - ed$c*b/ - a ed$c*b/a - end of string
62
11/25/2018 Smitha N Pai Pop whatever is remained on stack to intermediate stringed$c*b/a- After reversing the intermediate string, we get -a/b*c$de, which is the prefix expression
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.