Anda di halaman 1dari 33

INDEX

S NO 1 2 3 4 5 OBJECTIVE
SYNCHRONIZING POSIX THREADS USING MUTEX SYNCHRONIZING POSIX THREADS USING SEMAPHORE. THE PRAM ALGORITHM FOR PARALLEL REDUCTION PRAM ALGORITHM FOR PREORDED TREE TRAVERSAL PARALLEL MATRIX MULTIPLICATION USING ROWCOLUMN ORIENTED METHOD

SIGN.

REMARKS

6
7

THE PARALLEL QUICKSORT


IMPEMENTING THE SOLUTION OF LINEAR SYSTEMS USING GAUSSIAN ELIMINATION

8
9

IMPEMENTING THE SOLUTION OF LINEAR SYSTEMS USING JACOBI ALGORITHM PACK UPPER CASE

A program for synchronizing POSIX threads using Mutex


# include<pthread.h> # include<stdio.h>

# define NUM_THREADS 3 # define TCOUNT 10 # define COUNT_LIMIT 12 int count=0; int thread_ids[3]= {0,1,2}; pthread_mutex_t count_mutex; pthread_cond t count_threshold_cv; void *inc_count(void *idp) { int j,i; double result=0.0; int *my_id=idp; for(i=0;i<TCOUNT;i++) { pthread_mutex_lock(&count_mutex); count++; /* Check the value of count and signal waiting thread when condition is reached. note that this occurs while mutex is locked. */ if(count==COUNT_LIMIT) { pthread_cond_signal(&count_threshold_cv); printf("inc_count(): thread %d , count =%d Threshold reached. \n",*my_id,count); } printf("inc_count(): thread %d , count =%d, unlocking mutex\n",* my_id, count); ptread_mutex_unlock(&count_mutex); /* Do some work so threads can alternate on mutex lock */ for(j=0;j<1000;j++) { result=result +(double) random(); } pthread_exit(NULL); } void *watch_count(void *idp) { int *my_id= Idp; printf("Starting watch_count(): thread %d\n",*my_id); /* Lock mutex and wait for signal. note that the pthread_cond_wait routine will automatically and atomically unlock mutex whileit waits. also , note that if count_limit is reached before this routine is run by the waiting thresd, the loop will be skipped to prevent pthread_cond_wait from never returning */ pthread_mutex_lock(&count_mutex);

if(count<COUNT_LIMIT){ pthread_cond_wait (&count_threshold_cv, &count_mutex); printf(watch count thread : thread %d Condition signal received.\n, *my_id); }

pthread_mutex_unlock (&count_mutex); pthread_exit (NULL); }

int main (int argc , char *argv[]) { Int I , rc; pthread_t threads[3]; pthread_attr_t attr; /* Initialize mutex and condition variable objects */

pthread_mutex_init (&count_mutex , NULL); pthread_cond_init (&count_threshold_cv , NULL); /* For portability , explicitly create threads in a joinable state */ pthread_attr_init (&attr); pthread_attr_setdetachstate (&attr , PTHREAD_CREATE_JOINABLE ); pthread_create (&thread[0] , &attr , inc_count , (void *) &thread_ids[0]); pthread_create (&thread[1] , &attr , inc_count , (void *) &thread_ids[1]); pthread_create (&thread[2] , &attr , inc_count , (void *) &thread_ids[2]); /* Wait for threads to complete */ for (i=0; i< NUM_THREADS; i++) { pthread_join (threads[i] , NULL); } printf (Main() : Waited on %d threads. Done. \n, NUM_THREADS);

/* Clean up and exit */ pthread_attr_destroy (&attr); pthread_mutex_destroy (&count_mutex); pthread_cond_destroy (&count_threshold_cv); pthread_exit(NULL); }

2.A program for synchronization POSIX threads(PThreads) using a Semaphore.


#include<stdio.h> #include<unistd.h> #include<stdlib.h>

#include<string.h> #include<pthread.h> #include<semaphore.h>

void *thread_function(void *arg); sem_t bin_sem;

#define WORK_SIZE 1024 char work_area[WORK_SIZE]; int main() { int res; pthread_t a_thread; void *thread_result; res=sem_init(&bin_sem,0,0); if(res!=0) { perror(Semaphore initialization failed); exit(EXIT_FAILURE); } res=pthread_create(&a_thread.NULL,thread_function.NULL); if(res!=0) {perror(Thread creation failed); exit(EXIT_FAILURE); } printf(input some text .Enter endto finish\n); while(strncmp(end,work_area,3)!=0) { fgets(work_area,WORK_SIZE,stdin);

sem_post(&bin_sem); } printf(\n waiting for thread to finish.\n); res=pthread_join(a_thread,&thread_result); if(res!=0) { perror(thread join failed); exit(EXIT_FAILURE); }

printf(Thread joined\n); sem_destroy(&bin_sem); exit(EXIT_SUCCESS); }

void *thread_function(void *arg) { sem_wait(&bin_sem); while(strncmp(end,work_area,3)!=0) { printf(You input %d characters \n,strlen(work_area)-1); sem_wait(&bin_sem); } pthread_exit(NULL); }

3. A program for implementing the PRAM algorithm for parallel reduction


# include<conio.h> # include<stdio.h> # include<math.h> Void main() {

Int *a,n,i,g,cn=0,j,k; Clrscr(); Printf(enter no. Of elements\t); Scanf(%d,&n); a = (int *) malloc(2*n); printf(enter information\n); for(i=0;i<n;i++) { Scanf(%d,&[i]); } Printf(initial\n); for(i=0;i<n;i++) { Printf(%d,a[i]); } g=n; i=2; while(g>=1) { cn ++; printf(\nafter %d cycle \n,cn); j=0; while((j+(i/2))<n) { printf(%da[j]); for(k=0;k<(i/2);k++) printf( j=j+1; } );

If(j<n) Printf(%d,a[j]); g=g/2; i=i*2; } getch(); } OUTPUT: Enter no. of elements 10 Enter information 4 5 8 5 9 6 3 2 7 1 After 1 cycle 9 13 15 5 8 After 2 cycle 22 20 8

After 3 cycle 42 8

After 4 cycle 50 */

5. A program for implementating the Parallel Matrix multiplication using Row-column oriented algorithm..
#include<stdio.h #include<gmp.h> #include MatrixGMP.h #ifindef WIN32 #include<sys/time.h> #endif #include<time.h>

#include pvm3.h double sqrt(double x); void error_exit(char *message) { fprintf(stderr,Error:%s\n,message); pvm_exit(); exit(-1); } int main(int argc,cahr *argv[]) { int m,n,I,j,k,l; int tid,ptid,nprocs,*child_tids,info,numchilds; int numrows,divrest,size; // char buf[256]; char *buf; int bufid,buflength,type,source; struct timeval time_start,time_end;/*for timing*/ int dt; Matrix A,B,C,C2,My_A_Part,My_C_Part; LMatrix LA,LB; //Message Tags const int DIMENSION=1; const int MATRIX_B=2; const int SHARE=3; const int MATRIX_A_PART=4; const int RESULT=5; // initialize matrices A=Matrix_initialise(); B=Matrix_initialise(); C=Matrix_initialise();

LA=LMatrix_initialise(); LB=LMatrix_initialise(); //start pvm tid=pvm_mytid(); ptid=pvm_parent(); if(ptid==PvmNoParent) {// root process if(argc<3){ printf(\n\n Error on Program call.\n); printf(usage :%s <matrix dimension><numProcs>\n\n,argv[0]); exit(0); ) n=atoi(argv[1]);printf(matrix dimension :%2d\n,n); nprocs=atoi(argv[2]); printf(total number of processes: %2d\n,nprocs); if(nprocs>n) {printf?(\n WARNING:only %s processes will be created!,argv[1]); nprocs=n; } if(n<=0)||(nprocs<=0))error_exit(negative parameters); //create nprocs-1 additional processes numchilds=nprocs-1; printf(Newly created proceses :%2d\n,numchilds); child_tids=(int *)malloc(numchilds*sizeof(int); info=pvm_spawn(argv[0],(char **)0,PvmTaskDefault,,numchilds,child_tids); if(info!=numchilds)error_exit(process creation not successful\n); //Allocate storage for matrices A=Matrix_create(n,n); B=Matrix_create(n,n);

C=Matrix_create(n,n); LA=LMatrix_create(n,n);> LB=LMatrix_create(n,n); //Fill mtrices A and B with random numbers Matrix_fill(&A); Matrix_fill(&B); //time_start gettimeofday(&time_start,(struct timezone*)0); //convert input matrices to long int LA=Matrix_convert_to_long(A); LB= Matrix_convert_to_long(B); //broadcast matrix B pvm_initsend(PvmDataDefault); pvm_pkint(&n,1,1); pvm_mcast(child_tids,numchilds,MATRIX_B); //tell each worker its number of rows and its part of matrix A numrows=n/nprocs; divrest=n%nprocs; J=0; for(i=0;i<numchilds;i++) { int size=(divrest0)?numrows+1:numrows; pvm_initsend(PvmDataDefault); pvm_pkint(&size,1,1); pvm_send(child_tids[i],SHARE); pvm_initsend(PvmDataDefault); pvm_pklong( (LA.row)[j], size*n, 1); pvm_send(child_tids[i], MATRIX_A_PART); J +=size; }

// do own part of work My_A_Part=Matrix_initialise(); My_A_Part=Matrix_create(numrows,n); // copy my A-part for (i=0;i<numrows*n;i++) { mpz_init(C.data[i]); mpz_set(C.data[i], My_C_Part.data[i-j*n]); } Matrix_clear(&My_A_Part); Matrix_clear(&my_C_Part); //receive results from child processes J=0; divrest = n%nprocs; for( i=0; i<numchilds; i++) { int size = ( divrest 0) ? numrows+1: numrows; bufid=pvm_recv( child_tids[i], RESULT); pvm_bufinfo( bufid, &buflength, &type, &source); buf = (char*)malloc(buflength/n); for(k=j; k<size; k++) { for (l=0; l<n; l++) { pvm_upkstr(buf); mpz_init(C.row[k][1]); mpz_set_str( C.row[k][1],buf,10); } } J +=size; } // time_end gettimeofday(&time_end, (struct timezone*)0);

dt = (time_end.tv_sec time_start.tv_sec)+1000000 + printf(Runtime for matrix with dimension [%4d] on [ %2d] processor(s): [%8d] usec.\n,n, nprocs, dt); if(0) { printf (\nMatrix A\n); Matrix_output(A); printf(\nMatrix B\n); Matrix_output(B); } If(0) { Printf(\nResult matrix C\n); Matrix_output(C); } // correctness check If(0) { C2= Matrix_initialise(); C2= Matrix_create(n, n); C2= Matrix_multiply(A, B); printf(\nControl Matrix C2\n); Matrix_output(c2); If (Matrix_compare(C,C2)) { printf(\n!!!Result ok !!! \n); } else { printf(\n!!!>>>>>RESULT WRONG<<<<<<<<<<!!!\n); } } } Else { // worker process //receive dimension n pvm_recv(ptid, DIMENSION); pvm_upkint(&n, 1, 1);

//create , receive and convert matrix B B= Matrix_create(n, n); LB=L Matrix_create(n, n); pvm_recv(ptid, SHARE); pvm_upkint(&numrows, 1, 1); //create receive and convert part of matrix A A = Matrix_create(numrows ,n); LA =LMatrix_create(numrows ,n); pvm_recv(ptid, MATRIX_A_PART); pvm_upklong(LA.data, n*numrows, 1); Matrix_convert_from_long(&A,LA); //multiply C = Matrix_multiply(A,B); // send result pvm_initsend(PvmDataDefault); // convert result matrix elements into strings and //pack them into single buffer for(k=0; k<numrows*n;k++) { buf = (char*)malloc (mpz_sizeinbase(C.data[k],10)+2); mpz_get_str(buf,10,C.data[k]); pvm_pkstr(buf); free(buf); } pvm_send(ptid,RESULT); } //end worker //Deallocate Storage Matrix_clear(&A); Matrix_clear(&B);

Matrix_clear(&C); LMatrix_clear(&LA); LMatrix_clear(&LB); //abmelden bei pvm pvm_exit(); Matrix_output(c2); If (Matrix_compare(C,C2)) { Printf(\n!!!Result ok !!! \n); } else { printf(\n!!!>>>>>RESULT WRONG<<<<<<<<<<!!!\n); } } } Else { // worker process //receive dimension n pvm_recv(ptid, DIMENSION); pvm_upkint(&n, 1, 1); //create , receive and convert matrix B B= Matrix_create(n, n); LB=L Matrix_create(n, n); pvm_recv(ptid, SHARE); pvm_upkint(&numrows, 1, 1); //create receive and convert part of matrix A A = Matrix_create(numrows ,n); LA =LMatrix_create(numrows ,n); pvm_recv(ptid, MATRIX_A_PART); pvm_upklong(LA.data, n*numrows, 1); Matrix_convert_from_long(&A,LA); //multiply

C = Matrix_multiply(A,B); // send result pvm_initsend(PvmDataDefault); // convert result matrix elements into strings and //pack them into single buffer for(k=0; k<numrows*n;k++) { buf = (char*)malloc (mpz_sizeinbase(C.data[k],10)+2); mpz_get_str(buf,10,C.data[k]); pvm_pkstr(buf); free(buf); } pvm_send(ptid,RESULT); } //end worker //Deallocate Storage Matrix_clear(&A); Matrix_clear(&B); Matrix_clear(&C); LMatrix_clear(&LA); LMatrix_clear(&LB); //abmelden bei pvm pvm_exit();

6. A program for implementing the Parallel Quick Sort.


#define TRY(aaa,bbb){bbb=aaa;\ If(bbb=-1){\ printf(whoops at %d : %d\n,_LINE_,errno;\ exit(1);} } #include <thread.h> #include <errno.h> #define FAIL 1 #define MINSIZE 20 #define PASS 0 #define size 100 #define SIZEBY4 /************ *Quicksort driver: initialize,sort, and verify. */ SIZE/4 /*verification failed*/ /*Cut-over size for bubble-sort*/ /*verification passed*/ /*size of array to sort*/ /*quarter size of array*/

main () { int a[size]; int i; int check=PASS; int ncpus; ncpus=synconf(_SC_NPROCESSOR_ONLN); printf(ncpus: %d\n,ncpus); thr_setconcurrency(ncpus); /*Initialize sort array to random numbers*/ for (i=0;i<SIZE;i++){ a[i]=(int)rand(); } /* Quicksort the array*/ Qsortr(a,o,SIZE-1); /*Verify the results*/ for(i=1;i<SIZE;i++) if(a[i]<a[i-1]){ printf(INVALID SORT: a[%d]=%d,a[%d]=%d\n,i-1,a[i-1],i,a[i]); check =FAIL; } } printf(/n Sorted array is :\n); for(i=0;i<SIZEBY4;i++){ printf( %13d %13d %13d %13d\n,a[i],a[i+SIZEBY4],a[i+(2*SIZEBY4),a[i+(3*SIZEBY4)]); } If(check==PASS){ printf(\nqsort worked.\n); }

else{ printf(\nqsort test failed!\n); exit(1); } } /********** *Quicksort routine; parallel and recursive. */ qsort(d,first,last) int *d; int first,last; { int lastpos; int temp; int swap; int pivot; int lesspos; int morepos; int I; thread_t pt; void * stat; /* *Once we get to a subrange of MIZSIZE elements,use a bubble sort. */ if ((last-first+1)<MINSIZE){ swap=1; while (swap){ swap=0; lastpos=last; /*array to be sorted*/ /*element range to sort*/

for(i=first;i<lastpos;i++){ if(d[i]>d[i+1]){ temp=d[i]; d[i]=d[i+1}; d[i+1]=temp; swap=1; } } Lastpos=lastpos-1; } } /* *Otherwise,quicksort the element range */ else{ /* Position pivot element such that element before are less than and elements after are greater than. */ pivot=d[(first+last)/2]; lespos=last; morepos=first; do{ while {pivot<d[lesspos])lespos--; while(d[morepos]<pivot)morepos++; if(morepos<lesspos){ temp=d[morepos] d[morepos]=d[lesspos]; d[lesspos]=temp; lesspos--; morepos++;

} }while morepos<lesspos); /* If the less-than list is larger than two elements , hand it off to be sorted by a new thread . Then recursively sort the greater-than list ( if larger than two elements). */ If(first<lesspos){ thread_qsort(&pt,d,first,lesspos); } If(morepos<last) { qsort(d,morepos,last); } /* wait for the thread sorting the less-than list */ TRY(thr_join(pt,0,&stat),i); } } /************ Start procedure for a thread running quick sort . Get the arguments passed to the thread and pass them on to the quick sort routine . */ Void * start(ar) Void * ar; { Int a,b,c; Int *args; /* *Get the arguments passed to the thread and *free up the space used to pass arguments. */

args=(int*)ar; a=args[0]; b=args[1]; c=args[2]; free(args); /*quick sort using the arguments */ qsortr(a,b,c); /*Return successful completion status*/ Return((void*)0); } /* Spawn a thread to QuickSort an array & return a handle to a spawned thread. */ thread_qsort(p,a,b,c) thread_t * p; int * a,b,c; { Int * args; Int rcl; /* Allocate area to pass qsortr arguments in */ args=(int*)malloc(8*3); args[0]=(int)a; args[1]=b; args[2]=c; /* Spawn the thread */ TRY(thr_create(0,0,start,(void*)args,0,p),rcl); }

7. A program for implementing the solution of linear systems using Gaussian elimination
#include<stdio.h> #include<stdlib.h> int n; float a[10][11]; void forwardsubstitution(){ int I,j,k,max; float t; for(i=0;i<n;++i){ max = i; for(j=i+1;j<n;++j) if(a[j][i]>a[max][i]) max=j; for(j=0;j<n+1;++j){ t=a[max][j]; a[max][j]=a[i][j]; a[i][j]=t; } for(j=n;j>=I;--j) for(k=i+1;k<n;++k) a[k][j]-=a[k][i]/a[i][i]*a[i][j];

} } void reverseelimination(){ int i,j; for(i=n+1;i>=0;--i){ a[i][n]=a[i][n]/a[i][i]; a[i][i]=1; for(j=i-1;j>=0;--j){ a[j][n]-=a[j][i]*a[i][n]; a[j][i]=0; }}} void gauss(){ int i,j; forwardsubstitution(); reverseelimination(); for(i=0;i<n;++i){ for(j=0;j<n+1;++j) printf(2f\t,a[i][j]); printf(\n);}} int main(int argc,char*argv[]) { int I,j; FILE *fin=fopen(gauss.in,r); fscanf(fin,%d,&n); for(i=0;i<n;++i) for(j=0;j<n+1;++j) fscanf(fin,%f,&a[i][j]); fclose(fin); gauss(); return 0;

} Here is the input file for the example(gauss.in)(save it as gauss.in) 3 2 -3 -2 1 -1 1 1 2 2 8 -11 -3

8. A program for implementing the solution of linear systems using jacobian algorithm
/*must link the resulting object file with the libraries: -lf2c lm */ #include f2c.h /*table of constant values*/ Static doublereal c_b2=1.; Static integer c__1=1; Static doublereal c_b13=-1; Int Jacobi_(n,b,x,work,ldw,iter,resid,matvec,info) Integer *n,*ldw,*iter,*info; Doublereal *b,*x,*work,*resid; Int (*matvec)(); { /*system generated locals*/ Integer work_diml,work_offset,i__1; /*local variables*/ Static integer temp; Extern/*subroutine*/ int matsplit_(); Extern doublerealdnrm2_(); Static integer I; Extern/*subroutine*/int dcopy_(); (in that order)

Static integer maxit; Extern/*subroutine*/int daxpy_(); Static integer x1,mm; Static doublereal tol; /*executable statements.*/ /*parameter adjustments*/ Work_diml=*ldw; Work_offset=work_diml+1; Work-=work_offset; --x; --b; /*function body*/ *info=0; /*test the input parameters*/ If(*n<0){ *info=1; } Elseif(*ldw<max(l,*n)){ *info=-2; }elseif(*iter<=0){ *info=3; } If(*info!=0){ Return 0; } Maxit=*iter; Tol=*resid;
/* Alias workspace columns. */ mm=1;

x1=2; temp=3; *iter=0; /*Form matrix splitting inv(m) and N */ matsplit_(&c, b2, &b[1], &work[mm*work_dim1+1}, 1dw, "JACOBI", "SPLIT", 6L, 5L); L10: /*Perform Jacobi iteration */ ++(*iter); /* Save the current approximation to Xin X1. */ dcopy_(n, &x[1], &c_1, &work[x]*work_dim1+1], &c_1); /* Apply iteration ; result is updated approximation vector x. */ 'dcopy_(n, &b[1], &c_1, &work[temp * work_dim1+1], &c_1); (*matvec)(&c_b2, &x[1], &c_b2, &work[temp*work_dim1+1]); i_1=*n; for(i=1;i<=i_1;++i) { x[i]=work[i +mm*work_dim1]* work[i+temp *work_dim1]; } /* Compute error and check for acceptable convergence */ daxpy_(n, &c_b13, &c_1, &work[x1 *work_dim1+1], &c_1); *resid= dnrm2_(n, &work[x1* work_dim1+1], &c_1)/dnrm2_(n, &x[1], &c_1); if(*resid<=tol) { goto L30; } if(*iter==maxit) { gotoL20; } gotoL10; L20: /* iteration fails */ *info=1; goto L30; L30: /* Iteration succesful, reconstruct matrix a. */ matsplit_(&c_b2, &b[1], &work[mm *work_dim1+1], 1dw, "JACOBI","RECON\ STRUCT", 6L,11L); return 0; /*End of Jacobi */

9-A program for Pack Upper Case


#include<conio.h> #include<stdio.h> #include<math.h> void main() { Int *b,n,I,g,k,val; char *a,*c; clrscr(); printf(enter no. of characters\t); scanf(%d,&n); a=(char*)malloc(sizeof(char)*(n+1)); b=(char*)malloc(sizeof(char)*n); c=(int*)malloc(2*n); printf(enter information\n); i=0; while (i<=n) { scanf(%c, &a[i]); i=i+1; } I=0; for (i=0; i<n; i++) a[i] = a[i+1]; printf(initial\n);

for (i=0; i<n; i++) { printf(%c , a[i]); val=a[i]; if(val>=65 && val<=90) b[i]=1; else b[i]=0; } for (i=1; i<n; i++) { b[i]=b[i]+b[i-1]; } k=0; g= b[0]; if (g==1) c[k++]= a[0]; for (i=1; i<n; i++) { if (g!=b[i]) { c[k++]=a[i]; g=b[i]; }}

printf(\npack upper case.\n); for (i=1; i<k; i++) printf(%c,c[i]); getch(); } OUTPUT Enter no. of characters: Enter information: Initial: 10 AbCDEfgHiJ AbCDEfgHiJ

Pack upper case:

ECDEHJ

4. A program for the implementation of PRAM algorithm for Preorder Tree Traversal
#include<conio.h> #include<stdio.h> #include<alloc.h> char charat(char [],char[],char); int index(char[],char); struct nod { char ii; char jj; int wt; int rank; struct nod *next; }; typedef struct nod NODE; void main() { int *b,n,i,g,k,val,flag=1,count=0,j; NODE *start,*p,*q; char parent[8]={'n','a','a','b','b','c','e','e'}; char sibling[8]={'n','c','n','e','n','n','h','n'}; char child[8]={'b','d','f','n','g','n','n','n'}; char node[8]={'a','b','c','d','e','f','g','h'}; int preorder[8]; clrscr(); start=(NODE*)malloc(sizeof(NODE));

start->ii=node[0]; start->jj=child[0]; p=(NODE*)malloc(sizeof(NODE)); p=start; while(flag==1) { if(charat(parent,node,p->ii)==p->jj) { p->wt=0; p->rank=0; if(charat(sibling,node,p->ii)!='n') { q=(NODE*)malloc(sizeof(NODE)); q->ii=p->jj; q->jj=charat(sibling,node,p->ii); q->next=null; p->next=q; p=p->next; } else if(charat(parent,node,p->jj)!='n') { q=(NODE*)malloc(sizeof(NODE)); q->ii=p->jj; q->jj=charat(parent,node,p->ii); q->next=null; p->next=q; p=p->next; } else

{ q=(NODE*)malloc(sizeof(NODE)); q->ii=p->ii; q->jj=p->jj; q->next=null; p->next=q; p=p->next; preorder[index(node,p->jj)]=1; flag=0; } } else { p->wt=1; p->rank=1; if(charat(child,node,p->jj)!='n') { q=(NODE*)malloc(sizeof(NODE)); q->ii=p->jj; q->jj=charat(child,node,p->jj); q->next=null; p->next=q; p=p->next; } else { q=(NODE*)malloc(sizeof(NODE)); q->ii=p->jj; q->jj=p->ii;

q->next=null; p->next=q; p=p->next; } } } printf("list of successor..\n"); q=start; while(q->next!=NULL) { printf("%c %c\t",q->ii,q->jj); q=q->next; count++ } i=0; p=start; for(i=0;i<count-1,i++) { for(j=0;j<count-1,j++) q=p; p=p->next; } q->rank=q->rank+p->rank; p=start; } p=start; while(p->next!=NULL) { if(charat(parent,node,p->jj)==p->ii)

preorder[index(node,p->jj)]=8+1-p->rank; p=p->next; } printf("\nPreorder...\n"); for(i=0;i<8;i++) { printf("%c %d \n",node[i],preorder[i]); } getch(); } char charat(char n1[],char n2[],char a) { int in,i,j; for(i=0;i<8;i++) { if(n2[i]==a) in=i; } return n1[in]; } int index(char n1[],char a) { int i;

for(i=0;i<8;i++) { if(n1[i]==a)

return i; }}

Anda mungkin juga menyukai