当前位置:网站首页>6-8 创建、遍历链表
6-8 创建、遍历链表
2022-06-11 17:17:00 【西红柿_码农】
本题目要求实现下面三个函数,实现如下功能:
输入若干个正整数,以-1结束,采取向链表中添加节点的方式来建立一个单向链表。 遍历并输出这个单向链表。
函数接口定义:
向链表尾部添加节点函数:
struct link *AppendNode(struct link *head,int data);
head:链表头节点指针,如果head为NULL,则会新建头节点
data:要添加的节点data数值
函数返回:添加过节点后的链表头节点指针遍历显示链表函数:
void DisplyNode(struct link *head);
head :链表头节点指针裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
struct link
{
int data;
struct link *next;
};
struct link *AppendNode(struct link *head,int data);
void DisplyNode(struct link *head);
void DeleteMemory(struct link *head);
int main()
{
char c;
int data = 0;
struct link *head = NULL; /* 链表头指针 */
while (1)
{
scanf("%d",&data);
if (data==-1)
break;
head = AppendNode(head,data);/* 向head为头指针的链表末尾添加节点 */
}
DisplyNode(head); /* 显示当前链表中的各节点信息 */
DeleteMemory(head); /* 释放所有动态分配的内存 */
return 0;
}
/*在此实现 void DeleteMemory(struct link *head);*/
void DeleteMemory(struct link *head)
{
struct link *p, *pnext;
p = head;
while (p != NULL)
{
pnext = p->next;
free(p);
p = pnext;
}
}
/*在此实现 struct link *AppendNode(struct link *head,int data); */
/*在此实现 void DisplyNode(struct link *head); */
/* 请在这里填写答案 */输入格式:
一系列正整数,以-1结束
输出格式:
输出单向链表中的每一项,每个数之间以->连接
输入样例:
在这里给出一组输入。例如:
1 3 5 7 9 -1
输出样例:
在这里给出相应的输出。例如:
1->3->5->7->9struct link *AppendNode(struct link *head,int data)
{
struct link *p=NULL, *q=head;
p=(struct link*)malloc(sizeof(struct link));
if(p==NULL){
exit(0);
}
if(head==NULL){
head=p;
}
else{
while(q->next!=NULL){
q=q->next;
}
q->next=p;
}
p->data=data;
p->next=NULL;
return head;
}
void DisplyNode(struct link *head)
{
struct link*p;
p=head;
int i=1;
while(p!=NULL)
{
if(i==1){
printf("%d",p->data);
}else{
printf("->%d",p->data);
}
i++;
p=p->next;
}
}边栏推荐
- 拜登下令强制推行零信任架构
- Typescript learning notes (II)
- 字符串转数值
- vscode配置eslint自动格式化报错“The setting is deprecated. Use editor.codeActionsOnSave instead with a source“
- error:指针作为函数参数错误总结
- 04_特征工程—特征选择
- Learning C language from scratch day 039
- CentOS7服务器配置(四)---安装redis
- Is the second-class cost engineer worth the exam? What is the development prospect?
- 信息安全数学基础 Chapter 2——同余
猜你喜欢

Biden ordered to enforce the zero trust structure

ffmpeg硬编解码 Inter QSV

GemBox. Bundle 43.0 Crack

Leetcode力扣刷题

定制 or 订阅?未来中国 SaaS 行业发展趋势是什么?

Vscode configures eslint to automatically format an error "auto fix is enabled by default. use the single string form“

vscode保存代码时自动eslint格式化

Docker installs mysql5.7 (enable binlog function and modify characters)

vscode保存代碼時自動eslint格式化

The use of histogram function in MATLAB
随机推荐
From a "trendsetter" to a "wind chaser", can master Kang still lead the market?
04_特征工程—特征选择
使用exe4j 将.jar文件打包为.exe文件
Authing 背后的计算哲学
Semaphore PV operation of process interaction and its code implementation
如何成为一个乐观派组织?
Is it safe for Xiaobai to open an account directly on the flush?
QLineEdit 设置输入掩码
Splitting method of MySQL large tables
Recyclerview cache reuse analysis, source code interpretation
Association relationship
Authoring share | understanding saml2 protocol
Message queue push / pull mode Learning & ActiveMQ and JMS learning
LeetCode-1005. Maximized array sum after K negations
How to simplify a lot of if... Elif... Else code?
Docker installs mysql5.7 (enable binlog function and modify characters)
ffmpeg奇偶场帧Interlace progressive命令和代码处理
Is the securities account given by qiniu business school safe? Do you charge for opening an account
Database backup (MySQL)
Pychart tips - how to set up a background picture