24小时热门版块排行榜    

查看: 458  |  回复: 2
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

sheng-hao

铜虫 (初入文坛)

[求助] C语言链表的建立,有几个地方看不懂,请大神指导 已有1人参与

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define NULL 0
struct stu{
long  num;
float score;
struct stu *next;
};
typedef struct stu  Stu_link;
Stu_link *cre_link(){
        Stu_link *head=NULL,*last=NULL,*p;
        p=(Stu_link *)malloc(sizeof(Stu_link));//开辟节点
        printf("input num:";
        scanf("%d",&(p->num));
        printf("input score:";
        scanf("%d",&(p->score));
        p->next=NULL;
    while(p->num){
                if(head=NULL){
        head=p;
        last=p;
        }
        else{
        last->next=p;
        head=p;
        }
                p=(Stu_link *)malloc(sizeof(Stu_link));//开辟新节点
        printf("input num:";
                scanf("%d",&(p->num));
        if(p->num){
        printf("input score:";
                scanf("%d",&(p->score));
        p->next=NULL;
        }
    }
return head;
}
int main()
{
        cre_link();
        system("pause";
        return 0;
}
其中的如下语句:
if(head=NULL){
        head=p;
        last=p;
        }
        else{
        last->next=p;
        head=p;
        }
请问这几句怎么理解?
回复此楼

» 猜你喜欢

机会要自己创造
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

duotojh

金虫 (小有名气)

引用回帖:
2楼: Originally posted by duotojh at 2014-11-13 21:25:54
有错误:
(1)是 if(head=NULL) 应改为 if(head==NULL) 或 if(!head)
(2)是否定分支中:else{ last->next=p; head=p;} 改为 else{ last->next=p; last=p;}

肯定分支执行过程如图C语言链表的建立,有几个地方看不懂,请大神指导

否定分支执行过程如图C语言链表的建立,有几个地方看不懂,请大神指导-1
3楼2014-11-13 21:53:09
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 3 个回答

duotojh

金虫 (小有名气)

【答案】应助回帖

感谢参与,应助指数 +1
有错误:
(1)是 if(head=NULL) 应改为 if(head==NULL) 或 if(!head)
(2)是否定分支中:else{ last->next=p; head=p;} 改为 else{ last->next=p; last=p;}
2楼2014-11-13 21:25:54
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
信息提示
请填处理意见