首页 > 代码库 > 高级数据表示

高级数据表示

抽象数据类型(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         else26             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", &current->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     else40         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 }

 

高级数据表示