高分悬赏:链表问题!!!

矫情狗_____ 2012-02-17 11:35:45
求超级详细注释...一个个代码进行解释...
#include <stdio.h>
#include <malloc.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");
getch();
}

struct student *creat()
{
struct student *head;
struct student *p1, *p2;

p1 = p2 = (struct student *)mall(LEN); //LEN是结构体大小;
printf("Please enter the Nun: ");
scanf("%d", &p1->num);
printf("Please enter the Score: ");
scanf("%f", &p1->score);

head = NULL;
n = 0;

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

p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("Please enter the Nun: ");
scanf("%d", &p1->num);
printf("Please enter the Score: ");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
...全文
155 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Leaveye 2012-02-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 stacksg 的回复:]
超级详细注释...一个个代码进行解释...
[/Quote]
done
矫情狗_____ 2012-02-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gdujian0119 的回复:]
这个……楼主别想了,还是自己安心看书吧。
[/Quote]
总会有高人的啊...学到动态分配这,书里居然不给小例,一来就大例
孤独小剑 2012-02-18
  • 打赏
  • 举报
回复
这个……楼主别想了,还是自己安心看书吧。
duke56 2012-02-18
  • 打赏
  • 举报
回复
矫情狗_____ 2012-02-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 luhao1993 的回复:]
各位大神 我给这个程序补了个打印函数 再改了前面的两个错误 可是编译正确 但运行错误 这是为什么呢??打印函数如下:
void print(struct student *head)
{struct student *p;
printf("%d",n);
if (head!=NULL)
{ p=head;
while (p!=NULL)
{printf("%d%5.1f\n",p……
[/Quote]
void print(struct student *head)
{
struct student *p;
printf("%d",n);

p=head;

while (p!=NULL) //这里其实就是个判断!
{
printf("%d%5.1f\n",p->num,p->score);
p=p->next;
}
}
尘缘udbwcso 2012-02-18
  • 打赏
  • 举报
回复
看书,自己试着写
孤独小剑 2012-02-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 stacksg 的回复:]

引用 2 楼 gdujian0119 的回复:
这个……楼主别想了,还是自己安心看书吧。

总会有高人的啊...学到动态分配这,书里居然不给小例,一来就大例
[/Quote]不是高人,是好的不能再好的人……
maxiao001 2012-02-18
  • 打赏
  • 举报
回复
我靠,你肿么没请个老师教你。。
卓越_小Y 2012-02-18
  • 打赏
  • 举报
回复
各位大神 我给这个程序补了个打印函数 再改了前面的两个错误 可是编译正确 但运行错误 这是为什么呢??打印函数如下:
void print(struct student *head)
{struct student *p;
printf("%d",n);
if (head!=NULL)
{ p=head;
while (p!=NULL)
{printf("%d%5.1f\n",p->num,p->score);
p=p->next;
}
矫情狗_____ 2012-02-18
  • 打赏
  • 举报
回复
#include <stdio.h>
//用于包含输入输出函数
#include <malloc.h>
//用于包含malloc函数
#define LEN sizeof(struct student) //student结构大小;

struct student *creat(); //创建链表;
void print(struct student *head); //打印链表;
//创建student结构体模型
struct student
{
//学生ID号
int num;
//学生分数
float score;
//指向student类型的指针
struct student *next;
};

int n;//全局变量,用来记录存放多少数据;
//主函数
void main()
{
//定义一个student结构体的指针变量
struct student *stu;
//调用create函数stu返回链表首地址
stu = creat();
//将首地址传给print函数,进行打印
print(stu);
//打印两个回车
printf("\n\n");
//让控制台窗口停留,以便观看输出结果
getch();
}
//以下是create函数的实现过程
struct student *creat()
{
//定义一个student结构体的指针变量head用来指向链表的首个地址
struct student *head;
//定义一个student结构体的指针的临时变量 用来传送地址
struct student *p1, *p2;
//将定义的指针变量初始化设置成LEN大小,这里函数名有误
p1 = p2 = (struct student *)mall(LEN); //LEN是结构体大小;
//打印指示
printf("Please enter the Nun: ");
//对学号进行赋值
scanf("%d", &p1->num);
//打印指示
printf("Please enter the Score: ");
//对分数进行赋值
scanf("%f", &p1->score);
//将指向链表首地址的变量初始化
head = NULL;
//n 用来计数的,保存学生总数
n = 0;
//只要学号不为0 则重复赋值
while(p1->num)
{
//赋值前,学生总数加1
n++;
//通过学生总数来判断链表是否为空
if(1 == n)
{
//如果是空,直接将正在输入的指针赋给头指针
head = p1;
}
else
{
//从这里往下逻辑有点乱


p2->next = p1;
}
//p1的值赋给p2 ,此时p1把p2又复盖了?
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("Please enter the Nun: ");
scanf("%d", &p1->num);
printf("Please enter the Score: ");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧