链接列表代码中的插入操作不起作用

时间:2014-08-29 13:09:26

标签: c struct

它是链接列表基本插入和显示操作的代码,但输入参数后插入函数程序的键和信息不继续我的意思是让我们说我输入4作为键,5作为信息应该有一个节点由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();
}

1 个答案:

答案 0 :(得分:0)

试试这个

static struct node{更改为(此后 - &gt;)struct node{

initialize() - &gt; void initialize()

insert(int n, int info) - &gt; void insert(int n, int info)

show() - &gt; void show()

main() - &gt; int main() //并返回0;

demo