C 链表,如果head不存在,如何插入?
我得到了一个链表,可以保存结果(W或L)和每场比赛的得/失分。到目前为止一切都很好,但当脑袋不存在/是空的时候,我就麻烦了。我还意识到我对如何实现链表有一个非常糟糕的概述,有人得到了好的和可以理解的资源吗?无论如何,这是我的代码:C 链表,如果head不存在,如何插入?,c,struct,linked-list,head,C,Struct,Linked List,Head,我得到了一个链表,可以保存结果(W或L)和每场比赛的得/失分。到目前为止一切都很好,但当脑袋不存在/是空的时候,我就麻烦了。我还意识到我对如何实现链表有一个非常糟糕的概述,有人得到了好的和可以理解的资源吗?无论如何,这是我的代码: #include <stdio.h> #include <stdlib.h> struct node { int point; char outcome; struct node *next; }; void add(struc
#include <stdio.h>
#include <stdlib.h>
struct node {
int point;
char outcome;
struct node *next;
};
void add(struct node *data){
if(data == NULL){
data = malloc(sizeof(struct node));
printf("Outcome and points?\n");
int point;
char outcome;
scanf("%c %d",&outcome,&point);
fgetc(stdin);
data->point=point;
data->outcome=outcome;
data->next=NULL;
}else{
struct node *current= data;
while(current->next != NULL){
current = current->next;
}
current->next = malloc(sizeof(struct node));
current=current->next;
printf("Outcome and points?\n");
int point;
char outcome;
scanf("%c %d",&outcome,&point);
fgetc(stdin);
current->point=point;
current->outcome=outcome;
current->next=NULL;
}
}
void print(struct node *data){
struct node *current = data;
while(current != NULL){
printf("%c with %3d\n",current->outcome,current->point);
current = current->next;
}
}
int main()
{
struct node *head=NULL;
add(head);
add(head);
add(head);
print(head);
}
#包括
#包括
结构节点{
积分点;
结果;
结构节点*下一步;
};
无效添加(结构节点*数据){
如果(数据==NULL){
数据=malloc(sizeof(struct node));
printf(“结果和分数?\n”);
积分点;
结果;
scanf(“%c%d”、&结果和点);
fgetc(stdin);
数据->点=点;
数据->结果=结果;
数据->下一步=空;
}否则{
结构节点*当前=数据;
while(当前->下一步!=NULL){
当前=当前->下一步;
}
当前->下一步=malloc(sizeof(结构节点));
当前=当前->下一步;
printf(“结果和分数?\n”);
积分点;
结果;
scanf(“%c%d”、&结果和点);
fgetc(stdin);
当前->点=点;
当前->结果=结果;
当前->下一步=空;
}
}
作废打印(结构节点*数据){
结构节点*当前=数据;
while(当前!=NULL){
printf(“%c和%3d\n”,当前->结果,当前->点);
当前=当前->下一步;
}
}
int main()
{
结构节点*head=NULL;
增加(标题);
增加(标题);
增加(标题);
打印头;
}
任何帮助都将不胜感激:)当您执行以下操作时:
void add(struct node *data){
if(data == NULL){
data = malloc(sizeof(struct node));
调用函数中,head
的值不变
建议改变策略
struct node* add(struct node *head)
{
if(head == NULL){
head = malloc(sizeof(struct node));
printf("Outcome and points?\n");
int point;
char outcome;
scanf("%c %d",&outcome,&point);
fgetc(stdin);
head->point=point;
head->outcome=outcome;
head->next=NULL;
}else{
struct node *current= head;
while(current->next != NULL){
current = current->next;
}
current->next = malloc(sizeof(struct node));
current=current->next;
printf("Outcome and points?\n");
int point;
char outcome;
scanf("%c %d",&outcome,&point);
fgetc(stdin);
current->point=point;
current->outcome=outcome;
current->next=NULL;
}
return head;
}
然后更改用法:
int main()
{
struct node *head = add(NULL);
add(head);
add(head);
print(head);
}
可以通过使用锚节点启动列表来简化代码。锚节点是仅用于其
next
指针的节点。在下面的代码中,调用calloc
创建锚节点,并将锚节点中的所有字段设置为0
。换句话说,创建节点时使用next==NULL
请注意,在打印列表时,for的循环通过使用for(list=list->next;…)跳过锚点节点开始
#包括
#包括
结构节点
{
积分点;
结果;
结构节点*下一步;
};
无效添加(结构节点*列表)
{
结构节点*数据;
数据=malloc(sizeof(struct node));
数据->下一步=空;
printf(“结果和分数?\n”);
scanf(“%c%d”,&data->output,&data->point);
fgetc(stdin);
while(列表->下一步!=NULL)
列表=列表->下一步;
列表->下一步=数据;
}
作废打印(结构节点*列表)
{
对于(列表=列表->下一步;列表!=NULL;列表=列表->下一步)
printf(“%c和%3d\n”,列表->结果,列表->点);
}
int main()
{
结构节点*head=calloc(1,sizeof(结构节点));
增加(标题);
增加(标题);
增加(标题);
打印头;
}
旁注:为了简单起见,我省略了一些错误检查,您应该检查calloc
、malloc
和scanf
中的返回值,并处理任何错误。当然,您应该释放末端的所有节点。如果head为NULL,则将添加的节点设为head。这是一个很好的资源。也指
#include <stdio.h>
#include <stdlib.h>
struct node
{
int point;
char outcome;
struct node *next;
};
void add( struct node *list )
{
struct node *data;
data = malloc(sizeof(struct node));
data->next = NULL;
printf("Outcome and points?\n");
scanf("%c %d",&data->outcome,&data->point);
fgetc(stdin);
while (list->next != NULL)
list = list->next;
list->next = data;
}
void print( struct node *list )
{
for (list = list->next; list != NULL; list = list->next)
printf("%c with %3d\n", list->outcome, list->point);
}
int main()
{
struct node *head = calloc( 1, sizeof(struct node) );
add(head);
add(head);
add(head);
print(head);
}