当前位置: 代码迷 >> C语言 >> 动态链表问题
  详细解决方案

动态链表问题

热度:232   发布时间:2008-04-21 17:01:58.0
动态链表问题
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
struct Sk
{
    int num;
    char name[20];
    float score;
    struct Sk * next;
};

struct Sk* linklist()
{
    struct Sk *h,*p1,*p2;
    int n=0;
    p1=p2=(struct Sk*)malloc(sizeof(struct Sk));    
    printf("输入学号,名字,和成绩\n");
    scanf("%d %s%f",&p1->num,p1->name,&p1->score);
    h=NULL;
    while(p1->num!=0)
    {
        n++;
        if(n==1)
            h=p1;
        else p2->next=p1;
        p2=p1;
        if((p1=(struct Sk*)malloc(sizeof(struct Sk)))==NULL)
        {
            printf("内存分配失败");
            exit(1);
        }
        scanf("%d %s%f",&p1->num,p1->name,&p1->score);        
    }
    p2->=NULL;
    return h;
}

int print(struct Sk *p)
{
    printf("学号    名字     成绩\n");
    while(p!=NULL)
    {
        printf("%d\t%s\t%.2f\n",p->num,p->name,p->score);
        p=p->next;
    }
    return 0;
}


int main()
{
    struct Sk *head;
    head=linklist();
    print(head);
    getch();
    return 0;
}为什么输出时会报错

[[it] 本帖最后由 bianfeng 于 2008-4-21 17:05 编辑 [/it]]
搜索更多相关的解决方案: 链表  struct  动态  num  

----------------解决方案--------------------------------------------------------
  相关解决方案