抽象数据类型(ADT)
链表
1 #include2 #include 3 #include 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", ¤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 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 }
1 #include2 3 struct student 4 { 5 int id; 6 char name[32]; 7 }; 8 9 typedef struct student * Node;10 11 void modi(const Node * ps)12 {13 (*ps)->id = 2008;14 }15 16 int main(void)17 {18 struct student stu = { 20092192, "fwy"};19 struct student * ps = &stu;20 21 printf("%d\n", stu.id);22 modi(&ps);23 printf("%d\n", stu.id);24 }
[root@playstation basic]# ./test200921922008[root@playstation basic]#
*ps被当做const,并不意味着*ps指向的数据是const,这是因为上面的代码并没有改变*ps,它只是改变了*ps所指向的数据。也就是说,你不要指望const能够捕获到意外修改数据的程序错误。