gpt4 book ai didi

c - 链表代码中的插入操作不起作用

转载 作者:行者123 更新时间:2023-11-30 20:39:53 26 4
gpt4 key购买 nike

这是链表基本插入和显示操作的代码,但是在输入插入函数程序的参数 key 和 info 后不会继续我的意思是让我们说我输入 4 作为 key 和 5 作为 info 应该有一个节点由 head 创建,当调用 show 时,应该显示带有一个元素的链接列表,但它没有发生。插入和显示功能有问题吗?我该怎么办?

#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();
}

最佳答案

试试这个

静态结构节点{更改为(下文-->)结构节点{

initialize() --> voidinitialize()

insert(int n, int info) --> void insert(int n, int info)

show() --> void show()

main() --> int main()//并返回0;

demo

关于c - 链表代码中的插入操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25568972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com