已知链表的结点如下:
struct Student
{ long num;
float score;
struct Student*next;
};
(1) 链表生成
要求设计函数
struct Student * creatStuLink(long int *, float *,int n)
{//提示要动态的创建链表。
}
功能是给定学号和分数两个数组,根据数组创建链表,并返回头结点。
例如 主函数中有学生学号和对应成绩如下:
long int nu[5]={9220101, 9220102, 9220108, 9220111, 9220115};
float sc[5]={98, 77, 67.5, 88.5, 89};
struct Student * Head;
当在调用 creatStuLink 后
Head= creatStuLink(nu, sc,5);
Head 为含五个学生信息的链表的头结点,最后一个结点的 next 要为 NULL。
C 语言实验上机指导 实验 7 结构体
(2) 链表结点内存释放
知某链表的头结点为 Head
类型为:struct Student *
请设计函数
struct Student *releaseStuLink(struct Student *pHead)
功能是将动态创建的链表 pHead,所有结点依次释放(用 free),成功释放后 要打印输出该结
点的学号和成绩。
#include<stdio.h>
#include<stdlib.h>
typedef struct Student{
long num;
float score;
struct Student *next;
}Stu;
typedef struct _list{
Stu *head;
Stu *tail;
}List;
Stu *creatStuLink(long int*,float*,int n);
Stu *releaseStuLink(Stu *pHead);
int main()
{
int n=5;
long int nu[5]={9220101,92201002,9220108,9220111,9220115};
float sc[5]={98,77,67.5,88.5,89};
Stu *Head;
Head=creatStuLink(nu,sc,n);
Head=releaseStuLink(Head);
return 0;
}
//当在调用 creatStuLink 后
//Head= creatStuLink(nu, sc,5);
//Head 为含五个学生信息的链表的头结点,最后一个结点的 next 要为 NULL。
Stu *creatStuLink(long int* nu,float* sc,int n)
{
List list;
list.head=NULL;//先定义头结点
while(n--)
{
Stu*p=(Stu*)malloc(sizeof(Stu));
p->num=nu[5-n];
p->score=sc[5-n];
p->next=NULL;//最后一个结点的next要为NULL
if(n=5)list.head=p;
Stu *last=list.head;
if(last){
while(last->next){
last=last->next;
}
last->next=p;
}
}
return list.head;//返回头结点
}
//功能是将动态创建的链表 pHead,所有结点依次释放(用 free),成功释放后 要打印输出该结
//点的学号和成绩
Stu *releaseStuLink(Stu *pHead)
{
Stu *p,*q;
q=pHead->next;
p=q;
printf("%d %f\n",pHead->num,pHead->score);
free(pHead);
for(;p;p=q)
{
q=p->next;
printf("%d %f\n",p->num,p->score);
free(p);
}
return NULL;
}
修改如下,供参考:
#include<stdio.h>
#include<stdlib.h>
typedef struct Student {
long num;
float score;
struct Student* next;
}Stu;
typedef struct _list {
Stu* head;
Stu* tail;
}List;
Stu* creatStuLink(long int*, float*, int n);
Stu* releaseStuLink(Stu* pHead);
int main()
{
int n = 5;
long int nu[5] = { 9220101,92201002,9220108,9220111,9220115 };
float sc[5] = { 98,77,67.5,88.5,89 };
Stu* Head;
Head = creatStuLink(nu, sc, n);
Head = releaseStuLink(Head);
return 0;
}
//当在调用 creatStuLink 后
//Head= creatStuLink(nu, sc,5);
//Head 为含五个学生信息的链表的头结点,最后一个结点的 next 要为 NULL。
Stu* creatStuLink(long int* nu, float* sc, int n)
{
List list;
list.head = NULL;//先定义头结点
while (n) //(n--) 修改
{
Stu* p = (Stu*)malloc(sizeof(Stu));
p->num = nu[5 - n];
p->score = sc[5 - n];
p->next = NULL;//最后一个结点的next要为NULL
if (n == 5) //if (n = 5) 修改
list.head = p;
else //修改
list.tail->next = p; //修改
list.tail = p; //修改
//Stu* last = list.head; 修改
//if (last) {
// while (last->next) {
// last = last->next;
// }
// last->next = p;
//}
n--; //修改
}
return list.head;//返回头结点
}
//功能是将动态创建的链表 pHead,所有结点依次释放(用 free),成功释放后 要打印输出该结
//点的学号和成绩
Stu* releaseStuLink(Stu* pHead)
{
Stu* p, * q;
q = pHead; //q = pHead->next;修改
//p = q;
//printf("%d %f\n", pHead->num, pHead->score);
//free(pHead);
while (q) //for (; q; p = q) 修改
{
p = q; //修改
q = q->next;//修改
printf("%d %f\n", p->num, p->score);
free(p);
}
return NULL;
}
41行的if(n=5)改成if(n==5)再试试,不然是死循环。