标签:style blog color io ar 数据 sp div c
抽象数据类型(ADT)
链表
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define TSIZE 45 5 6 struct film 7 { 8 char title[TSIZE]; 9 int rating; 10 struct film * next; 11 }; 12 13 int main(void) 14 { 15 struct film * head = NULL; 16 struct film * prev, * current; 17 char input[TSIZE]; 18 19 puts("Enter first movie title: "); 20 while(NULL != gets(input) && ‘\0‘ != input[0]) 21 { 22 current = (struct film *)malloc(sizeof(struct film)); 23 if(NULL == head) 24 head = current; 25 else 26 prev->next = current; 27 current->next = NULL; 28 strncpy(current->title, input, TSIZE); 29 current->title[TSIZE - 1] = ‘\0‘; 30 puts("Enter your rating <0-10>: "); 31 scanf("%d", ¤t->rating); 32 while(‘\n‘ != getchar()) 33 continue; 34 puts("Enter next movie title (empty line to stop): "); 35 prev = current; 36 } 37 if(NULL == head) 38 printf("No data entered. "); 39 else 40 printf("Here is the movie list: \n"); 41 current = head; 42 while(NULL != current) 43 { 44 printf("Movie: %s Rating: %d\n", current->title, current->rating); 45 current = current->next; 46 } 47 current = head; 48 while(NULL != current) 49 { 50 free(current); 51 current = current->next; 52 } 53 printf("Bye!\n"); 54 55 return 0; 56 }
标签:style blog color io ar 数据 sp div c
原文地址:http://www.cnblogs.com/itpoorman/p/4002282.html