| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 406 人关注过本帖
标题:代码有些疑问,麻烦大家帮我讲解一下
只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
已结贴  问题点数:20 回复次数:3 
代码有些疑问,麻烦大家帮我讲解一下

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "malloc.h"
#define NULL 0
#define LEN sizeof(struct stu)
struct stu
{
    int num;
    struct stu *next;
}*p1,*p2;
struct stu *creat()
{
    int temp;
    struct stu *head=NULL;
    printf("input temp:\n");
    scanf("%d",&temp);
    while(temp!=0)
    {
        p1=(struct stu *)malloc(LEN);
        if(head==NULL)//这个head=NULL在程序中是什么意思啊?,为什么为空呢?
            head=p1;
        else
            p2->next=p1;
        p1->num=temp;
        p2=p1;
        printf("\ninput number:");
        scanf("%d",&temp);
    }
    p2->next=NULL;
    return(head);
}
void output(struct stu *head)
{
    for(p1=head;p1!=NULL;p1=p1->next)
    {
      
        printf("%4d",p1->num);
      
    }
}
struct stu *turnback(struct stu *head)
{
    struct stu *a,*newhead,*temp;
    p2=head;
    p1=head->next;
    head->next=NULL;
    while(p1!=NULL)
    {
        temp=p1->next;
        p1->next=p2;
        p2=p1;
        p1=temp;
    }
    newhead=p2;
    return(newhead);
}


int main(int argc, char* argv[])
{
     struct stu *head;
    head=creat();
    printf("the original list:\n");
    output(head);
    head=turnback(head);
    printf("\nthe new list:\n");
    output(head);
    printf("\n");
    return 0;
}
搜索更多相关主题的帖子: 麻烦 讲解 代码 疑问 
2009-09-07 18:30
养下就很帅
Rank: 2
等 级:论坛游民
帖 子:32
专家分:50
注 册:2007-8-3
收藏
得分:20 
初始化的时候struct stu *head=NULL; head指向null  节点p1第一次被创建的时候 head->null
head==NULL 为了判断是不是第一个节点  
2009-09-07 19:19
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
还是不太懂,head指针已经初始化为NULL,如果head=null,怎么head等于第一个节点了呢?
2009-09-07 19:29
养下就很帅
Rank: 2
等 级:论坛游民
帖 子:32
专家分:50
注 册:2007-8-3
收藏
得分:0 
第一次循环的时候  p1=(struct stu *)malloc(LEN); 节点被创建 因为这时候 head指向空 所以 就执行if语句 让head指向p1
第二次循环的时候 head指向的是p1 所以直接就执行else后面的语句
2009-09-07 19:41
快速回复:代码有些疑问,麻烦大家帮我讲解一下
数据加载中...
 
   



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

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