码迷,mamicode.com
首页 > 其他好文 > 详细

高级数据表示

时间:2014-09-30 17:59:09      阅读:189      评论:0      收藏:0      [点我收藏+]

标签: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", &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     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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!