链表节点类型如下: typedef char datatype; struct node { datatype data; struct node *next; };
编写程序实现单向链表的建立、读取和删除函数,在主函数中选择执行。
输入用例:
5abcde
输出用例:
The content of list:abcde
The content of list:null
![](https://profile-avatar.csdnimg.cn/default.jpg!4)
编写程序实现单向链表的建立、读取和删除函数(语言-c语言)
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- CSDN专家-link 2022-04-22 09:31关注
读取就是链表内容显示?
#include <stdio.h> typedef char datatype; struct node { datatype data; struct node *next; }; struct node * listCreate(int n) { struct node * head, *p,*q; int i; head = (struct node*) malloc(sizeof(struct node)); head->next = NULL; p = head; for(i=0;i<n;i++) { q = (struct node*) malloc(sizeof(struct node)); scanf("%c",&q->data); q->next = NULL; p->next = q; p = q; } return head; } void showNode(struct node *head) { printf("The content of list:"); if(head == NULL || head->next == NULL) { printf("NULL\n"); return; } while(head->next != NULL) { printf("%c",head->next->data); head = head->next; } printf("\n"); } void clearList(struct node *head) { struct node *p = head,*q; while(p->next != NULL) { q = p->next; p->next = p->next->next; free(q); } } int main() { int n; struct node * head; scanf("%d",&n); head = listCreate(n); showNode(head); clearList(head); showNode(head); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 3无用
悬赏问题
- ¥15 在若依框架下实现人脸识别
- ¥15 网络科学导论,网络控制
- ¥100 安卓tv程序连接SQLSERVER2008问题
- ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
- ¥15 metadata提取的PDF元数据,如何转换为一个Excel
- ¥15 关于arduino编程toCharArray()函数的使用
- ¥100 vc++混合CEF采用CLR方式编译报错
- ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
- ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
- ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同