#include <stdio.h> #include
#include typedef struct pp { float heso; int bac; struct pp *tiep; } Dathuc; void void void void
Boxung( Dathuc **pdau, Dathuc *pnew ); Nhap( Dathuc **pdau ); CongDathuc( Dathuc **R, Dathuc *P, Dathuc *Q ); InDathuc( Dathuc *pdau );
void Boxung( Dathuc **pdau, Dathuc *pnew ) { Dathuc *p=*pdau; if ( !p ) *pdau = pnew; else { while ( p->tiep ) p = p->tiep; p->tiep = pnew; } } void Nhap( Dathuc **pdau ) { int n, i; Dathuc *pnew; float heso; int bac; *pdau = NULL; printf( "\nNhap so he so khac 0: " ); scanf( "%d", &n ); for ( i=0; iheso = heso; pnew->bac = bac; pnew->tiep = NULL; Boxung( pdau, pnew ); } } void CongDathuc( Dathuc **R, Dathuc *P, Dathuc *Q ) { Dathuc *p=P, *q=Q, *pnew; *R = NULL; while ( p || q ) { pnew = (Dathuc*)malloc( sizeof(Dathuc) ); if ( p ) { if ( q ) { if ( p->bac > q->bac ) { *pnew = *p; pnew->tiep = NULL; Boxung( R, pnew ); p = p->tiep; } else if ( p->bac < q->bac ) { *pnew = *q; pnew->tiep = NULL;
Boxung( R, pnew ); q = q->tiep;
} else { pnew->heso = p->heso+q->heso; pnew->bac = p>bac;
pnew->tiep = NULL; Boxung( R, pnew ); p = p->tiep; q = q->tiep; } } // if ( q ) else { *pnew = *p; pnew->tiep = NULL; Boxung( R, pnew ); p = p->tiep; }
}
} else { *pnew = *q; pnew->tiep = NULL; Boxung( R, pnew ); q = q->tiep; }
} void InDathuc( Dathuc *pdau ) { Dathuc *p=pdau; printf( "\n" ); while ( p ) { if ( p!=pdau ) printf( "-->" ); printf( "(%f,%d)", p->heso, p->bac ); p = p->tiep; } } void main() { Dathuc *P, *Q, *R; clrscr(); Nhap( &P ); InDathuc( P ); Nhap( &Q ); InDathuc( Q ); CongDathuc( &R, P, Q ); InDathuc( R ); getch(); }