#include <stdio.h> #include <stdlib.h> #define MAX 10 struct List { int num; struct List *Next; }; typedef struct List Node; typedef Node *Link; int Data1[MAX]={1,3,5,7,9,2,4,6,8,10}; int Data2[MAX]={11,13,15,17,19,12,14,16,18,20}; Link Concatenate_List(Link Head1,Link Head2) { Link Pointer; Link Back; Link Next; Pointer = Head1; while(Pointer->Next !=NULL){ Pointer = Pointer->Next; } Pointer->Next =Head2; return Head1; } void Print_List(Link Head) { Link Pointer; Pointer = Head; while(Pointer != NULL){ printf("[%d]",Pointer->num); Pointer=Pointer->Next; } printf("\n"); } void Free_List(Link Head) { Link Pointer; while(Head!=NULL){ Pointer = Head; Head = Head->Next; free(Pointer); } } Link Create_List(Link Head, int *Data, int Max) { Link New; Link Pointer; int i; Head = (Link)malloc(sizeof(Node)); if(Head==NULL) printf("Memory allocate Failure!!\n"); else{ Head->num = Data[0]; Head->Next = NULL; Pointer = Head; for(i=1;i<MAX;i++){ New = (Link)malloc(sizeof(Node)); New->num = Data[i]; New->Next = NULL; Pointer->Next = New; Pointer = New; } } return Head; } void main() { Link Head; Link Head1; Link Head2; Head1 = Create_List(Head,Data1,MAX); Head2 = Create_List(Head2,Data2,MAX); if(Head1 !=NULL && Head2 !=NULL){ printf("Input Data:\n"); Print_List(Head1); Print_List(Head2); Head = Concatenate_List(Head1, Head2); printf("After Concatenate\n"); Print_List(Head); Free_List(Head); } }
參考資料:
資料結構 黃國瑜/葉乃菁