结构体和数据结构学习

这篇博客介绍了如何使用C语言创建和管理链表数据结构。博主通过定义一个包含电影标题和评分的结构体,展示了如何动态分配内存,构建链表,并遍历打印链表中的电影信息。然而,在释放链表内存时遇到了问题,最后的释放内存代码可能存在错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "stdafx.h"  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <windows.h>  
  6. #define TSIZE 45  
  7.   
  8. struct film  
  9. {  
  10.     char title[TSIZE];  
  11.     int rating;  
  12.     struct film * next;  
  13. };  
  14.   
  15. int _tmain(int argc, _TCHAR* argv[])  
  16. {  
  17.     struct film *head=NULL;  
  18.     struct film *prev, *current;  
  19.     char input[TSIZE];  
  20.     puts("Enter first movie title:");  
  21.     while (gets(input) != NULL&&input[0] != '\0')  
  22.     {  
  23.         current = (struct film*) malloc(sizeof(struct film));  
  24.         if (head == NULL)  
  25.             head = current;  
  26.         else  
  27.         {  
  28.             prev->next = current;  
  29.         }  
  30.               
  31.         current->next = NULL;  
  32.         strcpy(current->title, input);  
  33.         puts("Enter your rating <0-10>:");  
  34.         scanf_s("%d", &currentt->rating);  
  35.         while (getchar() != '\n')  
  36.             continue;  
  37.         puts("Enter next movie title(empty line to stop):");  
  38.         prev = current;  
  39.   
  40.     }  
  41.     if (head == NULL)  
  42.         printf("No data entered.");  
  43.     else  
  44.         printf("Here is the movie list :\n");  
  45.     current = head;  
  46.     while (current != NULL)  
  47.     {  
  48.         printf("Movie : %s rating: %d \n", current->title, current->rating);  
  49.         current = current->next;  
  50.     }  
  51.   
  52.     current = head;  
  53.     while ((current!=NULL))  
  54.     {  
  55.         free(current);  
  56.         current = current->next;  
  57.     }  
  58.     printf("Bye\n");  
  59.     system("pause");  
  60.     return 0;  
  61. }  

课本源码,但是在释放内存的时候出错了。最后一个while()写法有问题正在求证中。

本例的关键点:

1.结构体的定义

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. struct film  
  2. {  
  3.     char title[TSIZE];  
  4.     int rating;  
  5.     struct film * next;  
  6. };  
关系到里面的数据类型,next链接到下一个数据

需要申请空间,用于进入的数据放到内存中,head是表头,用prev和current来表示整个链表,prev是前一个结构体,current是当前jieg,当前的结构体中需要有数据,此时将现在的结构体的next赋值为NULL(以后遍历的时候,只需要一个while !NULL 从表头遍历到尾部)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. while (gets(input) != NULL&&input[0] != '\0')  
  2.     {  
  3.         current = (struct film*) malloc(sizeof(struct film));  
  4.         if (head == NULL)  
  5.             head = current;  
  6.         else  
  7.         {  
  8.             prev->next = current;  
  9.         }  
  10.               
  11.         current->next = NULL;  
  12.         strcpy(current->title, input);  
  13.         puts("Enter your rating <0-10>:");  
  14.         scanf_s("%d", &currentt->rating);  
  15.         while (getchar() != '\n')  
  16.             continue;  
  17.         puts("Enter next movie title(empty line to stop):");  
  18.         prev = current;  
  19.   
  20.     }  

一下的片段是遍历链表

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. if (head == NULL)  
  2.     printf("No data entered.");  
  3. else  
  4.     printf("Here is the movie list :\n");  
  5. current = head;  
  6. while (current != NULL)  
  7. {  
  8.     printf("Movie : %s rating: %d \n", current->title, current->rating);  
  9.     current = current->next;  
  10. }  
找到表头,然后进行遍历

最后

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. current = head;  
  2. while ((current!=NULL))  
  3. {  
  4.     free(current);  
  5.     current = current->next;  
  6.     if(current=NULL) break;
  7. }  
释放内存的时候出错了,正在求解,
此时发现在最后一个while有问题,可以通过分步执行观察next的值得变化,其实最后在next空的时候跳出while就不会报错了。
也就是
  1. while ((current!=NULL))  
  2. {  
  3.     free(current);  
  4.     current = current->next;  
  5. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值