c语言链表的插入

2020-11-07 14:15发布

include<stdio.h>

include<stdlib.h>

struct Node
{
int value;
struct Insert *next;
};
void InsertNode(struct Node **head,int value) //接收的是head指针的值,对它进行修改,同时添加用户输入的数据
{
struct Node *pervious;
struct Node *current;
struct Node *news;
current = head; //修改head指针的值
pervious = NULL; //初始化pervious指针
while(current!=NULL&&current->value<value)//搜索和判断
{
pervious = current; //小于的时候current就变成了pervious
current = current->next;
}
news = (struct Node
)malloc(sizeof(struct Node));
if(news == NULL)
{
printf("分配空间失败\n");
exit(1);
}
news->value = value;
news->next = current;//将news指针指向current
if(pervious==NULL) //说明没有执行while循环,
{
*head = news;
}
else
{
pervious->next = news;//将pervious指针指向news
}
}
void printNode(struct Node *head) //打印操作
{
struct Node *current;
current = head;
while(current != NULL)
{
printf("%d",current->value);
current = current->next;
}
putchar('\n');
}
int main(void)
{
struct Node *head = NULL;
int input;
while(1)
{
printf("请输入添加的值(-1退出循环):\n");
scanf("%d",input);
if(input == -1)
{
break;
}
else
{
InsertNode(&head,input); //要修改的是head指针的值
printNode(head); //仅使用这个指针
}
}
return 0;
}

这个错误是什么,怎么改

标签:
1条回答
冷血范
2楼-- · 2020-11-07 14:41


改成

struct Node
{
int value;
struct Node *next;
};
查看更多
登录 后发表回答