prepared by:mitali sonar -...
Post on 04-May-2018
216 Views
Preview:
TRANSCRIPT
Prepared By:Mitali Sonar
We are allowed to delete an element from
and insert an element to any position in
list.
A subclass of a list that permits insertion
and deletion of an element to occur at
only one end is called a stack.
Ex:= shunting of trains in a railway yard
IN OUT
Suppose There is a railway
yard with a single track.
Trains enter into railway
yard for placement When
they exit it just in opposite
order in which they entered.
The Last train comes out
first.
Ex:- Plates on a Tray
Ex:- Racks of Books
The stack is a data structure which works on a
principal of
LAST IN FIRST OUT(LIFO)
So we can remove elements in opposite order
from which they are added to stack such a list is
referred as LIFO.
PUSH
POP
TOP OF THE STACK
First we have to allocate a memory block to accommodate stack then from the first location of memory, items of the stack can be stored in sequential form.
:
:
ITEM I
:
:
ITEM 2
ITEM 1
TOP
l
l + 1
l + i -1
u
Size = l + u -1
l and u denotes lower and
upper bounds of array.
LINKED LIST REPRESENTATION :
Sometime size of a stack may vary during the
execution of a program At that time Link list is useful.
In link list there are two fields
DATA field for ITEMS
LINK field points to the next item
Here top of stack is represented by the first item in the list
What is major advantage of link list
structure over array implementation of
stack?
With array implementation we have to
preallocate the maximum stack size in link list
there is no need for stack size
With array we must check for stack overflow but
with link list we don’t need this
LL allows sequential access. array allows
random access.
Insertion/ Deletion of values in arrays are very
expensive compared to link list and requires
memory reallocation.
Insertion operation is referred as “PUSH”
Deletion operation is referred as “POP”
These operations are performed at one end of stack & often known as top of stack.
A pointer TOP keeps track of top element in the stack.
When stack is empty TOP has value zero.
Stack has one element then top has value 1 and so on.
Each time when a new element is added in to stack top pointer is incremented by 1.The pointer is decremented by 1 each time deletion is made from stack.
TOP
A TOP A
TOPB
A
TOP
B
C
INSERTION INTO STACK
A
B
C TOP
A
TOPB
A TOP
DELETIONS FROM THE STACK
TOP
PUSH(S, TOP, X):-
S:- stack with N elements
TOP:- pointer denoting top element in the stack.
X:- is the element inserted top of the stack
[1] [Check stack overflow]
if TOP>=N then
Write(“stack is overflow”)
Return
[2] [Increment TOP]
TOP TOP + 1
[3] [insert an element]
S[TOP] X
[4] [finished]
return
POP(S,TOP):-
S:- Stack S with N element
TOP –Denotes TOP element of the stack
1.[check for underflow]
If TOP=0
Then write(“stack underflow”)
exit
2.[Decrement pointer]
ITEM=A[TOP]
TOP TOP-1
3.[Return to top element of stack]
Return
APPLICATION OF STACK:-
Stacks are used where last in first out
principal is required like reversing string,
checking whether the arithmetic expression
is properly parenthesized, Evaluation of
postfix expression, implementing recursion
And function calls
Recursion
Polish notations
Stack machines
Evaluation of arithmetic Expression
Tower of Hanoi Problem
RECURSION :-
A procedure that contains procedure call to
second procedure which eventually call first
procedure OR
A procedure that contains call to itself is
known as recursion
There are two important conditions that
must be satisfied
1) Each time a procedure call itself it must be
nearer in some sense to solution
2) There must be a condition for stopping the
recursion process.
In each recursive call the current value of
parameter, local variables and return address
from the call are required to be stored. For
storing all these values stack is maintained.
1st CALL
1st CALL
2nd CALL
1st CALL
2nd CALL
3RD CALL
Memory Allocation
FACTORIAL(N)= 1 IF n=0
n * factorial(n-1) otherwise
The factorial of a given positive number n is
defined as the product of all number from 1 to n
denoted n!.
N!= 1* 2* 3* 4* …………………………..*(n-1)*n
ALGORITHM :-FACTORIAL – Given an integer N
this algorithm computes N!
N- integer number, A – stack
RET-ADDR-Contains current return address
TEMP_REC- Contains two var (PARAM & ADDRESS)
PARAM is assigned a value of N
ADDRESS is assigned value of RET_ADDR.
1.[Save N & return address]
call push(A, TOP, TEMP_REC)
2.[Is base criteria found?]
if N=0 then FACTORIAL 1
go to step-4
else PARAM N-1, ADDRESS Step 3
Goto step-1
3.[Calculate N!]
FACTORIAL N* FACTORIAL
4.[Restore previous N & return address]
TEMP_REC POP(A,TOP)
goto ADDRESS
2
Main
address
Step 1: PUSH(A , 0 (2,main address))
Step 2: N != 0
PARAM 1, ADDR step 3
TOP
Step 1: PUSH(A , 1 (1,step 3))
Step 2: N != 0
PARAM 0, ADDR step 3
2 1
Main
address
Step 3
TOP
Step 1: PUSH(A , 2 (0,step 3))
Step 2: N = 0
FACTORIAL 1
2 1 0
Main
address
Step 3 Step 3
TOP
Step 3: FACTORIAL 1 * 1
Step 4: POP(A,2)
go to step 3
2
Main
address
TOP
Step 3: FACTORIAL 2 * 1
Step 4: POP(A,1)
go to main address
TOP
Step 4: POP (A,3) Go to step 32 1
Main
address
Step 3
TOP
Polish Notation
INFIX – Operator comes between operand
operand operator operand
a + b, a * b ,a / b
PREFIX- operator comes before operand
operator operand operand
+ab, *ab, /ab
POSTFIX – operator follows operand.
operand operand operator
ab+, ab*, ab/
INFIX POSTFIX PREFIX
A + B AB+ +AB
A+B+C AB+C+ ++ABC
A+(B+C) ABC++ +A+BC
A+B*C ABC * + +A*BC
A*(B+C) ABC+* *A+BC
A*B*C AB*C* **ABC
Convert following expression into prefix
P + (q + r) * s
P + (+ qr) * s
P + * + qrs
+ p * + qrs
A * (M + C ) + (M/D) * Z + Y * U
Symbol Precedence Rank
+, - 1 -1
*, / 2 -1
a,b,c 3 1
# 0 -----------
Convert infix to postfix with following arithmetic
expression using stack
A + B * C – D / E * H
CHAR SCANNED CONTENT OF STACK POSTFIX
#
A # A
+ # + A
B # + B A
* # + * A B
C # + * C A B
- # - A B C * +
D # - D A B C * +
/ # - / A B C * + D
E # - / E A B C * + D
* # - * A B C * + D E /
H #- * H A B C * + D E /
# A B C * + D E / H* -
A * B + C / DCHAR SCANNED CONTENT OF STACK POSTFIX
#
A #A
* # * A
B # * B A
+ # + A B *
C # + C A B *
/ # + / A B * C
D # + / D A B * C
# A B * C D / +
A B * C D / + IF A =2, B=3, C=25,D=5
CHAR SCANNED CONTENT OF STACK
#
2 #2
3 # 2, 3
* # 6
25 # 6, 25
5 # 6 ,25, 5
/ # 6,5
+ #11
Evaluate postfix expression AB+ C *D IF A=2,
B=3,C=4,D=5
A + B – C * D / E + F $ G / ( I + J ) IF
A=1,B=2,C=3,D=4,E=6,F=6,G=1,I=3 & J=3
( A + B * C / D – E + F / G / ( H + I ) )
( A + B ) * C + D / ( B + A * C ) + D
( A - ( B / C ) ^ D ) / ( E – F )
5 4 6 + * 4 9 3 / + *
7 5 2 + * 4 1 1 + /
ALGORITHM :-
INFIX –rep. infix exp.
S – rep. stack
NEXTCHAR- function which reads next char of
an input string
RANK- rank of each head of postfix string
TEMP –contains unstacked element
1.[initalize stack]
TOP 1, S[TOP] #
2.[INTIALIZE OUTPUT STRING & RANK COUNT]
POLISH “,
RANK 0
3.[GET FIRST INPUT SYMBOL]
NEXT NEXTCHAR(INFIX)
4.[TRANSLATE INFIX EXP.]
Repeat through step-6 while NEXT != #
5.[Remove symbol with greater or equal
symbol precedence from stack]
repeat while f(NEXT)<= f(S[TOP])
TEMPPOP(S,TOP)
POLISH POLISH ° TEMP
RANK RANK + r(TEMP)
IF RANK<1
Then write(“invalid”)
exit
6.[push the current symbol onto stackand
obtain next input symbol]
call PUSH(S,TOP,NEXTCHAR)
NEXT NEXTCHAR(INFIX)
7.[Remove remaining elements from stack]
Repeat while S[TOP] != #
TEMP POP(S,TOP)
POLISH POLISH ° TEMP
RANK RANK + r(TEMP)
IF RANK<1
Then write(“invalid”)
exit
8.[Is the exp. Is valid?]
if RANK= 1
then write (“valid”)
else write(“invalid”)
exit
TOWER OF HANOI
Suppose there are 3 pillars A,B & C.There are N
discs of decreasing size.Intially all the discs are
on one pillar in their decreasing order of size.
The problem is to move all the discs from one
pillar to another.A B C A B C
•Only one disc may be moved at a time
•A disc may be moved from one pillar to another pillar
•At no time a larger disc may be placed on smaller
one.
A B C A B C
A B C A B C
1 2
3 4
A B C
5
A B C
6
A B C
7
A B C
8
The solution is
Move N disc from A to C means
Move (N-1) discs from A to B
Move Nth Disc from A to C
Move all (N-1) discs from B to C
HANOI-
N-Number of Discs
SN_VALUE – Starting disk
IN_VALUE – Intermediate Pillar
DN_VALUE- Destination Pillar
1.[save parameters and return address]
PUSH(S,TOP,TEMP_REC)
TOP TOP + 1
N[TOP] N_VALUE
SN[TOP] SN_VALUE
IN[TOP] IN_VALUE
DN[TOP] DN_VALUE
RET_ADDR[TOP] ADDRESS
2.[Test for stopping value of N,if not reached
move N -1 discs from starting needle to
intermediate needle ]
if N[TOP] = 0
Then go to RET_ADDR[TOP]
else
N_VALUEN[TOP] – 1
SN_VALUESN[TOP]
IN_VALUEIN[TOP]
DN_VALUEDN[TOP]
ADDRESS step-3]
goto step 1
[move nth disc from start to destination
needle, move n-1 from intermediate to
destination]
TEMP_REC POP(ST,TOP)
N_VALUE N[TOP]-1
SN_VALUEIN[TOP]
IN_VALUESN[TOP]
DN_VALUEDN[TOP]
ADDRESS Step -1
4.[return to previous level]
TEMP_REC POP(ST,TOP)
Go to RET_ADDR[TOP]
One of the main problem with using machine which
have a very limited no of registers is how to handle
the store of intermediate result.To solve this problem
stack machine is used
Stack machine is used at H/W level
Ex:- PDP11, Burroughs 5000.
Both machines are particularly well suited for the
stacking of local variables & parameters that arise in
procedure calls of block nested languages.
Instructions available for the machine
PUSH<name>= loads an operand from memory to
stack
POP<name>= top of the stack is removed & store into
memory
ADD, SUB, DIV, MUL :-arithmetic operations
top related