190 likes | 275 Views
The Stack. Alexander G. Chefranov CMPE-231 Spring 2012. Definition and Examples. A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end, called the top of the stack.
E N D
The Stack Alexander G. Chefranov CMPE-231 Spring 2012
Definition and Examples • A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end, called the top of the stack. • Unlike array, stack is a dynamic, constantly changing object • Last-In-First-Out (LIFO) list
Primitive Operations When an item is added to a stack, it is pushed onto the stack, push(s,i) When an item is removed, it is popped from the stack, i=pop(s) Stack is also called a pushdown list If a stack contains a single item, and the stack is popped, the resulting stack contains no items and is called the empty stack Pop operation can’t be applied to the empty stack Empty(s) determines whether or not the stack is empty (returns TRUE if empty, FALSE otherwise) i=Stacktop(s) returns the top element of stack s without removing it (equivalent to i=pop(s); push(s,i);) Not defined for the empty stack The result of an illegal attempt to pop or access an item from an empty stack is called underflow
Example 7- ( ( x*( ( x+y) / ( j - 3) )+ y) /( 4 -2 .5 ) ) 0012223444433444432221122222210 Check that there are an equal number of right and left parenthesis, and every right parenthesis is preceded by a matching left parenthesis Each left parenthesis opens a scope, each right parenthesis closes a scope The nesting depth at a point is the number of scopes that have been opened but not yet closed at the point in an expression. Parenthesis count = number of left parenthesis – number of encountered right parenthesis • The parenthesis count at the end of the expression is 0 • The parenthesis count at each point in the expression is nonnegative
Example (cont) {x+(y-[a+b])*c-[(d+e)]}/(h-(j-(k-[l-n]))) Valid=true; S=the empty stack; While(we have not read the entire string){ read the next symbol(symb) of the string; if(symb in {‘(‘,’[‘,’{‘}) push(s, symb); if(symb in {‘)’,’]’,’}’)) if(empty(s))valid=false; else{ i=pop(s); if(I is not the matching opener of symb) valid=false; }/*end else*/ }/*end while*/ If(!empty(s)) valid=false; if(valid) printf)”The string is valid”; Else printf(“the string is invalid”);
The Stack as an Abstract Data Type Abstract typedef <<eltype>> STACK(eltype); Abstract empty(s) STACK(eltype) s; Postcondition empty==(len(s)==0); Abstract eltype pop(s) STACK(eltype) s; Precondition empty(s)==FALSE; Postcondition pop==first(s’);s==sub(s’,1,len(s’)-1); Abstract push(s,elt) STACK(eltype) s; Eltypeelt; Postcondition s==<elt>+s’;
Representing Stacks in C Array is static, stack is dynamic An array can be the home for a stack #define STACKSIZE 100 Struct stack{ int top; int items[STACKSIZE]; } Struct stack s;
Representing Stacks in C (cont 1) #define STACKSIZE 100 #define INT 1 #define FLOAT 2 #define STRING 3 Struct stackelement{ int type; union{ int ival; float fval; char *pval; } element Struct stack{ int top struct stackelement items[STACKSIZE]; }
Representing Stacks in C (cont 2) Struct stack s; Struct stackelement se; Se=s.items[s.top]; Switch(se.type){ Case INT: printf(“%d\n”, se.ival); Case FLOAT: printf(“%f\n”, se.fval); Case STRING: printf(“%s\n”, se.pval); } For simplicity, in the remainder, we assume that a stack is declared to have only homogeneous elements (so that unions are not necessary) The empty stack contains no elements (s.top==-1) Int empty(struct stack *ps){ if(ps->top==-1) return TRUE; else return FALSE; }
Representing Stacks in C (cont 3) Use of empty() is better than ps->top==1 as not implementation depending Modularization: individual functions are isolated into low-level modules whose properties are easily verifiable If precautions are taken at the time that a program is written that it is easily modifiable and comprehensible , the total time needed to get the program to run correctly is reduced sharply The mature professional is constantly concerned with the proper balance between code economy and code clarity
Representing Stacks in C (cont 4) Int pop(struct stack *ps){ if(empty(ps)){ printf(“Stack underflow\n”); exit(1); } return (ps->items[ps->top--]); } .. X=pop(&s);
Representing Stacks in C (cont 5) Void popandtest(struct stack *ps, int *px, int *pund){ if(empty(ps)){ *pund=TRUE; return; } *pund=FALSE; *px=ps->items[ps->top--]; return; } Popandtest(&s,&x,&und); If(und)/*take corrective actions*/ Else /*use value of x*/
Representing Stacks in C (cont 6) Void push(struct stack *ps, int x){ ps->items[++(ps->top)]=x; return; } Void push(struct stack *ps, int x){ if(ps->top==STACKSIZE-1){ printf(“Stack overflow\n”); exit(1); } ps->items[++(ps->top)]=x; return; }
Representing Stacks in C (cont 7) Pushand test(&s,x,&overflow); If(overflow)/*take remedial action*/ Else /*continue processing*/ Int stacktop(struct stack *ps){ if(empty(ps)){ printf(“Stack underflow\n”); exit(1); } else return (ps->items[ps->top]); }
Example: Infix, Postfix, and Prefix Notations + a b A b + Exponentiation a$b$c=a$(b$c) Multiplication/division Addition/subtraction a+b+c=(a+b)+c Prcd(‘*’,’+’)==TRUE, prcd(‘+’,’+’)==TRUE, Prcd(‘$’,’$’)==FALSE prcd(op1,op2) returns TRUE if op1 has precedence over op2 when op1 appears to the left of op2 in an infix expression without parenthesis, and FALSE otherwise
Infix to Postfix Conversion a*b+c*d A+b*c$d$e Prcd(‘(‘,op)=FALSE for any op Prcd(op,’(‘)=FALSE for any op other than ‘)’ Prcd(op,’)’)=TRUE for any op other than ‘(‘ Prcd(‘)’,op)=undefined for any op (error) 10 if(empty(opstk)||symb!=‘)’) push(opstk,symb); Else /*pop the open parenthesis and discard it*/ topsymb=pop(opstk); ((a-(b+c))*d)$(e+f)