You are on page 1of 10

STACK ALGORITHM FOR INSERTING ELEMENT INTO STACK USING ARRAY PUSH(S, TOP, X) This procedure inserts an element

X to the top of a stack which is represented by a vector S containing N elements with a pointer TOP denoting the top element in the stack. Step 1. [Check for Stack Overflow] If TOP >= N then Write ( STACK OVERFLOW) Return Step 2. [Increment TOP] TOP TOP + 1 Step 3. [Insert element] S[TOP] X Step 4. [Finished] Return ALGORITHM FOR DELETING ELEMENT FROM STACK USING ARRAY POP(S, TOP) This procedure removes the top element from a stack which is represented by a vector S and returns this element. TOP is a pointer to the top element of the stack. Step 1. [Check for Underflow on stack] If TOP = 0 then Write ( STACK UNDERFLOW ON POP) take action in response to underflow Exit Step 2. [Decrement TOP] TOP TOP - 1 Step 3. [Return former top element of stack] Return ( S[TOP + 1] )

ALGORITHM FOR RETRIEVING ELEMENT FROM STACK USING ARRAY PEEP(S, TOP, I) Given a vector S (consisting of N elements) representing a sequentially allocated stack, and a pointer TOP denoting the top element of the stack, this function returns the value of the Ith element from the top of the stack. The element is not deleted by this function. Step 1. [Check for Stack Underflow] If TOP I + 1 <= 0 then Write ( STACK UNDERFLOW ON PEEP) take action in response to underflow Exit Step 2. [Return Ith element from top of stack] Return ( S[TOP I + 1] ) ALGORITHM FOR CHANGING VALUE OF ITH ELEMENT FROM TOP OF STACK USING ARRAY CHANGE(S, TOP, X, I) Given a vector S (consisting of N elements) representing a sequentially allocated stack, and a pointer TOP denoting the top of element of the stack, this function changes the value of the Ith element from the top of the stack to the value contained in X. Step 1. [Check for Stack Underflow] If TOP I + 1 <= 0 then Write ( STACK UNDERFLOW ON CHANGE) Return Step 2. [Change Ith element from top of stack] S[TOP I + 1] X Step 3. [Finished] Return

ALGORITHM FOR INSERTING ELEMENT INTO STACK USING LINKED LIST PUSH(TOP, X). This function inserts X, a new element, into the stack. X is the variable which will store the value item that we want to push on the stack TOP is the pointer which will point to particular node having INFO & LINK portion. NEW is a temporary pointer variable of Node Type AVAIL is a pointer to the top element of the availability stack Step 1. [Underflow? / Available space] If AVAIL = NULL then Write ( AVAILABILITY STACK UNDERFLOW) Exit Step 2. [Obtain address of next free node] NEW AVAIL Step 3. [Remove free node from availability stack] AVAIL LINK[AVAIL] Step 4. [Assign Data to INFO portion of New Node] INFO[NEW] X Step 5. [Assign Address of TOP to LINK portion of NEW node] LINK[NEW] TOP Step 6. [Assign Address of New Node to TOP pointer] TOP NEW Step 7. [Finished] Return

ALGORITHM FOR DELETING ELEMENT FROM STACK USING LINKED LIST POP(TOP, X). This function deletes the top element from a linked stack. X is the variable which will store the value item that we want to delete from the stack TOP is the pointer which will point to particular node having INFO & LINK portion. TEMP is a temporary pointer variable of Node Type AVAIL is a pointer to the top element of the availability stack Step 1. [Stack has an element to be removed?] If TOP = NULL then Write ( UNDERFLOW) Exit Step 2. [ Assign the Address of Node pointed by Node pointer to TEMP ] TEMP TOP Step 3. [Assign value of INFO portion to variable X] X INFO[TEMP] Step 4. [Move the pointer to its predecessor] TOP LINK[TEMP] Step 5. [Delete the Node which is pointed by TEMP] (i) [Assign LINK of a TEMP pointer to node which is pointed by AVAIL pointer] LINK[TEMP] AVAIL

(ii) [Add free node, TEMP, to availability stack] AVAIL TEMP Step 6. [Finished] Return

Q-1. Write an algorithm which will check that the given string belongs to following grammar or not L={ wcwR | w {a,b}*}(Where wR is the reverse of w) RECGONIZE. Given an input string named STRING on the alphabet {a, b, c} which contains a blank in its rightmost character position, and a function NEXTCHAR which returns the next symbol in STRING, this algorithm determines whether the contents of STRING belong to the above language. The vector S represents the stack, and TOP is a pointer to the top element of the stack. Step 1. [Initialize stack by placing a letter c on the top] TOP 1 S[TOP] c Step 2. [Get stack symbols until c or blank is encountered] NEXT NEXTCHAR(STRING) Repeat while NEXT c IF NEXT = then Write(INVALID STRING) Exit else Call PUSH(S, TOP, NEXT) NEXT NEXTCHAR(STRING) Step 3. [Scan characters following c; compare them to characters on stack] Repeat while S[TOP] c NEXT NEXTCHAR(STRING) X POP(S, TOP) IF NEXT X then Write(INVALID STRING) Exit Step 4. [Next symbol must be a blank] NEXT NEXTCHAR(STRING) IF NEXT = then Write(VALID STRING) else Write(INVALID STRING) Step 5. [Finished] Exit

Q-2. Write an algorithm which will check that the given string belongs to following grammar or not L={ wcw | w {a,b}*} RECGONIZE. Given an input string named STRING on the alphabet {a, b, c} which contains a blank in its rightmost character position, and a function NEXTCHAR which returns the next symbol in STRING, this algorithm determines whether the contents of STRING belong to the above language. The vector S represents the stack, and TOP is a pointer to the top element of the stack. LOC is an integer value to move down from the top of the stack. Step 1. [Initialize stack by placing a letter c on the top] TOP 1 S[TOP] c Step 2. [Get stack symbols until c or blank is encountered] NEXT NEXTCHAR(STRING) Repeat while NEXT c IF NEXT = then Write(INVALID STRING) Exit else Call PUSH(S, TOP, NEXT) NEXT NEXTCHAR(STRING) Step 3. [Scan characters following c; compare them to characters at bottom of stack] LOC TOP Repeat while LOC 1 LOC LOC-1 NEXT NEXTCHAR(STRING) X PEEP(S, TOP,LOC) IF NEXT X then Write(INVALID STRING) Exit Step 4. [Next symbol must be a blank] NEXT NEXTCHAR(STRING) IF NEXT = then Write(VALID STRING) else Write(INVALID STRING) Step 5. [Finished] Exit

ALGORITHM TO CONVERT INFIX TO POSTFIX(RPN) WITHOUT USING RANK FUNCTION REVPOL Given an input string INFIX containing an infix expression which has been padded on the right with ) and whose symbols have precedence values given by Table 1, a vector S, used as a stack, and a function NEXTCHAR, which, when invoked, returns the next character of its argument. This algorithm converts INFIX into Reverse Polish and places the result in the string POLISH. The integer variable TOP denotes the top of the stack. The string variable TEMP is used for temporary storage purposes. Step 1. [Initialize stack] TOP 1 S[TOP] ( Step 2. [Initialize output string] POLISH Step 3. [Get first input symbol] NEXT NEXTCHAR(INFIX)

Step 4. [Translate the infix expression] Repeat thru step 7 while NEXT Step 5. [Remove symbols with greater precedence from stack] IF TOP < 1 then Write(INVALID) Exit IF f(NEXT) = -1 then POLISH POLISH NEXT ELSE Repeat while f(NEXT) < g(S[TOP]) TEMP POP(S,TOP) POLISH POLISH TEMP

Step 6. [Are there matching parentheses?] IF f(NEXT) = g(S[TOP]) then POP(S, TOP) else IF f(NEXT) -1 then Call PUSH(S, TOP, NEXT) Step 7. [Get next input symbol] NEXT NEXTCHAR(INFIX)

Step 8. [Is the expression valid?] IF TOP 0 then Write(INVALID) else Write(VALID) Exit Table 1 Symbol + * / variables ( ) Precedence Input Precedence Stack Precedence Function f Function g 1 2 3 4 6 5 -1 7 0 0 -

ALGORITHM TO CONVERT INFIX TO POSTFIX(RPN) USING RANK FUNCTION REVPOL Given an input string INFIX containing an infix expression which has been padded on the right with ) and whose symbols have precedence values given by Table 2, a vector S, used as a stack, and a function NEXTCHAR, which, when invoked, returns the next character of its argument. This algorithm converts INFIX into Reverse Polish and places the result in the string POLISH. The integer variable TOP denotes the top of the stack. The integer variable RANK accumulates the rank of the expression. The string variable TEMP is used for temporary storage purposes. Step 1. [Initialize stack] TOP 1 S[TOP] ( Step 2. [Initialize output string and rank count] POLISH RANK 0 Step 3. [Get first input symbol] NEXT NEXTCHAR(INFIX) Step 4. [Translate the infix expression] Repeat thru step 7 while NEXT Step 5. [Remove symbols with greater precedence from stack] IF TOP < 1 then Write(INVALID) Exit IF f(NEXT) = -1 then POLISH POLISH NEXT RANK RANK + r(NEXT) ELSE Repeat while f(NEXT) < g(S[TOP]) TEMP POP(S,TOP) POLISH POLISH TEMP RANK RANK + r(TEMP) [End of If Structure]

IF RANK < 1 then Write(INVALID) Exit Step 6. [Are there matching parentheses?] IF f(NEXT) = g(S[TOP]) then POP(S, TOP) else IF f(NEXT) -1 then Call PUSH(S, TOP, NEXT) Step 7. [Get next input symbol] NEXT NEXTCHAR(INFIX)

Step 8. [Is the expression valid?] IF TOP 0 or RANK 1 then Write(INVALID) else Write(VALID) Exit Table 2 Symbol + * / variables ( ) Precedence Input Precedence Stack Precedence Rank Function f Function g Function r 1 2 -1 3 4 -1 6 5 -1 -1 1 7 0 0 -

You might also like