IMG_20130817_0001_merged_merged

April 20, 2018 | Author: Prince Vineeth | Category: Bracket, String (Computer Science), Pointer (Computer Programming), Notation, Mathematical Notation
Share Embed Donate


Short Description

hey ds notes singly linked list is missing...

Description

 Applicati  Appli cations ons of Stack: S tack: A) Postfix evaluation In a postfix expression Operators are written after their operands. Eg: ab+c* . Computers do the computation of any an y expression using this postfix evaluation only. The postfix evaluation algorithm is as follows: • Initialise an empty stack 1 2 • While a character remain in the input stream 1 2  – Read next character  3  – If character is a number, number, push it into the stack 4  – Else, if character is an operator, operator, pop top two characters off the the stack, apply the operator, and push the answer back into the stack

3 4

• In the end, Pop the answer off the stack and print the answer.

Example: Let us see how the above algorithm will be imlemented using an example. Consider a Postfix String : 123*+4Initially the Stack is empty. empty. Now, the first three characters scanned scanned are 1,2 and 3, which are operands. Thus they will be pushed into the stack in that order. order.

Expression

Stack 

Next character scanned is "*", which is an operator. operator. Thus, we pop the top two elements from the stack and perform the "*" operation with the two operands. The second operand operand will be the first element that is popped.

Expression

Stack 

The value of the expression(2*3) that has been evaluated(6) is pushed into the stack.

Expression

Stack 

Next character scanned is "+", which is an operator. Thus, we pop the top two elements from the stack and perform the "+" operation with the two operands. The second operand will be the first element that is popped.

Expression

Stack 

The value of the expression(1+6) that has been evaluated(7) is pushed into the stack.

Expression

Stack 

Next character scanned is "4", which is added to the stack.

Expression

Stack 

Next character scanned is "-", which is an operator. Thus, we pop the top two elements from the stack and perform the "-" operation with the two operands. The second operand will be the first element that is popped.

Expression

Stack 

The value of the expression(7-4) that has been evaluated(3) is pushed into the stack.

Expression

Stack 

Now, since all the characters are scanned, scanned, the remaining element in the stack (there will be only one element in the stack) will be returned. End result :  Postfix String : 123*+4 Result : 3 The Coding for the operation: The function will not return any value and hence data type void is used. *s is a character pointer which will be pointing to the expression that is passed as argument.

void evaluate_postfix(char *s) { int i,a,op1,op2; i,a,op1,op2; for(i=0;s[i]=’\0’;i++) { a=toascii(s[i]);

if(a>=48 && a= priority(in[i])) priority(in[i])) {  pop();  post[pcount]=ch;  pcount++; }  push(in[i]); }// end of else if  }// end of for  while(!isempty()) {  post[pcount]=top();  pcount++;  pop; }  post[pcount]=’\0’; return (post); }

C) Balancing Symbols If you do not balance mathematical expressions, then you will get an error. Given a mathematical expression such as "{[(A+B)-(c-d)]/6}*5", each "{", "[", and "(" must be balanced with a ")", "]", and "}" in the proper order (so expressions like "[A-(B+D])," "{d-e}+(f-g)]," "{d-e}+(f-g)]," and "((a+c)" are not balanced. To check that every right brace, bracket, and parentheses must correspond to its left counterpart.  –  e.g. [( )] is legal, but legal,  but [( ] ) is illegal The algorithm for balancing symbols is as follow: Make an empty stack. (1) (2) Read characters until end of f il ile i. If the the cha chara racte cterr is an open openin ing g symb symbol ol,, push push it it onto onto the the sta stack ck ii. ii. If it it is a clos closin ing g symbol, then if the stack is empty, empty, report an error  iii. iii. Otherw Otherwise ise,, pop pop the the stack. stack. If the symbol symbol popped popped is is not not the the corre correspo spondi nding ng openi opening ng symbo symbol, l, then report an error  (3) At end of  file, file, if  the stack is not empty, report an error. Example: Consider this string of parentheses: "( ( ) ( ) )"



Initially, Initially, the stack is empty



After scanning the first character in the string:



The next char is another left paren:



The next char is a right paren that matches the second char:



The next char is a left paren:



The fifth char is a right paren that matches the fourth char:



Finally, Finally, the last char is a right paren p aren that matches the first char:

For instance, in the end if the stack is not empty, will imply that the symbols are not balanced. The coding is as follows: The function does not return any an y value and has the character c haracter pointer to the expression string as its argument. void balance_symbols(char *c) { int I; for(i=0;c[i]!=’\0’;i++) { switch(c[i]) { case ‘[‘: case ‘(‘: case ‘{‘: case ‘’: ch=top(); if(ch!=’
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF