c语言链表的插入
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&¤t->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;
}
这个错误是什么,怎么改
回答
改成
struct Node{
int value;
struct Node *next;
};
以上是 c语言链表的插入 的全部内容, 来源链接: utcz.com/a/65146.html