【新手求教】建立单向动态链表程序的若干有关问题,请各位大神详解
【新手求教】建立单向动态链表程序的若干问题,请各位大神详解
学到结构体的链表这块了,但是创建单向动态链表的代码让我有点头晕,请各位大侠指教:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student *creat(); 问题1.这句话是什么意思?是声明一个类型为struct
student的creat函数吗?那为什么creat前面要加*?
而且这个函数为什么也没有参数?
void print(struct student *head);
struct student
{
int num;
float score;
struct student *next;
};
int n; //全局变量,用来记录存放了多少数据。
void main()
{
struct student *stu; 分析1:这里是定义一个结构体指针变量stu
stu = creat(); 问题2:这里就是将结构体指针变量stu指向creat函数返回
值的首地址?
print( stu );
printf("\n\n");
system("pause");
}
struct student *creat() 问题3:重点:为啥creat函数的定义前面还要加*
{
struct student *head;
struct student *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN); 分析2:这句话的意思是利用malloc函数给
struct student这个结构体类型分配动态存储
空间,然后malloc函数返回的是这个分配的动
态空间的首地址,这个首地址再被(struct
学到结构体的链表这块了,但是创建单向动态链表的代码让我有点头晕,请各位大侠指教:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student *creat(); 问题1.这句话是什么意思?是声明一个类型为struct
student的creat函数吗?那为什么creat前面要加*?
而且这个函数为什么也没有参数?
void print(struct student *head);
struct student
{
int num;
float score;
struct student *next;
};
int n; //全局变量,用来记录存放了多少数据。
void main()
{
struct student *stu; 分析1:这里是定义一个结构体指针变量stu
stu = creat(); 问题2:这里就是将结构体指针变量stu指向creat函数返回
值的首地址?
print( stu );
printf("\n\n");
system("pause");
}
struct student *creat() 问题3:重点:为啥creat函数的定义前面还要加*
{
struct student *head;
struct student *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN); 分析2:这句话的意思是利用malloc函数给
struct student这个结构体类型分配动态存储
空间,然后malloc函数返回的是这个分配的动
态空间的首地址,这个首地址再被(struct