Scilab Textbook Companion for Data Structures Using C And C++ by Y. Langsam, M. Augenstein And A. M. Tenenbaum1 Created by Dharmesh Majethiya B.Tech (pursuing) Computer Engineering NIT Tiruchirappalli College Teacher Mr.Kunwar Singh Cross-Checked by Siddharth Jain August 12, 2013
1
Funded by a grant from the National Mission on Education through ICT, http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab codes written in it can be downloaded from the ”Textbook Companion Project” section at the website http://scilab.in
Book Description Title: Data Structures Using C And C++ Author: Y. Langsam, M. Augenstein And A. M. Tenenbaum Publisher: Prentice - Hall Of India Pvt. Ltd. Edition: 2 Year: 2006 ISBN: 81-203-1177-9
1
Book Description Title: Data Structures Using C And C++ Author: Y. Langsam, M. Augenstein And A. M. Tenenbaum Publisher: Prentice - Hall Of India Pvt. Ltd. Edition: 2 Year: 2006 ISBN: 81-203-1177-9
1
Scilab numbering policy used in this document and the relation to the above book. Exa Example (Solved example) Eqn Equation (Particular equation of the above book) AP Appendix to Example(Scilab Code that is an Appednix to a particular
Example of the above book) For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means a scilab code whose theory is explained in Section 2.3 of the book.
2
Contents List of Scilab Codes
4
1 Introduction To Data Structures
6
2 Stacks
21
3 Recursion
33
4 Queues and linked list
40
5 Trees
65
6 Sorting
79
7 Searching
86
8 Graphs
89
3
List of Scilab Codes Exa 1.1 Exa 1.1.4 Exa 1.1.5 1.1.5 Exa 1.1.7 1.1.7 Exaa 1. Ex 1.22 Exa 1.2.1 1.2.1 Exa 1.2.6 1.2.6 Exa 1.3 Exa 1.3.1 1.3.1 Exa 1.3.6 1.3.6 Exa 1.3.7 1.3.7 Exaa 1.4 Ex 1.4 Exa 1.5 Exa 1.6 Exa 2.1 Exa 2.1.2 Exa 2.2 Exa 2.2.3 2.2.3 Exa 2.3 Exa 2.4 Exaa 3. Ex 3.11 Exaa 3. Ex 3.22 Exaa 3. Ex 3.33 Exaa 3. Ex 3.44 Exaa 3. Ex 3.55 Exa 3.6 Exa 3.7 Exa 4.1
To cal calcua cualte lte Av Avera erage ge And Dev Deviat iation ion . . . . . . . . . Decimal Decim al form of given given no represen represented ted variabl variably y. . . . Add Substrac Substractt And Multipl Multiply y binary binary numbers numbers . . . . TO Conver Convertt Binary To To Ternar Ternary y . . . . . . . . . . . . Stri St ring ng Ma Mani nipu pula lati tions ons . . . . . . . . . . . . . . . . . . Calcul Cal culate ate Media Median n And Mode Of an Arra Array y . . . . . . Findin Fin dingg the adress adress in a row major major array array . . . . . . . Writ riting ing nam namee from from stru structu cture re and and coun countin tingg alpha alphabets bets Implemen Imple menting ting Complex Complex Numbers Numbers by by structure structure . . . . Adding Substr Substractin actingg and multiply multiplying ing Rational Rational Nos . Checking Chec king Equalit Equality y Of 2 Rational Rational Number Numberss . . . . . . Rais Ra isin ingg th thee sa sala lary ry of em empl plooyee . . . . . . . . . . . . Reduci Red ucing ng the giv given en rat ration ional al nu numbe mberr . . . . . . . . . Equali Equ ality ty ch chec eck k of of 2 rat ration ional al nos by red reduct uction ion . . . . To deter determin minee the the syn syntac tactic ticaly aly vali alid d strin stringg . . . . . . To determine determine the syntacti syntacticaly caly valid valid string string . . . . . . Implem Imp lemen entin tingg Sta Stack ck usi using ng uni union on . . . . . . . . . . . Check Che ck if strin stringg is of certain certain form form . . . . . . . . . . . Implem Imp lemen entin tingg Pus Push h And Po Pop p Func unctio tions ns . . . . . . . Conve Con verin ringg an infi infix x expr express ession ion to a Po Postfi stfix x Expr Express ess . Mult Mu ltip ipli lica cati tion on of 2 nu num mber berss . . . . . . . . . . . . . . Fac acto tori rial al of a num umber ber . . . . . . . . . . . . . . . . . Fibbo Fi bbona nacc ccii se seri ries es . . . . . . . . . . . . . . . . . . . . Bina Bi nary ry Se Sear arcch . . . . . . . . . . . . . . . . . . . . . Tower Of Ha Hano noii . . . . . . . . . . . . . . . . . . . . Prefix Pre fix To Po Postfi stfix x Con Conve versi rsion on . . . . . . . . . . . . . Simula Sim ulatin tingg Fact Factori orial al By Non rec recurs ursion ion . . . . . . . . Implem Imp lemen entin tingg Sin Singly gly Con Connec necte ted d Link Linked ed Lis Listt . . . . . 4
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
6 7 9 10 10 11 13 14 15 16 17 17 18 19 21 22 24 25 27 28 33 33 34 35 36 36 39 40
Exa 4.2 Exa 4.3 Exa 4.4 Exa 4.5 Exa 4.6 Exa 5.1 Exa 5.2 Exa 5.3 Exa 5.4 Exaa 6. Ex 6.11 Exaa 6. Ex 6.22 Exaa 6. Ex 6.33 Exaa 6. Ex 6.44 Exaa 6. Ex 6.55 Exaa 6. Ex 6.66 Exaa 6. Ex 6.77 Exaa 7. Ex 7.11 Exaa 7. Ex 7.22 Exaa 7. Ex 7.33 Exaa 8. Ex 8.11 Exa 8.2 Exa 8.3 Exa 8.4 Exaa 8. Ex 8.55 Exa 8.6 Exaa 8. Ex 8.77 Exaa 8. Ex 8.88
Implemen Implem entin tingg Que Queue ue Oper Operari arions ons . . . . . . . . . . . . Implem Imp lemen entin tingg Cir Circul cular ar Lin Linke ked d Lis Listt . . . . . . . . . . . Implem Imp lemen entin tingg Dou Doubly bly con connec necte ted d Link Linked ed Lis Listt . . . . . . Implem Imp lemen entin tingg Stac Stack k usin usingg circ circula ularr Link Linked ed lis listt . . . . . Implem Imp lemen entin tingg Pri Priori ority ty Que Queue ue Usi Using ng Lis Lists ts . . . . . . . Implem Imp lemen entin tingg Bin Binary ary Tre reee . . . . . . . . . . . . . . . . Tree Trv rvers ersal al Tec echni hnique quess . . . . . . . . . . . . . . . . Implem Imp lemen entin tingg And And trav travers ersing ing a Binary Binary Sea Searc rch h Tree Tree . . Check Che cking ing the dup duplic licate ate nu numbe mberr usi using ng BST . . . . . . . Bubb Bu bble le So Sort rt . . . . . . . . . . . . . . . . . . . . . . . . Qui uicck So Sort rt . . . . . . . . . . . . . . . . . . . . . . . . Sele Se lect ctio ion n So Sort rt . . . . . . . . . . . . . . . . . . . . . . . Inse In sert rtio ion n So Sort rt . . . . . . . . . . . . . . . . . . . . . . . Sheell so Sh sort rt . . . . . . . . . . . . . . . . . . . . . . . . . Mer erge ge So Sort rt . . . . . . . . . . . . . . . . . . . . . . . . Bina Bi nary ry Tre reee So Sort rt . . . . . . . . . . . . . . . . . . . . . Sequ Se quen enti tial al Se Sear arcch . . . . . . . . . . . . . . . . . . . . Sort So rted ed se sequ quen enti tial al se sear arcch . . . . . . . . . . . . . . . . . Bina Bi nary ry Se Sear arcch . . . . . . . . . . . . . . . . . . . . . . Simp Si mple le Gr Grap aph h Fun unct ctio ions ns . . . . . . . . . . . . . . . . . Finding Findi ng The Num Number ber Of Of Pat Paths hs From One Verte ertexT xToOthe oOtherr Findin Fin dingg The The Numbe Numberr Of Sim Simple ple Pa Paths ths From One Po Poin intt Findin Fin dingg Tran ransit sitiv ivee Clo Closur suree . . . . . . . . . . . . . . . War arsh shal alls ls Al Algo gori rith thm m . . . . . . . . . . . . . . . . . . . Depth Dep th Fir First st Sea Searc rch h Tra rave versa rsall . . . . . . . . . . . . . . BFS BF S Tra rav ver ersa sall . . . . . . . . . . . . . . . . . . . . . . Dijk Di jkst stra rass Al Algo gori rith thm m . . . . . . . . . . . . . . . . . . .
5
45 46 50 55 61 65 68 72 76 79 80 80 81 81 82 84 86 86 87 89 90 90 91 93 94 95 96
Chapter 1 Introduction To Data Structures
Scilab code Exa 1.1 To calcualte Average And Deviation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/ / S o l v e d E xa x a mp mp le le 1 / / : To T o c a l c u a l t e A v er e r ag a g e An A nd D e vi v i a t io io n function [ a v g ] = a v e r a g e ( a ) i=1; [ j , k ] = size ( a ) ; j=0; for i = 1 : k j=j+a(i); end avg=j/k; dev=0; disp ( a v g , ” A v e r a g e = ” ) ; disp ( ” Th Th e d e v i a t i o n s a r e : ” ) ; for i = 1 : k dev=a(i)-avg; disp ( d e v ) ; end endfunction
// C a l l i n g r o u t i n e 6
20 a = [3 2 23 2 12 3 43 ] 21 a v g = a v e r a g e ( a )
Scilab code Exa 1.1.4 Decimal form of given no represented variably
1 / / E x e r c i s e 1 . 1 Examp le . 1 . 1 . 4 2 //To c a l c u l a t e D ec im al No . o f a g i ve n Number 3 / / T r e a t i n g them a s i ) N o rm al b i n a r y n o s ( i i ) Twos
compl ement ed i i i )BCD: 4 function [ c ] = t w o s 1 ( a 1 ) 5 [ j 1 , i 1 ] = size ( a 1 ) 6 i4=1 7 c=-(a1(i4)*2^(i1-1)); 8 i1=i1-1; 9 while ( i 1 > = 1 ) 10 i4=i4+1; c=c+a1(i4)*2^(i1-1); 11 12 i1=i1-1; 13 end 14 disp ( a 1 , ” D e c i m al f or m o f t h e Twos Co mp le me nt Number” ) ; 15 disp (c , ” i s ” ) ; 16 e n d f u n c t i o n 17 function [ d ] = b i n a r y _ d e c ( a 2 ) 18 [ j 2 , i 2 ] = size ( a 2 ) ; k = modulo ( i 2 , 4 ) ; 19 20 d = 0 ; if ( k = = 0 ) 21 22 e=i2/4; 23 i3=1 24 while ( i 3 < = i 2 ) 25 l =3 26 m =0 27 while ( l > = 0 ) m=m+(a2(i3)*2^l); 28
7
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
l=l-1; i3=i3+1; end if ( m > 9 ) d=-1; disp ( ” Cannot be c od ed i n t h i s fo rm ” ) break ; end if ( m < = 9 ) d=d+m*10^(e-1) e=e-1; end end end disp ( a 2 , ” D e c i m al f or m o f BCD nu mb er ” ) ; disp (d , ” is ” ) ; endfunction
/ / G i v e n E xa m pl e : //(A) p1 =[1 0 0 1 1 0 0 1]; p 2 = b a s e 2 d e c ( [ ’ 1 0 0 1 1 0 0 1 ’ ],2) p2=twos1(p1) p2=binary_dec(p1)
//(b) p3 =[1 0 0 1]; p 4 = b a s e 2 d e c ( [ ’ 100 1 ’ ],2) p4=twos1(p3) p4=binary_dec(p3)
//(C) p5 =[0 0 0 1 0 0 0 1 0 0 0 1]; p 6 = b a s e 2 d e c ( [ ’ 0 0 0 1 0 0 0 1 0 0 0 1 ’ ],2) p6=twos1(p5) p6=binary_dec(p5)
//(d) p7 =[0 1 1 1 0 1 1 1]; p 8 = b a s e 2 d e c ( [ ’ 0 1 1 1 0 1 1 1 ’ ],2) p8=twos1(p7) p8=binary_dec(p7)
8
67 68 69 70 71 72 73 74 75 76
// ( e ) p9 =[0 1 0 1 0 1 0 1]; p 1 0 = b a s e 2 d e c ( [ ’ 0 1 0 1 0 1 0 1 ’ ],2) p10=twos1(p9) p10=binary_dec(p9)
//(F) p11 =[1 0 0 0 0 0 0 1 0 1 0 1]; p 1 2 = b a s e 2 d e c ( [ ’ 1 0 0 0 0 0 0 1 0 1 0 1 ’ ],2) p12=twos1(p11) p12=binary_dec(p11)
Scilab code Exa 1.1.5 Add Substract And Multiply binary numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// E x e r c i s e 1 . 1 e xa mp le 1 . 1 . 5 / / Add , S u b s t r a c t And M u l t i p l y b i n a r y n um be rs function [ a ] = a d d ( b , c ) d=base2dec(b,2) e=base2dec(c,2) a=d+e a=dec2bin(a) disp (a , ” R e su l t o f a d d i ti o n ” ) endfunction function [ a ] = s u b t r a c t ( b , c ) d=base2dec(b,2) e=base2dec(c,2) a=d-e a=dec2bin(a) disp (a , ” R e su l t o f s u b t r a ct i o n ” ) endfunction function [ a ] = m u l t i p l y ( b , c ) d=base2dec(b,2) e=base2dec(c,2) a=d*e a=dec2bin(a) disp (a , ” R es ul t o f m u l ti p l i ca t i on ”);
9
23 24 25 26 27 28 29
endfunction
/ / C a l l i n g R o ut i n e : b=”11001”; c=”10011”; a=add(b,c) a=subtract(b,c) a=multiply(b,c)
Scilab code Exa 1.1.7 TO Convert Binary To Ternary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/ / E x e r c i s e 1 . 1 Examp le 1 . 1 . 7 / /TO C o n v e r t B i n a r y To T e r n a r y function [ t ] = b i n _ t e r ( a ) b =0 b=base2dec(a,2); disp ( b ) ; [ j , i ] = size ( a ) ; t=[]; while ( b ~ = 0 ) m = modulo ( b , 3 ) ; t = [ t ( : , :) m ] ; b=b/3; b = b - m odulo ( b , 1 0 ) ; end disp (t , ” T er na ry E q u i v a le n t ” ) ; endfunction
/ / C a l l i n g R o ut i n e : a=”100101101110” disp (a , ” i n p ut s t r i n g i s ” ) ; b=bin_ter(a)
Scilab code Exa 1.2 String Manipulations
10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/ / S o l v e d E xa mp le 2 // : Stri ng Manipulations funcprot (0) function [ l ] = s t r l e n ( s t r ) i=1; l=0; [ j , k ] = size ( s t r ) for i = 1 : k l = l + length ( s t r ( i ) ) ; end disp (l , ” s t r i n g l e n g t h i s ” ) ; endfunction
/ / C a l l i n g R o ut i n e : s t r = ” H e l l o W or ld ” ; l=strlen(str) function [ c ] = s t r c a t 1 ( a , b ) disp ( strcat ([ a b ]) , ” A f t e r c o n c a t i n a t i o n ” ) ; c = strcat ( [a b ]) ; endfunction
/ / C a l l i n g R o ut i n e : a=” h e l l o ”; b=” w o r l d ”; c=strcat1(a,b);
Scilab code Exa 1.2.1 Calculate Median And Mode Of an Array
1 2 3 4 5 6 7 8 9
/ / E x e r c i s e Exa mpl e 1 . 2 . 1 / / C a l c u l a t e s Medi an And Mode Of an //(A) function [ y ] = m e d i a n 1 ( a ) p=mtlb_sort(a); [ j , i ] = size ( a ) ; y =0 j = modulo ( i , 2 ) ; if ( j = = 0 )
11
A rr ay
10 y=((a(i/2)+a(i/2+1))/2); end 11 12 if ( j = = 1 ) i=i/2; 13 14 i = i - m odulo ( i , 1 0 ) ; y=a(i+1); 15 16 end 17 disp (y , ” m ed ia n i s ” ) ; 18 e n d f u n c t i o n 19 / / ( B ) 20 function [ z ] = m o d e 1 ( a ) 21 p = m t l b _ s o r t ( a ) ; 22 disp ( p ) 23 q = 1 ; 24 r = 1 ; 25 i = 1 ; 26 [ j , i 1 ] = size ( a ) ; 27 if ( i 1 > 1 ) for i = 1 : i 1 - 1 28 29 if ( p ( i ) ~ = p ( i + 1 ) ) 30 q = [ q ( : , :) i + 1 ] ; 31 r = [ r ( : , :) 1 ]; 32 else [ c , d ] = size ( r ) ; 33 34 r(d)=r(d)+1; end 35 36 end q1=mtlb_sort(r); 37 38 [ j , i 1 ] = size ( q 1 ) if ( q 1 ( i 1 - 1 ) = = q 1 ( i 1 ) ) 39 40 z=-1; 41 disp ( ” Mode d o es n ot e x i s t ” ) ; 42 break ; 43 else 44 c=q1(i1); 45 k=1; while ( r ( k ) ~ = c ) 46 47 k=k+1;
12
48 49 50 51 52 53 54 55 56 57 58 59
end z=p(q(k)); end end if ( i 1 = = 1 ) z=a(1); end disp (z , ” mode i s ” ) ; endfunction a = [ 22 3 1 2 2 33 3 22 1 21 ] y=median1(a); z=mode1(a);
Scilab code Exa 1.2.6 Finding the adress in a row major array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/ / E x e r c i s e 1 . 2 Examp le 1 . 2 . 6 // F in di n g t he a d r e s s i n a row ma jo r a r ra y function [ ] = a d d ( m , n ) printf ( ” A d re ss i s %d\ n ” , m + n * 2 0 ) ; endfunction
//(a) add(10,0);
//(b) add(100,0);
// ( c ) add(0,0);
//(d) add(2,1);
// ( e ) add(5,1);
// ( f ) add(1,10);
//(g) add(2,10);
13
21 22 23 24
//(h) add(5,3);
// ( i ) add(9,19);
Scilab code Exa 1.3 Writing name from structure and counting alphabets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/ / S o l v e d E xa mp le 5 : / / W r it i ng a name f ro m t h e g i v e n s t r u c t u r e and / / c o u n t in g t h e number o f a l p h a b e t s p r i n t e d function [ l ] = s t r l e n ( s t r ) i=1; l=0; [ j , k ] = size ( s t r ) for i = 1 : k l = l + length ( s t r ( i ) ) ; end endfunction function [ c o u n t ] = w r i t e n a m e ( n a m e ) printf ( ” \ n ” ) ; printf ( ”%s” , n a m e . f i r s t ) ; ’ ); printf ( ”%c” , ’ printf ( ”%s” , n a m e . m i d i n i t ) ; printf ( ” \ t ” ) ; printf ( ”%s” , n a m e . l a s t ) ; printf ( ” \ n ” ) ; a = string ( n a m e . f i r s t ) ; count=strlen(a); a = string ( n a m e . m i d i n i t ) ; count=count+strlen(a); a = string ( n a m e . l a s t ) ; count=count+strlen(a); disp ( c o u n t , ” C ou nt i s : ” ) ; endfunction
14
29 / / C a l l i n g R o ut i n e 30 n a m e = s t r u c t ( ’ f i r s t ’ , ’ p r a v e e n ’ , ’ m i d i n i t ’ , ’ r a j e e v ’ , ’ l a s t ’ , ’ chauha n ’ ) ; 31 c o u n t = w r i t e n a m e ( n a m e )
Scilab code Exa 1.3.1 Implementing Complex Numbers by structure
1 2 3 4 5 6 7 8 9 10
// E x e r c i s e 1 . 3 / / E xam ple 1 . 3 . 1 / / I m p l e m en t i n g C om ple x N umb ers by s t r u c t u r e function [ ] = c o m p l e x m a n u ( x 1 , x 2 , x 3 , x 4 ) c o m 1 = s t r u c t ( ’ r e a l ’ , x 1 , ’ com plex ’ , x 2 ) ; c o m 2 = s t r u c t ( ’ r e a l ’ , x 3 , ’ com plex ’ , x 4 ) ;
11 12 13
14 15 16
17 18
19 20 21 22 23
/ / a d d i n g 2 n um be rs a d d = s t r u c t ( ’ r e a l ’ , x 1 + x 3 , ’ comp lex ’ , x 2 + x 4 ) ; disp (add. complex , ”+ i ” , a d d . real , ” A d di ti on r e s u l t i s ”); // Sub stra ct s u b = s t r u c t ( ’ r e a l ’ , x 1 - x 3 , ’ comp lex ’ , x 2 - x 4 ) ; disp (sub. complex , ”+ i ” , s u b . real , ” S u b s t r a c t i o n r es ul t i s ” ); // Nega ting n e g = s t r u c t ( ’ r e a l ’ , - x 1 , ’ comp lex ’ , - x 2 ) ; disp (neg. complex , ”+ i ” , n e g . real , ” N eg at io n r e s u l t f o r t he f i r s t i s ” ) ; // M u l t i p l i c a t i o n m u l = s t r u c t ( ’ r e a l ’ , x 1 * x 3 - x 2 * x 4 , ’ comp lex ’ , x 2 * x 3 + x 4 * x1); disp (mul .complex , ”+ i ” , m u l . real , ” M u l t i p l i c a t i o n r e s u l t i s ” ); endfunction x1=3; x2=5; x3=5;
15
24 x 4 = 6 ; 25 c o m p l e x m a n u ( x 1 , x 2 , x 3 , x 4 ) ;
Scilab code Exa 1.3.6 Adding Substracting and multiplying Rational Nos
1 // E x e r c i s e 1 . 3 2 // Example 1.3. 6 3 / / Adding , S u b t r a c t i n g and m u l t i p l y i n g R a t i o n a l
Numbers 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
function [ ] = r a t i o n a l ( x 1 , x 2 , x 3 , x 4 ) r a t i o n a l 1 = s t r u c t ( ’ nume rato r ’ , x 1 , ’ d e n o m i n a t o r ’ , x 2 ) ; disp ( r a t i o n a l 1 ) ; r a t i o n a l 2 = s t r u c t ( ’ nume rato r ’ , x 3 , ’ d e n o m i n a t o r ’ , x 4 ) ; disp ( r a t i o n a l 2 ) ;
//Add x5 = int32 ( [ x2 x 4 ]) ; x5 = lcm ( x 5 ) ; x6=x1*(x5/x2)+x3*(x5/x4); r a t i o n a l 3 = s t r u c t ( ’ nume rato r ’ , x 6 , ’ d e n o m i n a t o r ’ , x 5 ) ; disp (rational3 , ” A f t e r a d d i t i o n ” ) ;
// sub tra ct x6=x1*(x5/x2)-x3*(x5/x4) r a t i o n a l 4 = s t r u c t ( ’ nume rato r ’ , x 6 , ’ d e n o m i n a t o r ’ , x 5 ) ; disp (rational4 , ” A f t e r S u b t r a c t i o n ” ) ;
// Mul tip ly
x7=x1*x3; x8=x2*x4; r a t i o n a l 5 = s t r u c t ( ’ nume rato r ’ , x 7 , ’ d e n o m i n a t o r ’ , x 8 ) ; disp (rational5 , ” A f t er m u l t i p l i c a t i o n ” ) ; endfunction x1=43; x2=32; x3=233; x4=33; rational(x1,x2,x3,x4);
16
Scilab code Exa 1.3.7 Checking Equality Of 2 Rational Numbers
1 // E x e r c i s e 1 . 3 2 / / E xam ple 1 . 3 . 7 3 / / C h e ck i ng E q u a l i t y Of 2 R a t i o n a l Numbers W it ho ut
Reduci ng Them 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
function [ ] = r a t i o n a l _ e q u a l ( x 1 , x 2 , x 3 , x 4 ) r a t i o n a l 1 = s t r u c t ( ’ nume rato r ’ , x 1 , ’ d e n o m i n a t o r ’ , x 2 ) ; disp ( r a t i o n a l 1 ) ; r a t i o n a l 2 = s t r u c t ( ’ nume rato r ’ , x 3 , ’ d e n o m i n a t o r ’ , x 4 ) ; disp ( r a t i o n a l 2 ) ; if ( x 1 * x 4 = = x 2 * x 3 ) disp ( ” E q u a l ” ) ; break ; else disp ( ” Not E qu al ” ) ; break ; end endfunction
/ / C a l l i n g R o ut i n e :
x1=32; x2=45; x3=43; x4=55; rational_equal(x1,x2,x3,x4);
Scilab code Exa 1.4 Raising the salary of employee
1 / / S o l v e d E xa mp le 6 2 //To R a is e The s a l a r y o f an e mp lo ye e 3 function [ e m p l o y e e 1 ] = r a i s e ( e m p l o y ee , n )// e m pl oy ee i s
t h e l i s t o f e m pl o ye es 17
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
for i = 1 : n if ( e m p l o y e e ( i ) ( 1 ) . y e ar < = 2 0 0 0 ) employee(i)(2)=employee(i)(2)*1.1; else employee(i)(2)=employee(i)(2)*1.05; end end employee1=employee; disp ( ” A f t er R a is i ng ” ) ; for i = 1 : n printf ( ” E m p l o y ee n o %d\ n” , i ) ; disp ( e m p l o y e e ( i ) ( 1 ) ) ; disp ( e m p l o y e e ( i ) ( 2 ) ) ; end
endfunction
/ / C a l l i n g R o ut i n e : d a t e h i r e d = s t r u c t ( ’ ye ar ’ ,1993, ’ month ’ ,12);
e m p l o y e e 1 =list (datehired ,14000) ; d a t e h i r e d = s t r u c t ( ’ ye ar ’ ,1998, ’ month ’ ,12); e m p l o y e e 2 =list (datehired ,17000) ; d a t e h i r e d = s t r u c t ( ’ ye ar ’ ,2003, ’ month ’ ,12); e m p l o y e e 3 =list (datehired ,25000) ; d a t e h i r e d = s t r u c t ( ’ ye ar ’ ,2002, ’ month ’ ,12); e m p l o y e e 4 =list (datehired ,35000) ; d a t e h i r e d = s t r u c t ( ’ ye ar ’ ,2006, ’ month ’ ,12); e m p l o y e e 5 =list (datehired ,13000) ; e m p l o y e e = list (employee1 ,employee2 ,employee3 , employee4 ,employee5); 32 employee =raise (employee ,5)
Scilab code Exa 1.5 Reducing the given rational number
1 / / S o l v e d E xa mp le 7 : 2 / / R e d u ci n g The G iv en R a t i o n a l Number
18
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
funcprot (0) function [ y ] = r e d u c e ( n m , d n ) r a t i o n a l 1 = s t r u c t ( ’ nume rato r ’ , n m , ’ d e n o m i n a t o r ’ , d n ) y =0 if ( r a t i o n a l 1 . n u m e ra t o r > r a t i o n a l 1 . d e n o m i n a t o r ) a=rational1.numerator; b=rational1.denominator; else a=rational1.denominator; b=rational1.numerator; end while ( b ~ = 0 ) r e m = modulo ( a , b ) ; a=b; b=rem; end y = s t r u c t ( ’ nume rato r ’ , n m / a , ’ d e n o m i n a t o r ’ , d n / a ) ; disp ( y ) ; endfunction nm=22; dn=44; y=reduce(nm,dn)
Scilab code Exa 1.6 Equality check of 2 rational nos by reduction
1 / / S o l v e d E xa mp le 8 : 2 / / C he c k i ng f o r t he e q u a l i t y o f 2 r a t i o n a l
numbers b y
r e d u c i n g them 3 function [ ] = e q u a l ( x 1 , x 2 , x 3 , x 4 ) 4 r a t i o n a l 1 = s t r u c t ( ’ nume rato r ’ , x 1 , ’ d e n o m i n a t o r ’ , x 2 ) 5 r a t i o n a l 2 = s t r u c t ( ’ nume rator ’ , x 3 , ’ d e n o m i n a t o r ’ , x 4 ) 6 y =0 7 if ( r a t i o n a l 1 . n u m e ra t o r > r a t i o n a l 1 . d e n o m i n a t o r ) 8 a=rational1.numerator; 9 b=rational1.denominator;
19
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
else a=rational1.denominator; b=rational1.numerator; end while ( b ~ = 0 ) r e m = modulo ( a , b ) ; a=b; b=rem; end y = s t r u c t ( ’ nume rato r ’ , x 1 / a , ’ d e n o m i n a t o r ’ , x 2 / a ) ; y1=0 if ( r a t i o n a l 2 . n u m e ra t o r > r a t i o n a l 2 . d e n o m i n a t o r ) a=rational2.numerator; b=rational2.denominator; else a=rational2.denominator; b=rational2.numerator; end while ( b ~ = 0 ) r e m = modulo ( a , b ) ; a=b; b=rem; end y 1 = s t r u c t ( ’ numer ator ’ , x 3 / a , ’ d e n o m i n a t o r ’ , x 4 / a ) ; if ( y = = y 1 ) disp ( ” E q u a l ” ) break ; else disp ( ” No t E q ua l ” ) break ; end endfunction x1=5; x2=7; x3=35; x4=49; equal(x1,x2,x3,x4);
20
Chapter 2 Stacks
Scilab code Exa 2.1 To determine the syntacticaly valid string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/ / S o l v e d E xa mp le 1 //To d et er mi ne t h e s y n t a c t i c a l y v a l i d s t r i n g function [ l ] = s t r l e n ( x ) i=1; l=0; [ j , k ] = size ( x ) for i = 1 : k l = l + length ( x ( i ) ) ; end endfunction function [ ] = s t r i n g v a l i d ( s t r ) s t r = string ( s t r ) ; s t a c k = s t r u c t ( ’ a ’ , ’ 0 ’ , ’ top ’ ,0); l1=strlen(str); valid=1; l=1; while ( l < = l 1 ) if ( s t r ( l ) = = ’ ( ’ | s t r ( l ) = = ’ [ ’ | s t r ( l ) = = ’ { ’ ) if ( s t a c k . t o p = = 0 ) stack.a=str(l); stack.top=stack.top+1;
21
22 23 24 25 26 27 28 29 30 31 32 33 34 35
else s t ac k . a = [ s t ac k . a ( : , : ) s tr ( l ) ] ; stack.top=stack.top+1; end end if ( s t r ( l ) = = ’ ) ’ | s t r ( l ) = = ’ ] ’ | s t r ( l ) = = ’ } ’ ) if ( s t a c k . t o p = = 0 ) valid=0; break ; else i=stack.a(stack.top); stack.top=stack.top-1; symb=str(l); if ( ( ( s y m b = = ’ ) ’ ) & ( i = = ’ ( ’ ) ) | ( ( s y m b = = ’ ] ’ ) & ( i = = ’ [ ’ ) ) | ( ( s y m b = = ’ } ’ ) & ( i = = ’ { ’ ) ) ) else valid=0; break ; end end end l=l+1; end if ( s t a c k . t o p ~ = 0 ) valid=0; end if ( v a l i d = = 0 ) disp ( ” I n v a l i d S t r i n g ” ) ; else disp ( ” V a l id S t r i n g ” ) ; end endfunction
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 / / C a l l i n g R o ut i ne : 54 s t r i n g v a l i d ( [ ’H ’ ’E ’
’ L ’ ’ L ’ ’O ’ ])
22
Scilab code Exa 2.1.2 To determine the syntacticaly valid string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/ / S o l v e d E xa mp le 1 //To d et er mi ne t h e s y n t a c t i c a l y v a l i d s t r i n g function [ l ] = s t r l e n ( x ) i=1; l=0; [ j , k ] = size ( x ) for i = 1 : k l = l + length ( x ( i ) ) ; end endfunction function [ ] = s t r i n g v a l i d ( s t r ) s t r = string ( s t r ) ; s t a c k = s t r u c t ( ’ a ’ , ’ 0 ’ , ’ top ’ ,0); l1=strlen(str); valid=1; l=1; while ( l < = l 1 ) if ( s t r ( l ) = = ’ ( ’ | s t r ( l ) = = ’ [ ’ | s t r ( l ) = = ’ { ’ ) if ( s t a c k . t o p = = 0 ) stack.a=str(l); stack.top=stack.top+1; else s t ac k . a = [ s t ac k . a ( : , : ) s tr ( l ) ] ; stack.top=stack.top+1; end disp ( s t a c k ) ; end if ( s t r ( l ) = = ’ ) ’ | s t r ( l ) = = ’ ] ’ | s t r ( l ) = = ’ } ’ ) if ( s t a c k . t o p = = 0 ) valid=0; break ; else i=stack.a(stack.top); b=stack.a(1); for i 1 = 2 : s t a c k . t o p - 1 b = [ b ( : , :) s t ac k . a ( i 1 ) ]
23
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
end stack.a=b; stack.top=stack.top-1; symb=str(l); disp ( s t a c k ) ; if ( ( ( s y m b = = ’ ) ’ ) & ( i = = ’ ( ’ ) ) | ( ( s y m b = = ’ ] ’ ) & ( i = ’ [ ’ ) ) | ( ( s y m b = ’ } ’ ) & ( i = ’ { ’ ) ) ) else valid=0; break ; end end end l=l+1; end if ( s t a c k . t o p ~ = 0 ) valid=0; end if ( v a l i d = = 0 ) disp ( ” I n v a l i d S t r i n g ” ) ; else disp ( ” V a l id S t r i n g ” ) ; end endfunction
63
64
65
/ / C a l l i n g R o ut i ne : s t r i n g v a l i d ( [ ’ ( ’ ’A ’ ’+ ’ ’B ’ s t r i n g v a l i d ( [ ’ { ’ ’ [ ’ ’A ’ ’+ ’ ’C ’ ’− ’ ’D ’ ’ ) ’ ’ ] ’ ]) s t r i n g v a l i d ( [ ’ ( ’ ’A ’ ’+ ’ ’B ’ D ’ ’ } ’ ’− ’ ’ [ ’ ’ F ’ ’+ ’ ’G ’ s t r i n g v a l i d ( [ ’ ( ’ ’ ( ’ ’H ’ ’ ) ’ + ’ ’K ’ ’ ] ’ ’ ) ’ ’ } ’ ’ ) ’ ]) s t r i n g v a l i d ( [ ’ ( ’ ’ ( ’ ’ ( ’ ’A ’
’ } ’ ’ ) ’ ]) ’B ’ ’ ] ’ ’− ’ ’ [ ’ ’ ( ’ ’ ) ’ ’ − ’ ’ { ’ ’C ’ ’+ ’ ’ ’ ] ’ ]) ’ ∗ ’ ’ { ’ ’ ( ’ ’ [ ’ ’ J ’ ’ ’ ) ’ ’ ) ’ ’ ) ’ ])
Scilab code Exa 2.2 Implementing Stack using union
24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/ / S o l v e d E xa mp le 2 : / / I m p le m en t i ng S t a ck u s i n g u n io n : function [ s t a c k ] = s t a _ u n i o n ( e t y p e , a ) s t a c k e l e m e n t = s t r u c t ( ’ e t y p e ’ , e t y p e ) ; [ k , l ] = size ( a ) ; select s t a c k e l e m e n t . e t y p e , case ’ i n t ’ then a = int32 ( a ) ; s t a c k = s t r u c t ( ’ top ’ ,l , ’ i t e m s ’ , a ) ; , case ’ f l o a t ’ then a = double ( a ) ; s t a c k = s t r u c t ( ’ top ’ ,l , ’ i t e m s ’ , a ) ; , case ’ cha r ’ then a = string ( a ) ; s t a c k = s t r u c t ( ’ top ’ ,l , ’ i t e m s ’ , a ) ; , end disp ( s t a c k , ” S t a ck i s : ” ) ; endfunction a = [ 32 1 2. 34 2 32 3 2. 3 22 ] s t a c k = s t a _ u n i o n ( ’ f l o a t ’ ,a ) s t a c k = s t a _ u n i o n ( ’ i n t ’ ,a ) s t a c k = s t a _ u n i o n ( ’ cha r ’ ,a )
Scilab code Exa 2.2.3 Check if string is of certain form
1 function [ l ] = s t r l e n ( x ) 2 i=1; 3 l=0; 4 [ j , k ] = size ( x ) 5 for i = 1 : k 6 l = l + length ( x ( i ) ) ; 7 end 8 endfunction 9 function [ ] = s t r ( s t ) 10 s t a c k = s t r u c t ( ’ a ’ ,0 , ’ top ’ ,0);
25
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
st = string ( s t ) ; l=1; l1=strlen(st); symb=st(l); valid=1; while ( l < l 1 ) while ( s y m b ~ = ’ C ’ ) if ( s t a c k . t o p = = 0 ) stack.a=st(l); stack.top=stack.top+1; else s t ac k . a = [ s t ac k . a ( : , : ) s t ( l ) ]; stack.top=stack.top+1; end l=l+1; symb=st(l); end i=st(l+1); if ( s t a c k . t o p = = 0 ) valid=0; break ; else symb1=stack.a(stack.top); stack.top=stack.top-1; if ( i ~ = s y m b 1 ) valid=0; break ; end end l=l+1; end if ( s t a c k . t o p ~ = 0 ) valid=0; end if ( v a l i d = = 0 ) disp ( ” Not o f t he g i ve n f or ma t ” ) ; else disp ( ” S t r i n g Of t h e G iv en Fo rma t” ) ;
26
49 50 51 52 53 54 55
end endfunction
/ / C a l l i n g R o ut i n e : s t = [ ’A ’ ’A ’ ’B ’ ’A ’ ’C ’ ’A ’ ’B ’ ’A ’ ’A ’ ] str(st) s t = [ ’A ’ ’A ’ ’B ’ str(st)
’A ’ ’C ’ ’A ’ ’B ’ ’A ’ ]
Scilab code Exa 2.3 Implementing Push And Pop Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/ / S o l v e d E xa mp le 3 : / / I m p l e m e n ti n g P ush And Pop F u n c t i o n s : function [ y , s t a 1 ] = e m p t y ( s t a ) y=0; sta1=0; if ( s t a . t o p = = 0 ) y=0; else y=1; end sta1=sta endfunction function [ s t a ] = p u s h ( s t a c , e l e ) sta=0; if ( e m p t y ( s t a c ) = = 0 ) stac.a=ele; stac.top=stac.top+1; else s t ac . a = [ s t a c . a ( : , :) e le ] stac.top=stac.top+1; end disp ( s t a c ) ; sta=stac; endfunction
27
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
function [ e l e , s t a ] = p o p ( s t a c k ) e l e = ’ −1 ’ ; if ( e m p t y ( s t a c k ) = = 0 ) disp ( ” S t a c k U n de r fl o w ” ) ; break ; else ele=stack.a(stack.top); stack.top=stack.top-1; if ( s t a c k . t o p ~ = 0 ) b=stack.a(1); for i 2 = 2 : s t a c k . t o p b = [ b ( : , :) s t ac k . a ( i 2 ) ] ; end stack.a=b; else s t a c k . a = ’ 0 ’ ; end end disp ( s t a c k ) ; sta=stack; endfunction global stack
/ / C a l l i n g R o ut i n e : s t a c k = s t r u c t ( ’ a ’ ,0 , ’ top ’ ,0); stack= push( stack ,4); stack= push( stack ,55); stack= push( stack ,199); stack= push( stack ,363); [ele,stack]=pop(stack); disp ( s t a c k , ” A f t er t he a b o v e o p e r a ti o n s s t a c k i s : ” ) ;
Scilab code Exa 2.4 Convering an infix expression to a Postfix Express
1 / / S o l v e d E xa mp le 5 :
28
2 / / Co nv e r i n g an i n f i x
e x pr e ss i o n to a P o s tf i x
Expression : 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
function [ s t a ] = p u s h ( s t a c , e l e ) sta=0; if ( s t a c . t o p = = 0 ) stac.a=ele; stac.top=stac.top+1; else s t ac . a = [ s t a c . a ( : , :) e le ] stac.top=stac.top+1; end disp ( s t a c ) ; sta=stac; endfunction function [ e l e , s t a ] = p o p ( s t a c k ) e l e = ’ −1 ’ ; if ( s t a c k . t o p = = 0 ) disp ( ” S t a c k U n de r fl o w ” ) ; break ; else ele=stack.a(stack.top); stack.top=stack.top-1; if ( s t a c k . t o p ~ = 0 ) b=stack.a(1); for i 2 = 2 : s t a c k . t o p b = [ b ( : , :) s t ac k . a ( i 2 ) ] ; end stack.a=b; else s t a c k . a = ’ 0 ’ ; end end sta=stack; endfunction function [ l ] = s t r l e n ( x ) i=1; l=0;
29
39 [ j , k ] = size ( x ) for i = 1 : k 40 41 l = l + length ( x ( i ) ) ; end 42 43 e n d f u n c t i o n 44 function [ p ] = p r e ( s 1 , s 2 ) 45 i1=0; select s1 , 46 47 case ’+ ’ then i 1 = 5 ; 48 case ’− ’ then i 1 = 5 ; 49 case ’ ∗ ’ then i 1 = 9 ; 50 case ’ / ’ then i 1 = 9 ; end 51 52 i2=0; select s2 , 53 54 case ’+ ’ then i 2 = 5 ; case ’− ’ then i 2 = 5 ; 55 56 case ’ ∗ ’ then i 2 = 9 ; case ’ / ’ then i 2 = 9 ; 57 58 end 59 p=0; 60 p=i1-i2; 61 if ( s 1 = = ’ ( ’ ) p=-1; 62 63 end if ( s 2 = = ’ ( ’ & s 1 ~ = ’ ) ’ ) 64 65 p=-1; end 66 67 if ( s 1 ~ = ’ ( ’ & s 2 = = ’ ) ’ ) p=1; 68 69 end 70 71 e n d f u n c t i o n 72 function [ a 2 ] = i n t o p o ( a 1 , n ) 73 s t a c k = s t r u c t ( ’ a ’ ,0 , ’ top ’ ,0); 74 l 1 = 1 ; 75 l 2 = s t r l e n ( a 1 ( 1 ) ) 76 for i = 2 : n
30
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
l2=l2+strlen(a1(i)) end a2 = list () ; while ( l 1 < = l 2 ) symb=a1(l1); if ( i s a l p h a n u m ( string ( a 1 ( l 1 ) ) ) ) a2 = list ( a 2 , s y m b ) ; else while ( s t a c k . t o p ~ = 0 & ( p r e ( s t a c k . a ( s t a c k . t o p ) , symb)>=0)) [ t o p s y mb , s t a c k ] = p o p ( s t a c k ) ; if ( t o p s y m b = = ’ ) ’ | t o p s y m b = = ’ ( ’ ) a2=a2; else a2 = list ( a 2 , t o p s y m b ) ; end end if ( s t a c k . t o p = = 0 | s y m b ~ = ’ ) ’ ) stack=push(stack,symb); else [ele,stack]=pop(stack); end end l1=l1+1; end while ( s t a c k . t o p ~ = 0 ) [ t o p s ym b , s t a c k ] = p o p ( s t a c k ) ; if ( t o p s y m b = = ’ ) ’ | t o p s y m b = = ’ ( ’ ) a2=a2; else a2 = list ( a 2 , t o p s y m b ) ; end end disp ( a 2 ) ; endfunction
/ / C a l l i n g R o ut i n e : a 1 = [ ’ ( ’ ’ 2 ’ ’+ ’ ’ 3 ’ ’ ) ’ ’ ∗ ’ ’ ( ’ ’ 5 ’ ’− ’ ’ 4 ’ ’ ) ’ ] a2=intopo(a1,11)
31
32
Chapter 3 Recursion
Scilab code Exa 3.1 Multiplication of 2 numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// M u l t i p l i c a t i o n o f 2 numbers funcprot (0) function [ v a l ] = m u l ( a , b ) if ( b = = 1 ) val=a; else val=a+mul(a,b-1); end endfunction
/ / C a l l i n g R o ut i n e :
a=4; b=3; val=mul(4,3) printf ( ” P r o du c t o f %d a nd %d i s %d” , a , b , v a l ) ;
Scilab code Exa 3.2 Factorial of a number
1 // F un ct io n To C a l u cu l a t e
f a c t o r i a l o f a g i v en
number 33
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
function [ v a l u e ] = f a c t ( a ) value=-1; if ( a < 0 | a > 1 7 0 ) disp ( ” I n v a l i d v a l u . ” ) ; break ; else if ( a = = 1 | a = = 0 ) value=1; else value=a*fact(a-1); end end endfunction
/ / C a l l i n g R o ut i n e : a=5; val=fact(a); printf ( ”%d f a c t o r i a l
i s %d” , a , v a l ) ;
Scilab code Exa 3.3 Fibbonacci series
1 function [ f i b ] = f i b b o ( n ) 2 fib=-1; 3 if ( n < 0 ) disp ( ” I n v a l i d E nt ry ” ) ; 4 5 else if ( n < = 1 ) 6 7 fib=n; 8 else 9 fib=fibbo(n-1)+fibbo(n-2); 10 end 11 end 12 e n d f u n c t i o n 13 14 function [ l ] = f i b b o n ( n ) 15 x = 0 ;
34
16 17 18 19 20 21 22 23 24
l=(fibbo(0)); for x = 1 : n - 1 l=[l(:,:),fibbo(x)]; end disp ( l ) ; endfunction
/ / C a l l i n g R o ut i n e : n=5; l=fibbon(n)
Scilab code Exa 3.4 Binary Search
1 function [ b ] = b s e a r ( a , l , u , n ) 2 if ( l > u ) b=-1; 3 4 else m i d = int32 ( ( l + u ) / 2 ) ; 5 6 if ( n = = a ( m i d ) ) 7 b=n; 8 else 9 if ( n > a ( m i d ) ) 10 m i d = int32 ( ( l + u ) / 2 ) ; 11 b=bsear(a,mid+1,u,n); else 12 13 m i d = int32 ( ( l + u ) / 2 ) ; b=bsear(a,l,mid-1,n); 14 15 end end 16 17 end 18 e n d f u n c t i o n 19 20 function [ b ] = b s e a r c ( a , l , u , n ) 21 b = b s e a r ( a , l , u , n ) ; 22 if ( b = = - 1 ) disp ( ” The e le me nt i s n ot t h e r e ” ) ; 23
35
24 25 26 27 28 29 30 31
end if ( b = = n ) disp ( ” The e l em e nt i s end endfunction
there ”);
/ / C a l l i n g R o ut i n e : a = [ 12 1 22 3 23 3 1 21 2 1] / / Must b e s o r t e d : b=bsearc(a,1,4,12)
Scilab code Exa 3.5 Tower Of Hanoi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
function [ ] = t o w e ( n , f r o m , t o , a u x ) if ( n = = 1 ) ; disp ( t o , ” t o ” , f r o m , ” Move p eg 1 f ro m ” ) ; else towe(n-1,from,aux,to); disp ( t o , ” t o ” , f r o m , ” f r o m ” ,n , ”Move Peg” ) ; towe(n-1,aux,to,from); end endfunction function [ ] = t o w e r ( f r o m , t o , a u x ) n = input ( ” E n t er n ” ) ; towe(n,from,to,aux); endfunction
/ / C a l l i n g R o ut i n e : n =3 / / Number o f d i s k s t o w e ( n , ’ a ’ , ’ b ’ , ’ c ’ )
Scilab code Exa 3.6 Prefix To Postfix Conversion
1 funcprot (0) 2 function [ y ] = f i n d 1 ( g )
36
3 length1=strlen(g); if ( l e n g t h 1 = = 0 ) 4 5 y=0; 6 else 7 if ( i s a l p h a n u m ( g ( 1 ) ) ) y=1; 8 9 else if (le ngth1 <2) 10 11 y=0; 12 else 13 s=strsplit(g,1); 14 s=s(2); m=find1(s); 15 16 if ( m = = 0 | l e n g t h 1 = = m ) y=0; 17 18 else e=strsplit(g,m+1); 19 20 e=e(2); n=find1(e); 21 22 if ( n = = 0 ) 23 y=0; 24 else 25 y=m+n+1; end 26 27 end end 28 29 end end 30 31 e n d f u n c t i o n 32 function [ l ] = s t r l e n ( x ) 33 i = 1 ; 34 l = 0 ; 35 [ j , k ] = size ( x ) 36 for i = 1 : k 37 l = l + length ( x ( i ) ) ; 38 end 39 e n d f u n c t i o n 40 function [ p o ] = p r 2 p o ( p r )
37
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
length1=strlen(pr); if (length1==1) if ( i s a l p h a n u m ( p r ) ) po(1)=pr(1); else disp ( ” I n v a l i d s t r i n g \ n ” ) ; end else s=strsplit(pr,1); g=s(2); m=find1(g); s=strsplit(pr,m+1); g1=s(2); n=find1(g1); f=strsplit(pr,1); c=f(1); i f ( ( c ~ = ’+ ’ + ’ & c ~ = ’ − ’ & c ~ = ’ / ’ & c ~ = ’ ∗ ’ ) | m = = 0 | n = = 0 | m + n +1~=length1) printf ( ” I n v a l i d s t r i n g \ n ” ) ; else s=strsplit(pr,1); s=strsplit(s(2),m); opnd1=s(1); s=strsplit(pr,m+1); opnd2=s(2); post1=pr2po(opnd1); post2=pr2po(opnd2); p o st st = [ p o s t 1 ( : , :) : ) p o st st 2 ( : , : ) ] f=strsplit(pr,1); c=f(1); p o st s t 3 = [ p o s t ( : , :) :) c ] ; po=post3; end end endfunction
/ / C a l l i n g R o u ti ti n e : n o s p a c e s b et e t we w e en en s 1 = ”+−∗a b c d ” ; / / no 38
78 79 80 81 82
po=pr2po(s1); disp ( p o , ” p o s t f i x i s ” ) ; s 1 = ”+−∗/+−∗/ a b c d e f g h i ” po=pr2po(s1); disp ( p o , ” p o s t f i x i s ” ) ;
Scilab code Exa 3.7 Simulating Factorial By Non recursion
1 2 3 4 5 6 7 8 9 10 11 12
function [ ] = s i m u _ f a c t ( n ) ; a=1; while ( n > 0 ) a=a*n; n=n-1; end disp ( a , ” F a c t o r i a l i s ” ) ; endfunction
/ / C a l l i n g R o ut ut i n e : a =9 simu_fact(a)
39
Chapter 4 Queues and linked list
Scilab code Exa 4.1 Implementing Singly Connected Linked List
1 / / SINGLY CONNECTED LIN LINKED KED LI ST : 2 function [ l i n k 2 ] = a p p e n d ( e l e , l i n k 1 ) 3 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 4 if (link1(1)(1).add==0) 5 link1(1)(1).data=ele; 6 link1(1)(1).add=1; 7 link1(1)(1).nexadd=0; link2(1)=link1(1)(1); 8 9 else if ( l i n k 1 ( 1 ) ( 1 ) . n e x a d d = = 0 ) 10 11 lin2=link1(1)(1); lin2.data=ele; 12 13 lin2.add=link1(1)(1).add+1; link1(1)(1).nexadd=lin2.add; 14 15 lin2.nexadd=0; 16 link2(1)=link1(1)(1); 17 link2(2)=lin2; 18 else lin2=link1(1)(1); 19
40
20 i=1; while ( l i n k 1 ( i ) ( 1 ) . n e x a d d ~ = 0 ) 21 22 i=i+1; end 23 24 j=i; lin2.data=ele; 25 26 lin2.add=link1(i).add+1; lin2.nexadd=0; 27 28 link1(i).nexadd=lin2.add; 29 link2(1)=link1(1)(1); 30 i=2; 31 while ( l i n k 1 ( i ) . n e x a d d ~ = l i n 2 . a d d ) link2(i)=(link1(i)); 32 33 i=i+1; end 34 35 link2(i)=link1(i); link2(i+1)=lin2; 36 37 end end 38 39 e n d f u n c t i o n 40 function [ l i n k 2 ] = a d d ( e l e , p o s , l i n k 1 ) ; 41 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 42 i = 1 ; 43 while ( i < = p o s ) 44 if ( l i n k 1 ( i ) . n e x a d d = = 0 ) break ; 45 46 else i=i+1; 47 48 end 49 end 50 if ( l i n k 1 ( i ) . n e x a d d ~ = 0 ) 51 i=i-1; 52 lin2.data=ele; 53 lin2.add=i; j=i; 54 55 while ( l i n k 1 ( j ) . n e x a d d ~ = 0 )
41
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
link1(j).add=link1(j).add+1; link1(j).nexadd=link1(j).nexadd+1; j=j+1; end link1(j).add=link1(j).add+1; lin2.nexadd=link1(i).add; link1(i-1).nexadd=lin2.add; k=1; while ( k < i ) link2(k)=link1(k); k=k+1; end link2(k)=lin2; k=k+1; link2(k)=link1(k-1); k=k+1 l=k-1; while ( k ~ = j ) link2(k)=link1(l); k=k+1; l=l+1; end link2(j)=link1(j-1);; link2(j+1)=link1(j); else if ( i = = p o s & i ~ = 1 ) k=1; lin2.data=ele; lin2.add=link1(i-1).add+1; link1(i).add=link1(i).add+1; lin2.nexadd=link1(i).add; k=1; while ( k < p o s ) link2(k)=link1(k); k=k+1; end link2(k)=lin2; link2(k+1)=link1(k)
42
94 end if ( i = = p o s & i = = 1 ) 95 96 link2=append(ele,link1); return l i n k 2 ; 97 98 end end 99 100 e n d f u n c t i o n 101 function [ l i n k 2 ] = d e l e t e 1 ( p o s , l i n k 1 ) 102 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 103 i = 1 ; 104 while ( i < = p o s ) 105 if ( ( l i n k 1 ( i ) . n e x a d d = = 0 ) ) break ; 106 107 else i=i+1; 108 109 end end 110 111 if ( l i n k 1 ( i ) . n e x a d d ~ = 0 ) 112 i=i-1; 113 j=1; 114 if ( i = = 1 ) j=1; 115 116 while ( l i n k 1 ( j ) . n e x a d d ~ = 0 ) link2(j)=link1(j); 117 118 j=j+1; end 119 120 link2(j)=link1(j); else 121 122 link1(i-1).nexadd=link1(i+1).add; 123 while ( l i n k 1 ( j ) . n e x a d d ~ = l i n k 1 ( i + 1 ) . a d d ) 124 link2(j)=link1(j); 125 j=j+1; 126 end 127 if ( j ~ = i - 1 ) link2(j)=link1(j); 128 129 link2(j+1)=link1(j+1);
43
130 k=i+1; l=2; 131 132 else link2(j)=link1(j); 133 134 k=i+1; l=1; 135 136 end while ( l i n k 1 ( k ) . n e x a d d ~ = 0 ) 137 138 link2(j+l)=link1(k); 139 k=k+1; 140 l=l+1; 141 end link2(j+l)=link1(k); 142 143 end 144 else 145 if ( i = = p o s ) j=1; 146 147 link1(i-1).nexadd=0; while ( j < = i - 1 ) 148 149 link2(j)=link1(j); 150 j=j+1; 151 end 152 end end 153 154 e n d f u n c t i o n 155 156 157 158 / / C a l l i n g R o ut i n e : 159 l i n k 1 = s t r u c t ( ’ da ta ’ ,0 , ’ add ’ ,0 , ’ n e x a dd ’ ,0); / / C r e a t e s
empty l i s t 160 161 162 163 164 165
link1=append(4,link1) link1=append(6,link1) link1=add(7,2,link1) link1=append(8,link1) link1=delete1(4,link1) disp ( l i n k 1 , ” The l i n k e d l i s t m o d i f i c a t i o n s i s : ”);
44
a f t e r t he a bo ve
Scilab code Exa 4.2 Implementing Queue Operarions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/ / Queue
Operations
function [ q 2 ] = p u s h ( e l e , q 1 ) if ( q 1 . r e a r = = q 1 . f r o n t ) q1.a=ele; q1.rear=q1.rear+1; else q 1 . a =[ q 1 . a ( : , :) e l e ]; q1.rear=q1.rear+1; end q2=q1; endfunction function [ e l e , q 2 ] = p o p ( q 1 ) ele=-1; q2=0; if ( q 1 . r e a r = = q 1 . f r o n t ) disp ( ” Q ueu e U n d e r f l o w ” ) ; return ; else ele=q1.a(q1.rear-q1.front); q1.front=q1.front+1; i=1; a=q1.a(1); for i = 2 : ( q 1 . r e a r - q 1 . f r o n t ) a = [ a ( : , :) q 1 . a ( i ) ]; end q1.a=a; end q2=q1; endfunction
/ / C a l l i n g R o ut i n e : q 1 = s t r u c t ( ’ a ’ ,0 , ’ r e a r ’ ,0 , ’ f r o n t ’ ,0) q1=push(3,q1)
45
33 34 35 36 37 38 39 40 41
q1=push(22,q1); q1=push(21,q1); disp ( q 1 , ” Queue a f t e r i n s e r t i o n ” ) ; [ele,q1]=pop(q1) disp ( e l e , ” p op ed e l e m en t ” ) ; disp ( q 1 , ” Queue a f t e r p o pi n g ” ) ; [ele,q1]=pop(q1); [ele,q1]=pop(q1); [ e l e , q 1 ] = p o p ( q 1 ) ; / / U n d er f lo w C o n d i t i o n
Scilab code Exa 4.3 Implementing Circular Linked List
1 //CIRCULAR LINKED LIST 2 function [ l i n k 2 ] = a p p e n d ( e l e , l i n k 1 ) 3 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 4 if ( l i n k 1 ( 1 ) ( 1 ) . a d d = = 0 ) 5 link1(1)(1).data=ele; 6 link1(1)(1).add=1; 7 link1(1)(1).nexadd=1; 8 link2(1)=link1(1)(1); 9 else if ( l i n k 1 ( 1 ) ( 1 ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) 10 11 lin2=link1(1)(1); lin2.data=ele; 12 13 lin2.add=link1(1)(1).add+1; link1(1)(1).nexadd=lin2.add; 14 15 lin2.nexadd=link1(1)(1).add; 16 link2(1)=link1(1)(1); 17 link2(2)=lin2; 18 else 19 lin2=link1(1)(1); 20 i=1; while ( l i n k 1 ( i ) ( 1 ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) 21
46
22 i=i+1; end 23 24 j=i; lin2.data=ele; 25 26 lin2.add=link1(i).add+1; lin2.nexadd=link1(1)(1).add; 27 28 link1(i).nexadd=lin2.add; link2(1)=link1(1)(1); 29 30 i=2; 31 while ( l i n k 1 ( i ) . n e x a d d ~ = l i n 2 . a d d ) 32 link2(i)=(link1(i)); 33 i=i+1; end 34 35 link2(i)=link1(i); link2(i+1)=lin2; 36 37 end end 38 39 e n d f u n c t i o n 40 function [ l i n k 2 ] = a d d ( e l e , p o s , l i n k 1 ) ; 41 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 42 i = 1 ; 43 while ( i < = p o s ) 44 if ( l i n k 1 ( i ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) break ; 45 46 else i=i+1; 47 48 end end 49 50 if ( l i n k 1 ( i ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) 51 i=i-1; 52 lin2.data=ele; 53 lin2.add=i; 54 j=i; 55 while ( l i n k 1 ( j ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) link1(j).add=link1(j).add+1; 56 57 link1(j).nexadd=link1(j).nexadd+1;
47
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
j=j+1; end link1(j).add=link1(j).add+1; lin2.nexadd=link1(i).add; link1(i-1).nexadd=lin2.add; k=1; while ( k < i ) link2(k)=link1(k); k=k+1; end link2(k)=lin2; k=k+1; link2(k)=link1(k-1); k=k+1 l=k-1; while ( k ~ = j ) link2(k)=link1(l); k=k+1; l=l+1; end link2(j)=link1(j-1);; link2(j+1)=link1(j); else if ( i = = p o s ) k=1; lin2.data=ele; lin2.add=link1(i-1).add+1; link1(i).add=link1(i).add+1; lin2.nexadd=link1(i).add; link1(i).nexadd=link1(1)(1).add; k=1; while ( k < p o s ) link2(k)=link1(k); k=k+1; end link2(k)=lin2; link2(k+1)=link1(k) end
48
96 end 97 98 e n d f u n c t i o n 99 function [ l i n k 2 ] = d e l e t e 1 ( p o s , l i n k 1 ) 100 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 101 i = 1 ; 102 j = 1 ; 103 while ( i < p o s ) 104 if ( ( l i n k 1 ( j ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) ) 105 j=1; i=i+1; 106 107 else i=i+1; 108 109 j=j+1; end 110 111 end if ( l i n k 1 ( j ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) 112 113 k=1; 114 if ( j = = 1 ) 115 k=2; 116 while ( l i n k 1 ( k ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) link2(k-1)=link1(k); 117 118 k=k+1; end 119 120 link2(k-1)=link1(k); link2(k-1).nexadd=link2(1).add; 121 122 else lin2=link1(j); 123 124 link1(j-1).nexadd=link1(j+1).add; 125 k=1; 126 while ( l i n k 1 ( k ) . n e x a d d ~ = l i n k 1 ( j + 1 ) . a d d ) 127 link2(k)=link1(k); 128 k=k+1; 129 end link2(k)=link1(k); 130 131 k=k+2;
49
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
while ( l i n k 1 ( k ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) link2(k-1)=link1(k); k=k+1; end link2(k-1)=link1(k); end else link1(j-1).nexadd=link1(1)(1).add; l=1; while ( l i n k 1 ( l ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) link2(l)=link1(l); l=l+1; end link2(l)=link1(l); end endfunction
/ / C a l l i n g R o ut i n e : l i n k 1 = s t r u c t ( ’ da ta ’ ,0 , ’ add ’ ,0 , ’ n e x a dd ’ ,0); l i n k 1 = a p p e n d ( 4 , l i n k 1 ) ; // T hi s w i l l a c t u a l y c r e a t e a l i s t and 4 a s s t a r t
151 l i n k 1 = a p p e n d ( 6 , l i n k 1 ) ; 152 l i n k 1 = a d d ( 1 0 , 2 , l i n k 1 ) ; 153 l i n k 1 = d e l e t e 1 ( 4 , l i n k 1 ) ; // As t h e l i s t
i s c i r c u l a r th e 4 ’ th e le me nt r e f e r s t o a c t u a l y t he 1 ’ s t o ne 154 disp ( l i n k 1 , ” A f t e r t he a bo ve m an up la ti on s t he l i s t i s ”);
Scilab code Exa 4.4 Implementing Doubly connected Linked List
1 //DOUBLE LINKED LIST : 2 function [ l i n k 2 ] = a p p e n d ( e l e , l i n k 1 ) 3 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; if ( l i n k 1 ( 1 ) ( 1 ) . a d d = = 0 ) 4
50
5 link1(1)(1).data=ele; link1(1)(1).add=1; 6 7 link1(1)(1).nexadd=0; link1(1)(1).prevadd=0; 8 9 link2(1)=link1(1)(1); else 10 11 if ( l i n k 1 ( 1 ) ( 1 ) . n e x a d d = = 0 ) lin2=link1(1)(1); 12 13 lin2.data=ele; 14 lin2.add=link1(1)(1).add+1; 15 link1(1)(1).nexadd=lin2.add; 16 lin2.nexadd=0; lin2.prevadd=link1(1)(1).add; 17 18 link2(1)=link1(1)(1); link2(2)=lin2; 19 20 else lin2=link1(1)(1); 21 22 i=1; while ( l i n k 1 ( i ) ( 1 ) . n e x a d d ~ = 0 ) 23 24 i=i+1; 25 end 26 j=i; 27 lin2.data=ele; lin2.add=link1(i).add+1; 28 29 lin2.nexadd=0; link1(i).nexadd=lin2.add; 30 31 lin2.prevadd=link1(i).add; link2(1)=link1(1)(1); 32 33 i=2; while ( l i n k 1 ( i ) . n e x a d d ~ = l i n 2 . a d d ) 34 35 link2(i)=(link1(i)); 36 i=i+1; 37 end 38 link2(i)=link1(i); 39 link2(i+1)=lin2; 40 end end 41 42 e n d f u n c t i o n
51
43 function [ l i n k 2 ] = a d d ( e l e , p o s , l i n k 1 ) ; 44 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 45 i = 1 ; 46 while ( i < = p o s ) 47 if ( l i n k 1 ( i ) . n e x a d d = = 0 ) break ; 48 49 else 50 i=i+1; 51 end 52 end if ( l i n k 1 ( i ) . n e x a d d ~ = 0 ) 53 54 i=i-1; lin2.data=ele; 55 56 lin2.add=i; j=i; 57 58 while ( l i n k 1 ( j ) . n e x a d d ~ = 0 ) link1(j).prevadd=link1(j).prevadd+1; 59 60 link1(j).add=link1(j).add+1; 61 link1(j).nexadd=link1(j).nexadd+1; 62 j=j+1; 63 end link1(j).prevadd=link1(j).prevadd+1; 64 65 link1(j).add=link1(j).add+1; lin2.nexadd=link1(i).add; 66 67 link1(i).prevadd=lin2.add; lin2.prevadd=link1(i-1).add; 68 69 link1(i-1).nexadd=lin2.add; k=1; 70 71 while ( k < i ) 72 link2(k)=link1(k); 73 k=k+1; 74 end 75 link2(k)=lin2; 76 k=k+1; link2(k)=link1(k-1); 77 78 k=k+1
52
79 l=k-1; while ( k ~ = j ) 80 81 link2(k)=link1(l); k=k+1; 82 83 l=l+1; end 84 85 link2(j)=link1(j-1);; link2(j+1)=link1(j); 86 87 else 88 if ( i = = p o s ) 89 k=1; 90 lin2.data=ele; lin2.add=link1(i-1).add+1; 91 92 link1(i).add=link1(i).add+1; lin2.nexadd=link1(i).add; 93 94 link1(i).prevadd=lin2.add; lin2.prevadd=link1(i-1).add; 95 96 k=1; while ( k < p o s ) 97 98 link2(k)=link1(k); 99 k=k+1; 100 end 101 link2(k)=lin2; link2(k+1)=link1(k) 102 103 end end 104 105 106 e n d f u n c t i o n 107 function [ l i n k 2 ] = d e l e t e 1 ( p o s , l i n k 1 ) 108 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 109 i = 1 ; 110 while ( i < = p o s ) 111 if ( ( l i n k 1 ( i ) . n e x a d d = = 0 ) ) 112 break ; else 113 114 i=i+1;
53
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
end end if ( l i n k 1 ( i ) . n e x a d d ~ = 0 ) i=i-1; j=1; if ( i = = 1 ) j=1; while ( l i n k 1 ( j ) . n e x a d d ~ = 0 ) link2(j)=link1(j); j=j+1; end link2(j)=link1(j); else link1(i-1).nexadd=link1(i+1).add; link1(i+1).prevadd=link1(i-1).add; while ( l i n k 1 ( j ) . n e x a d d ~ = l i n k 1 ( i + 1 ) . a d d ) link2(j)=link1(j); j=j+1; end if ( j ~ = i - 1 ) link2(j)=link1(j); link2(j+1)=link1(j+1); k=i+1; l=2; else link2(j)=link1(j); k=i+1; l=1; end while ( l i n k 1 ( k ) . n e x a d d ~ = 0 ) link2(j+l)=link1(k); k=k+1; l=l+1; end link2(j+l)=link1(k); end else if ( i = = p o s )
54
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
j=1; link1(i-1).nexadd=0; while ( j < = i - 1 ) link2(j)=link1(j); j=j+1; end end end endfunction
/ / C a l l i n g R o ut i n e : l i n k 1 = s t r u c t ( ’ da ta ’ ,0 , ’ add ’ ,0 , ’ n e x a dd ’ ,0);
link1=append(4,link1); link1=append(6,link1); link1=add(10,2,link1); link1=delete1(3,link1); disp ( l i n k 1 , ” A f t e r t he a bo ve m an up la ti on s t he ”);
list
is
Scilab code Exa 4.5 Implementing Stack using circular Linked list
1 //STACK USING CIRCULAR LINKED LIST 2 funcprot (0) 3 function [ l i n k 2 ] = a p p e n d ( e l e , l i n k 1 ) 4 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 5 if ( l i n k 1 ( 1 ) ( 1 ) . a d d = = 0 ) link1(1)(1).data=ele; 6 7 link1(1)(1).add=1; 8 link1(1)(1).nexadd=1; 9 link2(1)=link1(1)(1); 10 else 11 if ( l i n k 1 ( 1 ) ( 1 ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) 12 lin2=link1(1)(1); lin2.data=ele; 13
55
14 lin2.add=link1(1)(1).add+1; link1(1)(1).nexadd=lin2.add; 15 16 lin2.nexadd=link1(1)(1).add; link2(1)=link1(1)(1); 17 18 link2(2)=lin2; else 19 20 lin2=link1(1)(1); i=1; 21 22 while ( l i n k 1 ( i ) ( 1 ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) 23 i=i+1; 24 end 25 j=i; lin2.data=ele; 26 27 lin2.add=link1(i).add+1; lin2.nexadd=link1(1)(1).add; 28 29 link1(i).nexadd=lin2.add; link2(1)=link1(1)(1); 30 31 i=2; while ( l i n k 1 ( i ) . n e x a d d ~ = l i n 2 . a d d ) 32 33 link2(i)=(link1(i)); 34 i=i+1; 35 end 36 link2(i)=link1(i); link2(i+1)=lin2; 37 38 end end 39 40 e n d f u n c t i o n 41 function [ l i n k 2 ] = a d d ( e l e , p o s , l i n k 1 ) ; 42 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 43 i = 1 ; 44 while ( i < = p o s ) 45 if ( l i n k 1 ( i ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) 46 break ; 47 else i=i+1; 48 49 end
56
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
end if ( l i n k 1 ( i ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) i=i-1; lin2.data=ele; lin2.add=i; j=i; while ( l i n k 1 ( j ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) link1(j).add=link1(j).add+1; link1(j).nexadd=link1(j).nexadd+1; j=j+1; end link1(j).add=link1(j).add+1; lin2.nexadd=link1(i).add; link1(i-1).nexadd=lin2.add; k=1; while ( k < i ) link2(k)=link1(k); k=k+1; end link2(k)=lin2; k=k+1; link2(k)=link1(k-1); k=k+1 l=k-1; while ( k ~ = j ) link2(k)=link1(l); k=k+1; l=l+1; end link2(j)=link1(j-1);; link2(j+1)=link1(j); else if ( i = = p o s ) k=1; lin2.data=ele; lin2.add=link1(i-1).add+1; link1(i).add=link1.add+1; lin2.nexadd=link1(i).add;
57
88 link1(i).nexadd=link1(1)(1).add; k=1; 89 90 while ( k < p o s ) link2(k)=link1(k); 91 92 k=k+1; end 93 94 link2(k)=lin2; link2(k+1)=link1(k) 95 96 end 97 end 98 99 e n d f u n c t i o n 100 function [ l i n k 2 ] = d e l e t e 1 ( p o s , l i n k 1 ) 101 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 102 i = 1 ; 103 if ( l i n k 1 ( 1 ) ( 1 ) . a d d = = 0 ) disp ( ” I n v a l i d ” ) ; 104 105 else 106 if ( l i n k 1 ( 1 ) ( 1 ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) 107 link1(1)(1).add=0; 108 link1(1)(1).nexadd=0; link1(1)(1).data=0; 109 110 link2(1)=link1(1)(1); else 111 112 while ( i < = p o s ) if ( ( l i n k 1 ( i ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) ) 113 114 break ; else 115 116 i=i+1; 117 end 118 end 119 if ( l i n k 1 ( i ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) 120 i=i-1; 121 j=1; if ( i = = 1 ) 122 123 j=1;
58
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
while ( l i n k 1 ( j ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) link2(j)=link1(j); j=j+1; end link2(j)=link1(j); else link1(i-1).nexadd=link1(i+1).add; while ( l i n k 1 ( j ) . n e x a d d ~ = l i n k 1 ( i + 1 ) . a d d ) link2(j)=link1(j); j=j+1; end if ( j ~ = i - 1 ) link2(j)=link1(j); link2(j+1)=link1(j+1); k=i+1; l=2; else link2(j)=link1(j); k=i+1; l=1; end while ( l i n k 1 ( k ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) link2(j+l)=link1(k); k=k+1; l=l+1; end link2(j+l)=link1(k); end else if ( i = = p o s ) j=1; link1(i-1).nexadd=link1(1)(1).add; while ( j < = i - 1 ) link2(j)=link1(j); j=j+1; end end end
59
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
end end endfunction function [ s t a ] = p u s h ( e l e , s t a c k ) if ( s t a c k . t o p = = 0 ) stack.a=ele; stack.top=stack.top+1; sta=stack; else i=1; l i n k 1 = s t r u c t ( ’ da ta ’ ,0 , ’ add ’ ,0 , ’ n e x a dd ’ ,0); while ( i < = s t a c k . t o p ) link1=append(stack.a(i),link1); i=i+1; end link1=append(ele,link1); stack.top=stack.top+1; a = [ s t ac k . a ( : , : ) l i nk 1 ( s t a ck . t o p ) . d a ta ] ; stack.a=a; sta=stack; end endfunction function [ e l e , s t a ] = p o p ( s t a c k ) ele=-1; sta=0; if ( s t a c k . t o p = = 0 ) disp ( ” S t a c k U n de r fl o w ” ) ; return ; else i=1; l i n k 1 = s t r u c t ( ’ da ta ’ ,0 , ’ add ’ ,0 , ’ n e x a dd ’ ,0); while ( i < = s t a c k . t o p ) link1=append(stack.a(i),link1); i=i+1; end ele=link1(stack.top).data; link1=delete1(stack.top,link1);
60
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
stack.top=stack.top-1; i=2; a=link1(1)(1).data while ( i < = s t a c k . t o p ) a = [ a ( : , :) l i nk 1 ( i ) . d a ta ] ; i=i+1; end stack.a=a; sta=stack; end endfunction function [ s t a c k ] = e m p t y ( ) s t a c k = s t r u c t ( ’ a ’ ,0 , ’ top ’ ,0); endfunction
/ / C a l l i n g R o ut i n e : s t a c k = e m p t y ( ) / / C r e a t e a n em pty s t a c k
stack=push(4,stack); stack=push(55,stack); stack=push(199,stack); stack=push(363,stack); [ele,stack]=pop(stack); disp ( s t a c k , ” A f t er t he a b o v e o p e r a ti o n s s t a c k i s : ” ) ;
Scilab code Exa 4.6 Implementing Priority Queue Using Lists
1 / / I mp le me nt in g P r i o r i t y Queue U si ng L i s t s 2 funcprot (0) 3 function [ l i n k 2 ] = i n s e r t _ p r i ( e l e , l i n k 1 ) 4 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 5 if ( l i n k 1 ( 1 ) ( 1 ) . a d d = = 0 ) 6 link1(1)(1).data=ele; 7 link1(1)(1).add=1; link1(1)(1).nexadd=1; 8
61
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
link2(1)=link1(1)(1); else if ( l i n k 1 ( 1 ) ( 1 ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) if ( e l e > = l i n k 1 ( 1 ) ( 1 ) . d a t a ) t=ele; p=link1(1)(1).data; else t=link1(1)(1).data; p=ele; end link1(1)(1).data=t; lin2=link1(1)(1); lin2.data=p; lin2.add=2; lin2.nexadd=link1(1)(1).add; link1(1)(1).nexadd=lin2.add; link2(1)=link1(1)(1); link2(2)=lin2; else i=1; a=[]; while ( l i n k 1 ( i ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) a = [ a ( : , :) l i nk 1 ( i ) . d a ta ] ; i=i+1; end a = [ a ( : , :) l i nk 1 ( i ) . d a ta ] ; a = gsort ( a ) ; j=1; while ( j < = i ) link1(j).data=a(j); j=j+1; end k=1; while ( l i n k 1 ( k ) . d a t a > = e l e ) if ( l i n k 1 ( k ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) break ; else link2(k)=link1(k);
62
47 k=k+1; end 48 49 end if ( l i n k 1 ( k ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) 50 51 lin2=link1(k); lin2.data=ele; 52 53 lin2.add=link1(k).add; j=k; 54 55 y=link1(1)(1).add; 56 while ( l i n k 1 ( k ) . n e x a d d ~ = y ) 57 link1(k).add=link1(k).add+1; 58 link1(k).nexadd=link1(k).nexadd+1; k=k+1; 59 60 end link1(k).add=link1(k).add+1; 61 62 lin2.nexadd=link1(j).add; link2(j)=lin2; 63 64 j=j+1; while ( j < = k + 1 ) 65 66 link2(j)=link1(j-1); 67 j=j+1; 68 end 69 else lin2=link1(k); 70 71 lin2.data=ele; lin2.nexadd=link1(1)(1).add; 72 73 lin2.add=link1(k).add+1; link1(k).nexadd=lin2.add; 74 75 j=1; while ( j < = k ) 76 77 link2(j)=link1(j); 78 j=j+1; 79 end 80 link2(j)=lin2; 81 end 82 end end 83 84 e n d f u n c t i o n
63
85 function [ e l e , l i n k 2 ] = e x t r a c t _ m i n ( l i n k 1 ) ; 86 l i n k 2 = list ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0 , 0 , 0 , , 0 , 0 ) ; 87 i = 1 ; 88 e l e = - 1 ; 89 if ( l i n k 1 ( 1 ) ( 1 ) . a d d = = 0 ) disp ( ” U n d e r f l o w ” ) ; 90 91 return ; 92 else 93 if ( l i n k 1 ( 1 ) ( 1 ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) 94 link1(1)(1).add=0; link1(1)(1).nexadd=0; 95 96 ele=link1(1)(1).data; link1(1)(1).data=0; 97 98 link2(1)=link1(1)(1); else 99 100 i=1; while ( l i n k 1 ( i ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d ) 101 102 link2(i)=link1(i); 103 i=i+1; 104 end 105 ele=link1(i).data; link2(i-1).nexadd=link2(1).add; 106 107 end end 108 109 e n d f u n c t i o n 110 / / C a l l i n g R o u ti n e : 111 l i n k 1 = s t r u c t ( ’ dat a ’ ,0 , ’ add ’ ,0 , ’ n e x a dd ’ ,0); 112 l i n k 1 = i n s e r t _ p r i ( 3 , l i n k 1 ) ; 113 l i n k 1 = i n s e r t _ p r i ( 4 , l i n k 1 ) ; 114 l i n k 1 = i n s e r t _ p r i ( 2 2 , l i n k 1 ) ; 115 l i n k 1 = i n s e r t _ p r i ( 2 1 , l i n k 1 ) ; 116 l i n k 1 = i n s e r t _ p r i ( 1 1 , l i n k 1 ) ; 117 disp ( l i n k 1 , ” L i s t A f t e r I n s e r t i o n s ” ) ; 118 [ e l e , l i n k 1 ] = e x t r a c t _ m i n ( l i n k 1 ) 119 disp ( e l e , ” E lemen t a f t e r t he min e x t r a c t i o n ” ) ;
64
Chapter 5 Trees
Scilab code Exa 5.1 Implementing Binary Tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
funcprot ( 0 ) ; function [ t r e e ] = m a k e t r e e ( x ) t r e e = zeros ( 3 0 , 1 ) ; for i = 1 : 3 0 tree(i)=-1; end tree(1)=x; tree(2)=-2; endfunction function [ t r e e 1 ] = s e t l e f t ( t r e e , t r e , x ) tree1=[]; i=1; while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) j=i; end i=i+1; end if ( i > 2 * j ) tree(2*j)=x;
65
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
else tree(2*j)=x; tree(2*j+1)=-2; for l = i : 2 * j - 1 tree(i)=-1; end end tree1=tree; endfunction function [ t r e e 1 ] = s e t r i g h t ( t r e e , t r e , x ) tree1=[]; i=1; while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) j=i; end i=i+1; end if ( i > 2 * j + 1 ) tree(2*j+1)=x; else tree(2*j+1)=x; tree(2*j+2)=-2; for l = i : 2 * j tree(i)=-1; end end tree1=tree; endfunction function [ x ] = i s l e f t ( t r e e , t r e ) i=1; x=0; while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) j=i; end i=i+1; end
66
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
if ( i > = 2 * j ) if ( ( t r e e ( 2 * j ) ~ = - 1 ) | ( t r e e ( 2 * j ) ~ = - 2 ) ) x=1; return 1; else return 0; end else x=0; return x ; end endfunction function [ x ] = i s r i g h t ( t r e e , t r e ) i=1; x=0; while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) j=i; end i=i+1; end if ( i > = 2 * j + 1 ) if ( ( t r e e ( 2 * j + 1 ) ~ = - 1 ) | ( t r e e ( 2 * j + 1 ) ~ = - 2 ) ) x=1; return 1; else return 0; end else x=0; return x ; end endfunction
/ / C a l l i n g R o ut i n e : tree=maketree(3); disp ( t r e e , ” T r e e ma de ” ) ; tree= setleft (tree ,3,1); disp ( t r e e , ” A f te r s e t t i n g 1 t o
67
l e f t o f 3 ”);
98 99 100 101 102 103 104 105 106
107 108
tree=setright(tree ,3,2); disp ( t r e e , ” A ft e r s e t t i n g 2 t o r i g h t o f 3 ” ) ; tree=setright(tree ,2,4); tree= setleft (tree ,2,5); tree=setright(tree ,1,6); tree=setright(tree ,5,8); disp ( t r e e , ” A f t e r a bo ve o p e r a t i o n s : ” ) ; x= isright (tree ,3); disp (x , ” Ch e c k i n g f o r t h e r i g h t s o n o f 3 y e s i f 1 e l s e no ” ) ; x= isleft( tree ,2); disp (x , ” Check f o r l e f t s on o f 2 ” ) ;
Scilab code Exa 5.2 Tree Trversal Techniques
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
funcprot ( 0 ) ; function [ t r e e ] = m a k e t r e e ( x ) t r e e = zeros ( 3 0 , 1 ) ; for i = 1 : 3 0 tree(i)=-1; end tree(1)=x; tree(2)=-2; endfunction function [ t r e e 1 ] = s e t l e f t ( t r e e , t r e , x ) tree1=[]; i=1; while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) j=i; end i=i+1; end if ( i > 2 * j ) tree(2*j)=x;
68
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
else tree(2*j)=x; tree(2*j+1)=-2; for l = i : 2 * j - 1 tree(i)=-1; end end tree1=tree; endfunction function [ t r e e 1 ] = s e t r i g h t ( t r e e , t r e , x ) tree1=[]; i=1; while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) j=i; end i=i+1; end if ( i > 2 * j + 1 ) tree(2*j+1)=x; else tree(2*j+1)=x; tree(2*j+2)=-2; for l = i : 2 * j tree(i)=-1; end end tree1=tree; endfunction function [ x ] = i s l e f t ( t r e e , t r e ) i=1; x=0; while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) j=i; end i=i+1; end
69
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
if ( i > = 2 * j ) if ( ( t r e e ( 2 * j ) ~ = - 1 ) | ( t r e e ( 2 * j ) ~ = - 2 ) ) x=1; return 1; else return 0; end else x=0; return x ; end endfunction function [ x ] = i s r i g h t ( t r e e , t r e ) i=1; x=0; while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) j=i; end i=i+1; end if ( i > = 2 * j + 1 ) if ( ( t r e e ( 2 * j + 1 ) ~ = - 1 ) | ( t r e e ( 2 * j + 1 ) ~ = - 2 ) ) x=1; return 1; else return 0; end else x=0; return x ; end endfunction funcprot ( 0 ) ; function [ ] = i n o r d e r ( t r e e , p ) if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 ) return ; else
70
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
inorder (tree ,2*p); printf ( ”%d\ t ” , t r e e ( p ) ) ; inorder (tree ,2*p+1) ; end endfunction function [ ] = p r e o r d e r ( t r e e , p ) if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 ) return ; else printf ( ”%d\ t ” , t r e e ( p ) ) ; preorder (tree ,2*p); preorder (tree ,2*p +1); end endfunction function [ ] = p o s t o r d e r ( t r e e , p ) if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 ) return ; else postorder (tree ,2*p); postorder (tree ,2*p +1); printf ( ”%d\ t ” , t r e e ( p ) ) ; end endfunction
/ / C a l l i n g R o ut i n e : tree=maketree(3); tree= setleft (tree ,3,1); tree=setright(tree ,3,2); tree= setleft (tree ,2,4); tree=setright(tree ,2,5); disp ( ” I n o r d er t r a v e r s a l ” ) ; inorder (tree ,1); disp ( ” P r e o rd e r t r a v e r s a l ” ) ; preorder (tree ,1); disp ( ” P o s to r de r t r a v e r s a l ” ) ; postorder (tree ,1);
71
Scilab code Exa 5.3 Implementing And traversing a Binary Search Tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
funcprot ( 0 ) ; function [ t r e e ] = m a k e t r e e ( x ) t r e e = zeros ( 1 , 3 0 ) ; for i = 1 : 3 0 tree(i)=-1; end tree(1)=x; tree(2)=-2; endfunction function [ t r e e 1 ] = s e t l e f t ( t r e e , t r e , x ) tree1=[]; i=1; while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) j=i; end i=i+1; end if ( i > 2 * j ) tree(2*j)=x; else tree(2*j)=x; tree(2*j+1)=-2; for l = i : 2 * j - 1 tree(i)=-1; end end tree1=tree; endfunction function [ t r e e 1 ] = s e t r i g h t ( t r e e , t r e , x ) tree1=[]; i=1;
72
33 while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) 34 35 j=i; end 36 37 i=i+1; end 38 39 if ( i > 2 * j + 1 ) tree(2*j+1)=x; 40 41 else 42 tree(2*j+1)=x; 43 tree(2*j+2)=-2; 44 for l = i : 2 * j tree(i)=-1; 45 46 end end 47 48 t r e e 1 = t r e e ; 49 e n d f u n c t i o n 50 function [ x ] = i s l e f t ( t r e e , t r e ) 51 i = 1 ; 52 x = 0 ; 53 while ( t r e e ( i ) ~ = - 2 ) 54 if ( t r e e ( i ) = = t r e ) 55 j=i; end 56 57 i=i+1; end 58 59 if ( i > = 2 * j ) if ( ( t r e e ( 2 * j ) ~ = - 1 ) | ( t r e e ( 2 * j ) ~ = - 2 ) ) 60 61 x=1; return 1; 62 63 else 64 return 0; 65 end 66 else 67 x=0; 68 return x ; end 69 70 e n d f u n c t i o n
73
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
function [ x ] = i s r i g h t ( t r e e , t r e ) i=1; x=0; while ( t r e e ( i ) ~ = - 2 ) if ( t r e e ( i ) = = t r e ) j=i; end i=i+1; end if ( i > = 2 * j + 1 ) if ( ( t r e e ( 2 * j + 1 ) ~ = - 1 ) | ( t r e e ( 2 * j + 1 ) ~ = - 2 ) ) x=1; return 1; else return 0; end else x=0; return x ; end endfunction funcprot ( 0 ) ; function [ ] = i n o r d e r ( t r e e , p ) if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 ) return ; else inorder (tree ,2*p); disp ( t r e e ( p ) , ” ” ) ; inorder (tree ,2*p+1) ; end endfunction function [ ] = p r e o r d e r ( t r e e , p ) if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 ) return ; else disp ( t r e e ( p ) , ” ” ) ; preorder (tree ,2*p); preorder (tree ,2*p +1);
74
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
end endfunction function [ ] = p o s t o r d e r ( t r e e , p ) if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 ) return ; else postorder (tree ,2*p); postorder (tree ,2*p +1); disp ( t r e e ( p ) , ” ” ) ; end endfunction function [ t r e e 1 ] = b i n a r y ( t r e e , x ) p=1; while ( t r e e ( p ) ~ = - 1 & t r e e ( p ) ~ = - 2 ) q=p; if ( t r e e ( p ) > x ) p=2*p; else p=2*p+1; end end i=1; while ( t r e e ( i ) ~ = - 2 ) i=i+1; end if ( t r e e ( q ) > x ) if ( i = = 2 * q ) tree(2*q)=x; tree(2*q+1)=-2 else if ( i < 2 * q ) tree(i)=-1; tree(2*q+1)=-2; tree(2*q)=x; end end
else
75
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
if ( i = = 2 * q + 1 ) tree(2*q+1)=x; tree(2*q+2)=-2; else if ( i < 2 * q + 1 ) tree(i)=-1; tree(2*q+1)=x; tree(2*q+2)=-2; end end end tree1=tree; endfunction
/ / C a l l i n g R o ut i n e : tree=maketree(3); tree= binary( tree ,1); tree= binary( tree ,2); tree= binary( tree ,4); tree= binary( tree ,5); disp ( t r e e , ” B i n ar y t r e e t hu s o b t a i n e by i n s e r t i n g 1 , 2 , 4 and5 i n t r e e r o o te d 3 i s : ” ) ;
Scilab code Exa 5.4 Checking the duplicate number using BST
1 function [ t r e e 1 ] = b i n a r y ( t r e e , x ) 2 p=1; 3 while ( t r e e ( p ) ~ = - 1 & t r e e ( p ) ~ = - 2 ) 4 q=p; 5 if ( t r e e ( p ) > x ) 6 p=2*p; 7 else 8 p=2*p+1; 9 end end 10
76
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
if ( t r e e ( q ) > x ) if ( t r e e ( 2 * q ) = = - 2 ) tree(2*q)=x; tree(2*q+1)=-2; else tree(2*q)=x; end else if ( t r e e ( 2 * q + 1 ) = = - 2 ) tree(2*q+1)=x; tree(2*q+2)=-2; else tree(2*q+1)=x; end end tree1=tree; endfunction funcprot ( 0 ) ; function [ t r e e ] = m a k e t r e e ( x ) t r e e = zeros ( 4 0 , 1 ) ; for i = 1 : 4 0 tree(i)=-1; end tree(1)=x; tree(2)=-2; endfunction function [ ] = d u p l i c a t e 1 ( a , n ) tree=maketree(a(1)); q=1; p=1; i=2; x=a(i) while ( i < n ) while ( t r e e ( p ) ~ = x & t r e e ( q ) ~ = - 1 & t r e e ( q ) ~ = - 2 ) p=q; if ( t r e e ( p ) < x ) q=2*p; else
77
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
q=2*p+1; end end if ( t r e e ( p ) = = x ) ”); disp (x , ” D u p li ca te else tree=binary(tree,x); end i=i+1; x=a(i); end while ( t r e e ( p ) ~ = x & t r e e ( q ) ~ = - 1 & t r e e ( q ) ~ = - 2 ) p=q; if ( t r e e ( p ) < x ) q=2*p; else q=2*p+1; end end if ( t r e e ( p ) = = x ) disp (x , ” D u p li ca te ”); else tree=binary(tree,x); end endfunction
/ / C a l l i n g A d r es s : a = [2 2 11 33 22 21 1 3 34] duplicate1(a,6) a = [2 1 11 33 22 22 33 4] duplicate1(a,6)
78
Chapter 6 Sorting
Scilab code Exa 6.1 Bubble Sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
function [ a 1 ] = b u b b l e ( a , n ) i=1; j=1; temp=0; for i = 1 : n - 1 for j = 1 : n - i if ( a ( j ) > a ( j + 1 ) ) temp=a(j); a(j)=a(j+1); a(j+1)=temp; end j=j+1; end i=i+1; end a1=a; disp ( a 1 , ” S o r te d a r ra y i s : ” ) ; endfunction
/ / C a l l i n g R o ut i n e : a = [ 23 21 2 32 1 21 2 32 4 1 22 24 33 1 21 2] disp (a , ” G i ve n A rr a y ” ) ;
79
22 a 1 = b u b b l e ( a , 7 )
Scilab code Exa 6.2 Quick Sort
1 2 3 4 5 6 7 8 9 10 11 12 13
function [ a 1 ] = q u i c k ( a ) ; a = gsort ( a ) ; // IN BUILT QUICK SORT FUNCTION n = length ( a ) ; a1=[]; for i = 1 : n a 1 = [ a1 ( : , : ) a ( n + 1 - i ) ]; end disp ( a 1 , ” S o r te d a r ra y i s : ” ) ; endfunction
/ / C a l l i n g R o ut i n e : a = [ 23 21 2 32 1 21 2 32 4 1 22 24 33 1 21 2] disp (a , ” G i ve n A rr a y ” ) ; a1=quick(a)
Scilab code Exa 6.3 Selection Sort
1 function [ a 1 ] = s e l e c t i o n ( a , n ) 2 i=n; 3 while ( i > = 1 ) 4 large=a(1); indx=1; 5 6 for j = 1 : i 7 if ( a ( j ) > l a r g e ) 8 large=a(j); 9 indx=j; 10 end 11 end a(indx)=a(i); 12 13 a(i)=large;
80
14 15 16 17 18 19 20 21 22
i=i-1; end a1=a; disp ( a 1 , ” S o r te d a r ra y i s : ” ) ; endfunction
/ / C a l l i n g R o ut i n e : a = [ 23 21 2 32 1 21 2 32 4 1 22 24 33 1 21 2] disp (a , ” G i ve n A rr a y ” ) ; a1=selection(a,7)
Scilab code Exa 6.4 Insertion Sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
function [ a 1 ] = i n s e r t i o n ( a , n ) for k = 1 : n y=a(k); i=k; while ( i > = 1 ) if ( y < a ( i ) ) a(i+1)=a(i); a(i)=y; end i=i-1; end end a1=a; disp ( a 1 , ” S o r te d a r ra y i s : ” ) ; endfunction
/ / C a l l i n g R o ut i n e : a = [ 23 21 2 32 1 21 2 32 4 1 22 24 33 1 21 2] disp (a , ” G i ve n A rr a y ” ) ; a1=insertion(a,7)
Scilab code Exa 6.5 Shell sort
81
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
function [ a 1 ] = s h e l l ( a , n , i n c r , n i c ) for i = 1 : n i c span=incr(i); for j = s p a n + 1 : n y=a(j); k=j-span; while ( k > = 1 & y < a ( k ) ) a(k+span)=a(k); k=k-span; end a(k+span)=y; end end a1=a; disp ( a 1 , ” S o r te d a r ra y i s : ” ) ; endfunction
/ / C a l l i n g R o ut i n e : a = [ 23 21 2 32 1 21 2 32 4 1 22 24 33 1 21 2] disp (a , ” G i ve n A rr a y ” ) ; i nc r =[ 5 3 1] // must a l wa y s end w it h 1 a1= shell(a ,7,incr ,3)
Scilab code Exa 6.6 Merge Sort
1 function [ a 1 ] = m e r g e s o r t ( a , p , r ) if ( p < r ) 2 3 q = int ( ( p + r ) / 2 ) ; a=mergesort(a,p,q); 4 5 a=mergesort(a,q+1,r); 6 a=merge(a,p,q,r); 7 else 8 a1=a; 9 return ; 10 end 11 a 1 = a ;
82
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
endfunction function [ a 1 ] = m e r g e ( a , p , q , r ) n1=q-p+1; n2=r-q; l e f t = zeros ( n 1 + 1 ) ; r i g h t = zeros ( n 2 + 1 ) ; for i = 1 : n 1 left(i)=a(p+i-1); end for i 1 = 1 : n 2 right(i1)=a(q+i1); end left(n1+1)=999999999; right(n2+1)=999999999; i=1; j=1; k=p; for k = p : r if ( l e f t ( i ) < = r i g h t ( j ) ) a(k)=left(i); i=i+1; else a(k)=right(j); j=j+1; end end a1=a; endfunction
/ / C a l l i n g R o ut i n e :
a = [2 3 21 2 32 1 21 2 63 24 1 22 24 33 1 42 12 ] disp (a , ” G i ve n A rr a y ” ) ; a1=mergesort(a,1,7) disp ( a 1 , ” S o r te d a r ra y i s : ” ) ; a = [ 23 2 1 12 12 3 44 3 2 32 21 1 23 42 4 3 23 34 1 22 12 2 44 3 2 32 ] disp (a , ” G i ve n A rr a y ” ) ; a1=mergesort(a,1,9); disp ( a 1 , ” S o r t e d A rr ay ” ) ;
83
Scilab code Exa 6.7 Binary Tree Sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
function [ t r e e 1 ] = b i n a r y ( t r e e , x ) p=1; while ( t r e e ( p ) ~ = - 1 & t r e e ( p ) ~ = - 2 ) q=p; if ( t r e e ( p ) > x ) p=2*p; else p=2*p+1; end end if ( t r e e ( q ) > x ) tree(2*q)=x; else tree(2*q+1)=x; end tree1=tree; endfunction funcprot ( 0 ) ; function [ t r e e ] = m a k e t r e e ( x ) t r e e = zeros ( 1 0 0 , 1 ) ; for i = 1 : 1 0 0 tree(i)=-1; end tree(1)=x; tree(2)=-2; endfunction function [ ] = i n o r d e r ( t r e e , p ) if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 ) return ; else inorder (tree ,2*p); printf ( ”%d\ t ” , t r e e ( p ) ) ;
84
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
inorder (tree ,2*p+1) ; end endfunction function [ ] = b i n s o r t ( a , n ) a1=maketree(a(1)) for i = 2 : n a1=binary(a1,a(i)); end disp ( ” S o r te d a r ra y i s : ” ) ; inorder(a1,1); endfunction
/ / C a l l i n g R o ut i n e : a = [ 23 21 2 32 1 21 2 32 4 1 22 24 33 1 21 2] disp (a , ” G i ve n A rr a y ” ) ; a1=binsort(a,7)
85
Chapter 7 Searching
Scilab code Exa 7.1 Sequential Search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
function [ ] = s e a r c h ( a , n , e l e ) i=1; j=0; for i = 1 : n if ( a ( i ) = = e l e ) printf ( ” Found %d AT %d\ n ” , e l e , i ) ; j=1; end end if ( j = = 0 ) disp ( ”%d NOT FOUND” , e l e ) ; end endfunction
/ / C a l l i n g R o ut i n e : a = [2 33 22 121 23 2 33 2 22] disp (a , ” G ive n a r r a y ” ) ; search(a,7,23)
Scilab code Exa 7.2 Sorted sequential search
86
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
function [ ] = s o r t e d s e a r c h ( a , n , e l e ) if ( a ( 1 ) > e l e | a ( n ) < e l e ) disp ( ”NOT IN THE LIST ” ) ; else i=1; j=0; for i = 1 : n if ( a ( i ) = = e l e ) printf ( ”FOUND %d AT %d” , e l e , i ) ; j=1; else if ( a ( i ) > e l e ) break ; end end end if ( j = = 0 ) disp ( ”%d NOT FOUND” , e l e ) ; end end endfunction
/ / C a l l i n g R o ut i n e : a = [2 22 23 33 121 2 22 2 33] // a s h o ul d b e disp (a , ” G ive n a r r a y ” ) ; search(a,7,23)
Scilab code Exa 7.3 Binary Search
1 function [ ] = b i n s e a r c h ( a , n , i ) 2 l=1; 3 h=n; 4 while ( l < = h ) 5 m i d = int ( ( l + h ) / 2 ) ; 6 if ( a ( m i d ) = = i ) printf ( ”FOUND %d AT %d” , i , m i d ) ; 7
87
s or te d
8 9 10 11 12 13 14 15 16 17 18 19 20 21
break ; else if ( a ( m i d ) > i ) h=mid-1; else l=mid+1; end end end endfunction
/ / C a l l i n g R o ut i n e : a = [2 22 23 33 121 2 22 23 3] // a s h o ul d b e disp (a , ” G ive n a r r a y ” ) ; search(a,7,23)
88
s or te d
Chapter 8 Graphs
Scilab code Exa 8.1 Simple Graph Functions
1 / / S i m p l e Gra ph F u n c t i o n s 2 function [ ] = g r a p h ( ) ; 3 4 i=1,j=1; 5 a d j = zeros ( 1 0 0 0 0 ) ; 6 for i = 1 : n 7 for j = 1 : n 8 9 adj((i-1)*n+j)=temp; end 10 11 end for i = 1 : n 12 13 for j = 1 : n if ( ( a d j ( ( i - 1 ) * n + j ) ) = = 1 ) 14 15 printf ( ” V er te x %d i s c on n ec te d t o v e r t ex %d \ n”,i,j); 16 end 17 end 18 end 19 20 e n d f u n c t i o n
89
Scilab code Exa 8.2 Finding The Number Of Paths From One Vertex-
ToOther 1 / / F i n d i n g The Number Of P a t h s From One V e r t e x To
A n ot h er Of A G iv en L e ng t h 2 3 function [ b ] = p a t h ( k , n , a d j , i , j ) 4 b=0; 5 if ( k = = 1 ) 6 b=adj((i-1)*n+j); 7 else 8 for c = 1 : n if ( a d j ( ( i - 1 ) * n + c ) = = 1 ) 9 10 b=b+path(k-1,n,adj,c,j); end 11 12 end end 13 14 printf ( ” Number o f p a t h s f ro m v e r t e x %d t o %d o f l e n g t h %d a r e %d” , i , j , k , b ) ; 15 return b ; 16 e n d f u n c t i o n 17 / / C a l l i n g R o ut i n e : 18 n = 3 ; 19 adj =[0 1 1 0 0 1 0 0 0] 20 b = p a t h ( 1 , n , a d j , 1 , 3 )
Scilab code Exa 8.3 Finding The Number Of Simple Paths From One
Point 1 / / F i n d i n g The
Number Of S i m p l e P a th s From One P o i n t To A n ot h er I n A G iv en Graph 90
2 funcprot (0) 3 function [ ] = s i m _ p a t h ( n , a d j , i , j ) ; 4 l=0; 5 m=1; 6 for m = 1 : n l=l+path(m,n,adj,i,j); 7 8 end 9 printf ( ” Ther e a r e %d s i m pl e p at hs from %d t o %d i n t he g i ve n g r a ph \ n ” , l , i , j ) ; 10 e n d f u n c t i o n 11 function [ b ] = p a t h ( k , n , a d j , i , j ) 12 b = 0 ; if ( k = = 1 ) 13 14 b=adj((i-1)*n+j); 15 else 16 for c = 1 : n if ( a d j ( ( i - 1 ) * n + c ) = = 1 ) 17 18 b=b+path(k-1,n,adj,c,j); end 19 20 end 21 end 22 return b ; 23 e n d f u n c t i o n 24 n = 3 ; 25 adj =[0 1 1 0 0 1 0 0 0]; 26 b = s i m _ p a t h ( n , a d j , 1 , 3 )
Scilab code Exa 8.4 Finding Transitive Closure
1 / / F in nd in g T r a n s i t i v e C l os u r e 2 funcprot (0) 3 function [ p a t h ] = T r a n c l o s e ( a d j , n ) ; 4 i=1,j=1; 5 p a t h = zeros ( n * n , 1 ) ; 6 path=tranclose(adj,n);
91
7 printf ( ” T r a n s i t i v e C l o s u re Of G iv en Graph i s : \ n ” ) ; for i = 1 : n 8 9 printf ( ” F o r V e r t ex %d \ n ” , i ) ; for j = 1 : n 10 11 printf ( ” %d %d i s %d\ n” , i , j , p a t h ( ( i - 1 ) * n + j ) ) ; end 12 13 end 14 15 e n d f u n c t i o n 16 function [ p a t h ] = t r a n c l o s e ( a d j , n ) 17 a d j p r o d = zeros ( n * n , 1 ) ; 18 k = 1 ; 19 n e w p r o d = zeros ( n * n , 1 ) ; 20 for i = 1 : n for j = 1 : n 21 22 path((i-1)*n+j)=adj((i-1)*n+j); a d j pr o d ( ( i - 1) * n + j ) = p a th ( ( i - 1 ) * n + j ) ; 23 24 end end 25 26 for i = 1 : n 27 newprod=prod1(adjprod ,adj,n); 28 for j = 1 : n 29 for k = 1 : n path((j-1)*n+k)=path((j-1)*n+k)|newprod((j 30 -1)*n+k); end 31 32 end for j = 1 : n 33 34 for k = 1 : n adjprod((j-1)*n+k)=newprod((j-1)*n+k); 35 36 end 37 end 38 end 39 e n d f u n c t i o n 40 function [ c ] = p r o d 1 ( a , b , n ) 41 for i = 1 : n for j = 1 : n 42 43 val=0
92
44 45 46 47 48 49 50 51 52 53 54
for k = 1 : n val=val|(a((i-1)*n+k)&b((k-1)*n+j)); end c((i-1)*n+j)=val; end end endfunction
/ / C a l l i n g R o ut i n e : n=3; adj =[0 1 0 0 0 1 0 0 0] path=Tranclose(adj,n)
Scilab code Exa 8.5 Warshalls Algorithm
1 / / W a r s h al l ’ s A l g o r i t hm 2 funcprot (0) 3 function [ p a t h ] = t r a n s c l o s e ( a d j , n ) 4 for i = 1 : n 5 for j = 1 : n 6 path((i-1)*n+j)=adj((i-1)*n+j); 7 end 8 end 9 for k = 1 : n for i = 1 : n 10 11 if ( p a t h ( ( i - 1 ) * n + k ) = = 1 ) for j = 1 : n 12 13 path((i-1)*n+j)=path((i-1)*n+j)|path((k-1) *n+j); 14 end 15 end 16 end 17 end 18 printf ( ” T r a n s i t i v e c l o s u r e f o r t h e g iv en g r a p h i s : \ n”); for i = 1 : n 19
93
20 21 22 23 24 25 26 27 28 29
printf ( ” F or v e r t e x %d \ n” , i ) ; for j = 1 : n printf ( ”%d %d i s %d\ n” , i , j , p a t h ( ( i - 1 ) * n + j ) ) ; end end endfunction
/ / C a l l i n g R o ut i n e : n=3; adj =[0 1 0 0 0 1 0 0 0] path=Tranclose(adj,n)
Scilab code Exa 8.6 Depth First Search Traversal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/ / Depth F i r s t S ea rc h T r a v er s a l funcprot (0) function [ ] = D f s ( a d j , n ) ; i=1,j=1; colour=[]; for i = 1 : n for j = 1 : n c o lo u r = [ c o l ou r ( : , : ) 0 ]; end end disp ( ” The DFS t r a v e r s a l i s ” ) ; dfs(adj ,colour ,1,n) ; endfunction function [ ] = d f s ( a d j , c o l o u r , r , n ) colour(r)=1; disp (r , ” ” ) ; for i = 1 : n if ( a d j ( ( r - 1 ) * n + i ) & ( c o l o u r ( i ) = = 0 ) ) dfs(adj,colour,i,n); end end colour(r)=2;
94
23 24 25 26 27
endfunction
/ / C a l l i n g R o ut i n e : n=4; adj =[0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0] Dfs(adj,n)
Scilab code Exa 8.7 BFS Traversal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/ / / / BFS T r a v e r s a l funcprot (0) function [ q 2 ] = p u s h ( e l e , q 1 ) if ( q 1 . r e a r = = q 1 . f r o n t ) q1.a=ele; q1.rear=q1.rear+1; else q 1 . a =[ q 1 . a ( : , :) e l e ]; q1.rear=q1.rear+1; end q2=q1; endfunction function [ e l e , q 2 ] = p o p ( q 1 ) ele=-1; q2=0; if ( q 1 . r e a r = = q 1 . f r o n t ) return ; else ele=q1.a(q1.rear-q1.front); q1.front=q1.front+1; i=1; a=q1.a(1); for i = 2 : ( q 1 . r e a r - q 1 . f r o n t ) a = [ a ( : , :) q 1 . a ( i ) ]; end q1.a=a; end
95
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
q2=q1; endfunction function [ ] = B f s ( a d j , n ) ; i=1,j=1; colour=[]; for i = 1 : n for j = 1 : n c o lo u r = [ c o l ou r ( : , : ) 0 ]; end end disp ( ” The BFS T r a v e r s a l i s ” ) ; bfs(adj ,colour ,1,n) ; endfunction function [ ] = b f s ( a d j , c o l o u r , s , n ) colour(s)=1; q = s t r u c t ( ’ r e a r ’ ,0 , ’ f r o n t ’ ,0 , ’ a ’ ,0); q=push(s,q); while ( ( q . r e a r ) - ( q . f r o n t ) > 0 ) [u,q]=pop(q); disp (u , ” ” ) ; for i = 1 : n if ( a d j ( ( u - 1 ) * n + i ) & ( c o l o u r ( i ) = = 0 ) ) colour(i)=1; q=push(i,q); end end colour(u)=2; end endfunction
/ / C a l l i n g R o ut i n e : n=4; adj =[0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0] Bfs(adj,n)
96
Scilab code Exa 8.8 Dijkstras Algorithm
1 / / D i j k s t r a s A lg o ri th m 2 funcprot (0) 3 function [ l ] = s h o r t ( a d j , w , i 1 , j 1 , n ) for i = 1 : n 4 5 for j = 1 : n if ( w ( ( i - 1 ) * n + j ) = = 0 ) 6 7 w((i-1)*n+j)=9999; 8 end 9 end 10 end 11 12 d i s t a n c e = [ ] ; 13 p e r m = [ ] ; 14 for i = 1 : n d i s ta n c e = [ d i s ta n c e ( : , :) 9 9 99 9 ] ; 15 16 p e rm = [ p e r m ( : , :) 0 ] ; end 17 18 p e r m ( i 1 ) = 1 ; 19 d i s t a n c e ( i 1 ) = 0 ; 20 c u r r e n t = i 1 ; 21 while ( c u r r e n t ~ = j 1 ) smalldist=9999; 22 23 dc=distance(current); for i = 1 : n 24 25 if ( p e r m ( i ) = = 0 ) newdist=dc+w((current -1)*n+i); 26 27 if ( n e w d i st < d i s t a n c e ( i ) ) distance(i)=newdist; 28 29 end 30 if ( d i s t a n c e ( i ) < s m a l l d i s t ) 31 smalldist=distance(i); 32 k=i; 33 end 34 end end 35 36 current=k;
97