高级数据表示

抽象数据类型(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 }

 

 1 #include <stdio.h>
 2 
 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]# ./test
20092192
2008
[root@playstation basic]# 

 *ps被当做const,并不意味着*ps指向的数据是const,这是因为上面的代码并没有改变*ps,它只是改变了*ps所指向的数据。也就是说,你不要指望const能够捕获到意外修改数据的程序错误。

 

转载于:https://www.cnblogs.com/itpoorman/p/4002282.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值