Professional Documents
Culture Documents
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.
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.
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
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
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/
2
+ * 12 4
5,6,2
5,8 40 40, 12 40,12,4
/
)
40,3
37
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.
Q:
A*b+(C+D)-(A/B)/C