当前位置:网站首页>6-8 creating and traversing linked lists
6-8 creating and traversing linked lists
2022-06-11 17:37:00 【Tomatoes_ Menon】
This topic requires the implementation of the following three functions , Achieve the following functions :
Enter several positive integers , With -1 end , Take the way of adding nodes to the linked list to establish a one-way linked list . Traverse and output this one-way linked list .
Function interface definition :
Add node functions to the tail of the linked list :
struct link *AppendNode(struct link *head,int data);
head: Chain header node pointer , If head by NULL, A new header node will be created
data: Nodes to add data The number
The function returns : The node pointer of the chain header after adding nodes Traversal display linked list function :
void DisplyNode(struct link *head);
head : Chain header node pointer Sample referee test procedure :
#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; /* Chain header pointer */
while (1)
{
scanf("%d",&data);
if (data==-1)
break;
head = AppendNode(head,data);/* towards head Add a node to the end of the linked list of the header pointer */
}
DisplyNode(head); /* Displays the information of each node in the current linked list */
DeleteMemory(head); /* Free all dynamically allocated memory */
return 0;
}
/* In this way 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;
}
}
/* In this way struct link *AppendNode(struct link *head,int data); */
/* In this way void DisplyNode(struct link *head); */
/* Please fill in the answer here */Input format :
A series of positive integers , With -1 end
Output format :
Output each item in the one-way linked list , Between each number -> Connect
sample input :
Here's a set of inputs . for example :
1 3 5 7 9 -1
sample output :
Here is the corresponding output . for example :
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;
}
}边栏推荐
- TiDB-unsafe recover(tikv宕机数大于等于一半副本数)
- R language to find missing value location of data set
- Authoring share | understanding saml2 protocol
- 测试基础之:黑盒测试
- [online problem] timeout waiting for connection from pool
- 开源项目那么多,这次带你了解个版本的区别,明白alpha版、beta版、rc版是什么意思
- 6-7 文件读写操作
- Biden ordered to enforce the zero trust structure
- ffmpeg硬编解码 Inter QSV
- What subclasses inherit, polymorphism, and upward transformation
猜你喜欢
随机推荐
Service learning notes 04 other service implementation methods and alternative methods
使用exe4j 将.jar文件打包为.exe文件
Mathematical basis of information security Chapter 4 -- quadratic residual and square root
Is it safe for Xiaobai to open an account directly on the flush?
6-3 批量求和(*)
6-7 文件读写操作
Authing 背后的计算哲学
Classification and method of feature fusion
定制 or 订阅?未来中国 SaaS 行业发展趋势是什么?
Biden ordered to enforce the zero trust structure
信息安全数学基础 Chapter 2——同余
vscode配置eslint自动格式化报错“The setting is deprecated. Use editor.codeActionsOnSave instead with a source“
Tidb CDC create task error unknown or incorrect time zone
R语言寻找数据集缺失值位置
Merge K ascending linked lists ---2022/02/26
Vscode configures eslint to automatically format with an error "the setting is deprecated. use editor.codeactionsonsave instead with a source“
端口规划与APJ
vscode保存代码时自动eslint格式化
tidb-写热点的测试及分析
tidb-gc相关问题









