| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2009 人关注过本帖
标题:创建节点
只看楼主 加入收藏
sunpy
Rank: 1
来 自:厦门
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-10-1
结帖率:100%
收藏
 问题点数:0 回复次数:4 
创建节点
一个创建节点的函数:
struct student * create(int n)
{
    //创建3个结构体指针变量
    struct student *head,*p1,*p2;
    int t=0;
    //首先进行初始化
    head=NULL;
    p1 = (struct student *)malloc(LEN);
    printf("the number and score is:\n");
    scanf("%d%f",&p1->num,&p1->score);
    //创建链表
    do
    {
        t=t+1;
        if(t==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1 = (struct student *)malloc(LEN);
        printf("the number and score is:\n");
        scanf("%d%f",&p1->num,&p1->score);
    }while(t<n);
    p2->next = NULL;
    free(p1);//输入的最后一个数被free掉;
    return(head);
}
n为需要创建的节点个数,为什么我要创建一个节点时编译器就会发送错误报告?
搜索更多相关主题的帖子: 节点 score student struct 
2007-12-20 17:56
jxj777
Rank: 1
等 级:新手上路
帖 子:91
专家分:0
注 册:2007-10-27
收藏
得分:0 
感觉程序没问题,
如果可以,请贴出报错提示或完整代码

一个人的力量是缈小的....... 互帮互助才是出路
2007-12-20 20:23
swiminfire
Rank: 1
等 级:新手上路
帖 子:96
专家分:0
注 册:2005-8-11
收藏
得分:0 
你初始化那边错了,应该用  head=(student *)malloc(sizeof(LEN));
head->next=NULL;这样才是头的初始化.你自己在看看吧

还有你那个新建节点的创建有错,是p=(student *)malloc(sizeof(LEN)); 才对.

[[italic] 本帖最后由 swiminfire 于 2007-12-20 21:49 编辑 [/italic]]

Get in first, and then word your ways out ! BY KIKI
2007-12-20 21:47
sunpy
Rank: 1
来 自:厦门
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-10-1
收藏
得分:0 
整个程序

//将2个链表合并,并按升序排列
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
    int num;
    float score;
    struct student * next;
};
struct student * create(int n);
struct student * combine(struct student *lista, struct student *listb);
int
main()
{
    struct student *list1,*list2,*list,*p;
    int n1,n2;
//  调用函数创建2个链表,返回头指针
    printf("please input the length of list1 and list2:\n");
    scanf("%d%d",&n1,&n2);
    printf("create list1,with the length of %d .\n",n1);
    list1=create(n1);
    p=list1;
    if(p!=NULL)
    {
        do
        {
            printf("%d  %.2f\n",p->num,p->score);
            p = p->next;
        }
        while(p != NULL);//while(p->next != NULL);
    }
    printf("create list2,with the length of %d .\n",n2);
    list2=create(n2);
    p=list2;
    if(p!=NULL)
    {
        do
        {
            printf("%d  %.2f\n",p->num,p->score);
            p = p->next;
        }
        while(p != NULL);//while(p->next != NULL);
    }
    printf("\n");
//合并链表
    list = combine(list1,list2);
//输出合并后的链表
    p=list;
    if(p!=NULL)
    {
        do
        {
            printf("%d  %.2f\n",p->num,p->score);
            p = p->next;
        }
        while(p != NULL);//while(p->next != NULL);
    }
    return(0);
}



struct student * create(int n)
{
    //创建3个结构体指针变量
    struct student *head,*p1,*p2;
    int t=0;
    //首先进行初始化
    head=NULL;
    p1 = (struct student *)malloc(LEN);
    printf("the number and score is:\n");
    scanf("%d%f",&p1->num,&p1->score);
    //创建链表
    do
    {
        t=t+1;
        if(t==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1 = (struct student *)malloc(LEN);
        printf("the number and score is:\n");
        scanf("%d%f",&p1->num,&p1->score);
    }while(t<n);
    p2->next = NULL;
    free(p1);//输入的最后一个数被free掉;
    return(head);
}
    

struct student * combine(struct student *lista, struct student *listb)
{
    struct student *pa,*pb,*head,*p1;
    pa=lista;
    pb=listb;
    if(pa==NULL)
        return(listb);
    else if(pb==NULL)
        return(lista);
    else
    {   
        if(pa->num > pb->num)
        {
            head=pb;
            pb=pb->next;
        }
        else
        {
            head=pa;
            pa=pa->next;
        }
        p1=head;
        while(pa->next!=NULL&& pb->next!=NULL)
        {
            if(pa->num > pb->num)
            {
                p1->next=pb;
                pb=pb->next;
            }
            else
            {
                p1->next=pa;
                pa=pa->next;
            }
            p1=p1->next;//
        }
        if(pa->next==NULL)
            p1->next=pb;
        else if(pb->next==NULL)
            p1->next=pa;
        return(head);
    }
}
编译时没有错误
可是运行时系统就说遇到问题要关闭。。。。

[[italic] 本帖最后由 sunpy 于 2007-12-20 23:04 编辑 [/italic]]

荀子《劝学》:“不积跬步,无以至千里;不积小流,无以成江海.”
2007-12-20 22:40
sunpy
Rank: 1
来 自:厦门
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-10-1
收藏
得分:0 
顺便看看这个程序
//将一个链表按逆序排列
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
    int num;
    float score;
    struct student * next;
};

struct student * create(int n);
struct student * revllist(struct student *head);
int
main()
{
    struct student * head,*p,*rehead;
    int length;
    printf("please input the length of the list:\n");
    scanf("%d",&length);
//创建链表
    head = create(length);
    p=head;
    if(head!=NULL)
        do
        {
            printf("%d  %f\n",p->num,p->score);
            p=p->next;
        }while(p!=NULL);
    //调用函数按逆序排列
    rehead=revllist(head);
    p=rehead;
    if(p!=NULL)
        do
        {
            printf("%d  %f\n",p->num,p->score);
            p=p->next;
        }while(p!=NULL);

    return(0);
}


struct student * create(int n)
{
    struct student *p1,*p2,*head;
    head=NULL;
    int t=0;
    p1=(struct student * )malloc(LEN);
    printf("please input the num and score of the student :\n");
    scanf("%d%f",&p1->num,&p1->score);
    do    
    {
        t=t+1;
        if(t==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student * )malloc(LEN);
        printf("please input the num and score of the student :\n");
        scanf("%d%f",&p1->num,&p1->score);
    }while(t<n);
    p2->next = NULL;
    free(p1);
    return(head);
}

struct student * revllist(struct student * head)
{
    
    struct student * p,*pr,*revhead;
    for(p=head;p!=NULL;p=p->next);
    revhead=p;
    pr=revhead;
    while(pr!=head)
    {
        for(p=head;p!=pr;p=p->next);
        pr->next=p;
        pr=pr->next;
    }
    head->next=NULL;
    return(revhead);
}
编译时没有错误
运行时系统就说遇到问题要关闭

荀子《劝学》:“不积跬步,无以至千里;不积小流,无以成江海.”
2007-12-20 23:02
快速回复:创建节点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020315 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved