#include <stdio.h> #include <stdlib.h> #define MAX 10 struct List { int num; int total; struct List *Next; }; typedef struct List Node; typedef Node *Link; int SearchTime = 0; int Data[2][MAX]={91,35,75,75,99,2,400,6,85,100, 141,77,150,200,19,12,1414,176,188,20}; int Search_List(int key, Link Head) { Link Pointer; Pointer = Head; while( Pointer != NULL){ SearchTime++; if( Pointer->num == key ){ printf("Data number: %d\n", Pointer->num); printf("Data total: %d\n", Pointer->total); return 1; } Pointer = Pointer->Next; } return 0; } void Free_List(Link Head) { Link Pointer; while(Head != NULL){ Pointer = Head; Head = Head->Next; free(Pointer); } } Link Create_List(Link Head) { Link New; Link Pointer; int i; Head = (Link)malloc(sizeof(Node)); if(Head == NULL) printf("Memory allocate Failure!!\n"); else{ Head->num = Data[0][0]; Head->total = Data[1][0]; Head->Next = NULL; Pointer = Head; for(i=1; i<MAX; i++){ New = (Link)malloc(sizeof(Node)); New->num = Data[0][i]; New->total = Data[1][i]; New->Next = NULL; Pointer->Next = New; Pointer = New; } } return Head; } void main() { Link Head = Create_List(Head); int number; if(Head != NULL){ printf("Input Data number:"); scanf("%d", &number); if( Search_List(number,Head)){ printf("Search time = %d\n", SearchTime); }else{ printf("NO found!!\n"); } Free_List(Head); } }
參考資料:
資料結構 黃國瑜/葉乃菁