| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 954 人关注过本帖
标题:问一下大神关于动态链表建立的问题
只看楼主 加入收藏
梦想如初
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-12-31
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
问一下大神关于动态链表建立的问题
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define LEN sizeof(struct student)  // student结构的大小

struct student *creat();            //创建链表
void print(struct student *head);   //打印链表

struct student
{
      int num;
      float score;
      struct student *next;
};

int n; //全局变量,用来记录存放了多少数据。

void main()
{
      struct student *stu;

      stu = creat();
      print( stu );

      printf("\n\n");
}

struct student *creat()
{
      struct student *head;
      struct student *p1, *p2;
  
      p1 = p2 = (struct student *)malloc(LEN);  // LEN是student结构的大小

      printf("Please enter the num :");
      scanf("%d", &p1->num);
      printf("Please enter the score :");
      scanf("%f", &p1->score);

      head = NULL;     
      n = 0;   
      
      while( 0 != p1->num )
      {
            n++;
            if( 1 == n )
            {
                  head = p1;                 
            }
            else
            {
                  p2->next = p1;
            }

            p2 = p1;

            p1 = (struct student *)malloc(LEN);

            printf("\nPlease enter the num :");
            scanf("%d", &p1->num);
            printf("Please enter the score :");
            scanf("%f", &p1->score);
      }

      p2->next = NULL;

      return head;
}

void print(struct student *head)
{
      struct student *p;
      printf("\nThere are %d records!\n\n", n);

      p = head;
      if( NULL != head )
      {
            do
            {
                  printf("学号为 %d 的成绩是: %f\n", p->num, p->score);
                  p = p->next;
            }while( NULL != p );
      }
}
源代码,请问一下struct student *creat(),这里的括号是什么意思;
struct student *creat()
{
      struct student *head;
      struct student *p1, *p2;
  
      p1 = p2 = (struct student *)malloc(LEN);  // LEN是student结构的大小

      printf("Please enter the num :");
      scanf("%d", &p1->num);
      printf("Please enter the score :");
      scanf("%f", &p1->score);

      head = NULL;     
      n = 0;   
      
      while( 0 != p1->num )
      {
            n++;
            if( 1 == n )
            {
                  head = p1;                 
            }
            else
            {
                  p2->next = p1;
            }

            p2 = p1;

            p1 = (struct student *)malloc(LEN);

            printf("\nPlease enter the num :");
            scanf("%d", &p1->num);
            printf("Please enter the score :");
            scanf("%f", &p1->score);
      }

      p2->next = NULL;

      return head;
}
结构体中为什么可以添加,while( 0 != p1->num )
      {
            n++;
            if( 1 == n )
            {
                  head = p1;                 
            }
            else
            {
                  p2->next = p1;
            }

            p2 = p1;

            p1 = (struct student *)malloc(LEN);

            printf("\nPlease enter the num :");
            scanf("%d", &p1->num);
            printf("Please enter the score :");
            scanf("%f", &p1->score);
这些
搜索更多相关主题的帖子: include 动态 记录 
2015-12-31 00:21
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
這些問題不知道,就表明你步子邁得太大,該回頭補基礎。別弄鏈表了。

授人以渔,不授人以鱼。
2015-12-31 00:48
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
收藏
得分:5 
源代码,请问一下struct student *creat(),这里的括号是什么意思;---------这里这么写可能更能说明问题:struct student*  creat(),这表明这是一个返回值为指向student结构体类型指针的函数,函数名为creat()。
结构体中为什么可以添加,while( 0 != p1->num ).......-----------完全不知道你在说什么,结构体在哪里?这一串,是创建动态链表的关键,你得读懂代码再来问的。
2015-12-31 09:22
未来大仙
Rank: 6Rank: 6
来 自:黑窟窿
等 级:侠之大者
威 望:4
帖 子:263
专家分:491
注 册:2015-6-20
收藏
得分:5 
显然不知道你想干嘛

好好学习,天天向上!
2015-12-31 10:09
tredy6t
Rank: 2
等 级:论坛游民
帖 子:41
专家分:59
注 册:2015-8-6
收藏
得分:5 
检测第一个输入的值,如果为0退出程序,!0就继续
2015-12-31 11:53
快速回复:问一下大神关于动态链表建立的问题
数据加载中...
 
   



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

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