C 链表代码中的插入操作无效
这是一个用于链表基本插入和显示操作的代码,但在输入参数后,插入函数程序的键和信息不继续,我的意思是,我输入4作为键,5作为信息,应该有一个由头部指向的节点,当显示被称为“我的链表”时,应该显示一个元素,但不会发生。插入和显示功能是否有问题,或者应该怎么做C 链表代码中的插入操作无效,c,struct,C,Struct,这是一个用于链表基本插入和显示操作的代码,但在输入参数后,插入函数程序的键和信息不继续,我的意思是,我输入4作为键,5作为信息,应该有一个由头部指向的节点,当显示被称为“我的链表”时,应该显示一个元素,但不会发生。插入和显示功能是否有问题,或者应该怎么做 #include <stdio.h> #include <stdlib.h> static struct node{ int key, info; struct node *next; }; stat
#include <stdio.h>
#include <stdlib.h>
static struct node{
int key, info;
struct node *next;
};
static struct node *head, *z;
initialize()
{
head = (struct node*)malloc(sizeof *head);
z = (struct node*)malloc(sizeof *z);
head->next = z;
z->next = z;
z->key = -1;
}
insert(int n, int info)
{
struct node *t, *x;
t = head;
while (t->next != z) {
t = t->next;
}
x = (struct node *)malloc (sizeof *x);
x->key = n;
x->next = t->next;
t->next = x;
x->info = info;
}
show()
{
struct node *t = head;
while (t->next != z) {
t = t->next;
printf("%d\t%d\n", t->key, t->info);
}
}
main()
{
initialize();
int i, j;
printf("enter the number and info\n");
scanf("%d%d", &i, &j); // i is key and j is info
insert(i, j); // passing arguments to insert function
show();
}
试试这个
静态结构节点{更改为此后->结构节点{
初始化->无效初始化
插入n,整数信息->无效插入n,整数信息
显示->作废显示
main->int main//并返回0
程序不继续这意味着什么?你知道你从不调用show吗?在代码中包含stdlib.h。使用-Wall选项编译代码以查看可能的警告。调用insert call show后查看输出。好的,我明白了,实际上我应该在两行中给出输入,即scanf%d,&i,然后scanf%d,&j。我不知道是否我可以同时给他们。谢谢@an检查此代码: