Stack
Stack is the list of elements in
which an element may be inserted or deleted only at one end, called the TOP of the stack. New nodes can be added and removed only at the top. Similar to a pile of dishes. Last-in, first-out (LIFO). Eg: A stack of books. When a person wear bangles the
last bangle worn is the first one to be removed and the first bangle would be the last to be removed.
Special terminology used for two basic operations
associated with stacks: PUSH is the term used to insert an element into a stack POP is the term used to delete an element from a stack.
Array/Static representation of STACKS
x 1 TOP 3 y z
8
9
MAXSTK
Push operation
PUSH(STACK,TOP,MAXSTK,ITEM) 1. IF TOP=MAXSTK then print OVERFLOW and return 2. Set TOP=TOP+1[increase top by one] 3. Set STACK[TOP]=ITEM, Increase ITEM in new TOP position 4. return
POP operation
POP(STACK,TOP,ITEM)
1. IF TOP=0, PRINT underflow and return 2. Set ITEM=STACK[TOP] [Assign top element to
item] 3.Set TOP=TOP-1 [Decrease TOP By one] 4. Return
Linked List/ Dynamic representation of Stack
PUSH_LINKSTACK(INFO,LINK,TOP,AVAIL, ITEM) 1. If AVAIL=NULL , then write OVERFLOW and exit. 2. [Remove first node from AVAIL list] Set NEW=AVAIL and AVAIL=LINK[AVAIL]. 3.Set INFO[NEW]=ITEM [Copies ITEM into new node] 4. Set LINK[NEW]=TOP[New node points to the original TOP node in the stack] 5. Set TOP=NEW[Reset TOP to point to the new node at the TOP of the stack] 6.Exit
POP_LINKSTACK(INFO,LINK,TOP,AVAIL,ITEM) 1.If TOP=NULL then write UNDERFLOW and exit. 2. Set ITEM=INFO[TOP] [Copies the TOP element of stack
into ITEM] 3. Set TEMP=TOP andTOP=LINK[TOP] [Remember the old value of the Top pointer in TEMP and reset TOP to point to the next element in the stack] 4.[Return delete node to the AVAIL list] Set LINK[TEMP]=AVAIL and AVAIL=TEMP
5. Exit
Infix notation: In this operator is placed in
between two operands. Eg (A+B)*C Polish/prefix notation: In this operator symbols are placed before two operands. Eg: +AB, *EF Reverse Polish/postfix /suffix notation: Operator symbol is placed after its two operands. Eg AB*, CD/
Transforming Infix Expression into prefix/polish notation
Eg1. Infix expressing: (A+B)*C
[+AB]*C Now [+AB] will act as operand *+ABC Eg2. Infix expressing: A+(B*C) A+[*BC] Now [*BC] will act as operand +A*BC
Calculate prefix notation of following infix expression
(A+B)/(C-D)
Evaluation of Postfix expression
P: 5,6,2,+,*,12,4,/,Symbol Scanned 5 6 STACK 5 5,6
2
+ * 12 4
5,6,2
5,8 40 40, 12 40,12,4
/
)
40,3
37
Transforming Infix expression to postfix expression
Table is divided into 3 parts symbol scanned(Q), STACK, expression P 1.Push ( onto stack and add ) to the end of Q 2. If operand is encountered in symbol scanned (Q) , add it to P. Eg
A,B,C.. 3. If left parenthesis ( is encountered, push it on to STACK 4. If operator is encountered: A) Push operator into the stack if it has lower precedence than that of next operator. B) Add operator to P if it has equal or higher preference than that of next operator. Eg: +* : + has lower precedence than * so + will remain in stack Eg: *+ : * has higher precedence than + so * will get added to P.
Transforming Infix expression to postfix expression
Q: A+(B*C-(D/E^F)*G)*H
Symbol Scanned A + ( B * C ( D / E ^ F STACK ( (+ (+( (+( (+(* (+(* (+((+(-( (+(-( (+(-(/ (+(-(/ (+(-(/^ (+(-(/^ Expression P A A A AB AB ABC ABC* ABC* ABC*D ABC*D ABC*DE ABC*DE ABC*DEF
Q:
A*b+(C+D)-(A/B)/C