Chain List Addition
/************************************************************************* > File name:list.h > Aut Hor:zhoulin > Mail: [email protected] > Created time:sat April 10:58:58 AM CST *************** *********************************************************/#ifndef _list_h//Define a node of listtypedefstruct_basenode{intv; struct_basenode *Next;} Basenode;basenode*listadd (Basenode *p1,basenode *p2); Basenode*listinsert (Basenode *b,intv);voidListfree (Basenode *p);#define_list_h#endif
LISTC:
/************************************************************************* > File name:list.c > Aut Hor:zhoulin > Mail: [email protected] > Created time:sat April 11:04:56 AM CST *************** *********************************************************/#include"list.h"#include<stdio.h>#include<stdlib.h>#include<string.h>#defineSize 64voidLISTPRT (Basenode *2) { while(P! =NULL) { if(P->next = =NULL) {fprintf (stdout,"%d\n",p->v); Break; } fprintf (stdout,"%d",p->v); P= p->Next; }}basenode*listinsert (Basenode *b,intv) { CharPbuf[size] = {' /'}; sprintf (Pbuf,"%d", V); intLen =strlen (PBUF), I; for(i =0; I < len;i++) { Chartbuf[2] = {' /'}; strncpy (Tbuf,pbuf+i,1); if(b = =NULL) {b= (Basenode *)malloc(sizeof(*b)); b->next =NULL; } Else{Basenode*cur = (Basenode *)malloc(sizeof(*b)); Cur->next =NULL; Cur->next =b; b=cur; } b->v =atoi (TBUF); } returnb;}//In three different situations//1. Link balance, P1.len = P2.len//2. Linked list left-leaning, P1.len > P2.len//3. Linked list right-leaning, P1.len < P2.len//P1 = 1->2->3, representing 321;//P2 = 4->9->0->3, representing 3094//output: P3 = 5->1->4->3,3451Basenode *listadd (Basenode *p1,basenode *p2) {Basenode*PH1 =P1; Basenode*PH2 =P2; Basenode*head =NULL; Basenode*tail =NULL; Basenode*PH =NULL; Basenode*FN =NULL; intmode =0, flag =0, sum =0; while(PH1! = NULL && PH2! =NULL) { if(ph = =NULL) {ph= (Basenode *)malloc(sizeof(*ph)); ph->next =NULL; Tail= Head =ph; }Else{Basenode*cur = (Basenode *)malloc(sizeof(*cur)); Cur->next =NULL; ph->next =cur; ph=cur; Tail=cur; } Sum= Ph1->v + ph2->v+Flag; if(Sum >=Ten) {mode= (sum)%Ten; Flag=1; }Else{mode=sum; Flag=0; } ph->v =mode; PH1= ph1->Next; PH2= ph2->Next; } if(PH1! =NULL) {fn=PH1; } if(PH2! =NULL) {fn=PH2; } while(fn! =NULL) {Basenode*t = (Basenode *)malloc(sizeof(*tail)); Sum= Fn->v +Flag; if(Sum >=Ten) {mode= sum%Ten; Flag=1; }Else{mode=sum; Flag=0; } t->v =mode; Tail->next =T; Tail=T; FN= fn->Next; } if(Flag = =1) {Basenode*t = (Basenode *)malloc(sizeof(*tail)); T->v = flag+mode; Tail->next =T; Tail=T; } returnhead;}//Destroy linked listvoidListfree (Basenode *p) { while(p!=NULL) {Basenode*n = p->Next; Free(P); P=N; } P=NULL;}intMainintargcChar*args[]) { if(ARGC! =3) {printf ("usage:\n"); printf ("%s P1 p2\n", args[0]); return-1; } intV1 = atoi (args[1]); intV2 = atoi (args[2]); fprintf (stdout,"********v1 =%d, v2 =%d********\n", V1,V2); Basenode*P1 =Listinsert (NULL,V1); Basenode*P2 =Listinsert (NULL,V2); Basenode*P3 =Listadd (P1,P2); printf ("\ n---------p1--------------\ n"); Listprt (p1); printf ("\ n---------p2--------------\ n"); Listprt (p2); printf ("\ n---------add p1 and p2 equals p3--------------\ n"); LISTPRT (p3); Listfree (p1); Listfree (p2); Listfree (p3); return 0;}
Operation Result:
[Email protected]:~/code/c_src/algorithm/list_add:./list22211 99999V1 =22211, V2 =99999---------P1--------------1-1-2-2-2---------P2--------------9-9-9-9-9---------add p1 and P2 equals p3--------------0-1-2-2-2-3
Algorithm. List Link List addition