注册 登录
编程论坛 C图形专区

关于链表错误

gold615 发布于 2015-06-18 14:33, 644 次点击
创建一个简单的链表后,编译也能通过,但输出链表时总是有错误为什么?
#include<stdio.h>
#include<stdlib.h>
struct list
{
    int data;
    struct list *next;
} *Link;
void Creat_List(list *Link);
void Print_list(list *Link);
main()
{
    Creat_List(Link);
    printf("the list has been created\n");
    getchar();
    Print_list(Link);
}
void Creat_List(list *Link)
{
   
    printf("to creat the list\n");
    char c='y';
    int t;
    list *p,*q;
    Link=q=(list *)malloc(sizeof(list));
    while(c=='y')
    {
        
        printf("input the data:\t");
        scanf("%d",&t);
        p=(list *)malloc(sizeof(list));
        p->data=t;
        q->next=p;
        q=p;
        q->next=NULL;
        printf("press 'y' to continue or 'n' to exit\n");
        getchar();
        c=getchar();
    }
}

void Print_list(list *Link)
{
    list *p;
    p=Link->next;
    while(p)
    {
        printf("%d\t",p->data);
        p=p->next;
    }
    printf("\n");
}
1 回复
#2
林月儿2015-06-21 10:19
程序代码:
#include<stdio.h>
#include<stdlib.h>
struct list
{
    int data;
    struct list *next;
} *Link;
struct list* Creat_List(list *Link);
void Print_list(list *Link);
main(){
    Link=Creat_List(Link);
    printf("the list has been created\n");
    getchar();
    Print_list(Link);
}
struct list* Creat_List(list *Link)
{
   
    printf("to creat the list\n");
    char c='y';
    int t;
    list *p,*q;
    Link=q=(list *)malloc(sizeof(list));
    while(c=='y')
    {
        
        printf("input the data:\t");
        scanf("%d",&t);
        p=(list *)malloc(sizeof(list));
        p->data=t;
        q->next=p;
        q=p;
        q->next=NULL;
        printf("press 'y' to continue or 'n' to exit\n");
        getchar();
        c=getchar();
    }
    return Link;
}

void Print_list(list *Link)
{
    list *p;
    p=Link->next;
    while(p)
    {
        printf("%d\t",p->data);
        p=p->next;
    }
    printf("\n");
}
1