严蔚敏版数据结构建立学生信息单链表C语言版适合VC++.doc
文本预览下载声明
#include stdio.h
#include malloc.h
#includestdlib.h
typedef struct Student/*定义学生类 */
{
int num;
char name[20];
char sex[2];
int age;
float grade;
}stu;
typedef struct LNode
{
stu data;
struct LNode *next;
}LNode,* Linklist;
Linklist InitList_L(Linklist L)/*构造一个空的单向链表*/
{
L=(Linklist)malloc(sizeof(stu));
if(!L)
printf(ERROR\n);
else
{
L=NULL;
printf(OK\n);
return L;
}
}
void DestroyList_L(Linklist L)//销毁单向链表 */
{
Linklist p;
if(!L)
printf(ERROR\n);
else
{
while(L)
{
p=L;
L=L-next;
free(p);
}
printf(OK\n);
}
}
void ClearList_L(Linklist L)/*将L重置为空表*/
{
Linklist p;
if(!L)
printf(ERROR\n);
else
{
while(L-next)
{
p=L-next;
L-next=p-next;
free(p);
}
printf(OK\n);
}
}
void ListEmpty_L(Linklist L)/*L为空表返回TRUE,否则返回FALSE*/
{
if(!L)
printf(ERROR\n);
else
{
if(!L-next)
printf(TRUE\n);
else
printf(FLASE\n);
}
}
int ListLength_L(Linklist L)/*返回L中数据元素个数*/
{
int i=0;
Linklist p=L;
if(!L)
return 0;
else
{
while(p)
{
i++;
p=p-next;
}
return i;
}
}
void GetElem_L(Linklist L,int i)//返回第i个元素的值 */
{
Linklist p=L;
int j=1;
while(p!=NULLji)
{
p=p-next;
++j;
}
if(!p||ji)
printf(ERROR\n);
else
{
printf(学生%d的数据为 :\n,i);
printf(学号:%d\n姓名:%s\n性别:%s\n年龄:%d\n成绩:%f\n,p-data.num,p-data.name,p-data.sex,p-data.age,p-data.grade);
printf(OK\n);
}
}
void PriorElem_L(Linklist L,stu cur_e)/*用pre_e返回cur_e的前驱 */
{
Linklist p=L;
while(p-next(p-next-data).num!=cur_e.num)
p=p-next;
if(!p-next)
printf(ERROR\n);
else
{
printf(cur_e的前驱为:\n);
printf(num:%d\nname:%s\nsex:%s\nage:%d\ngrade:%f\n,p-data.num,p-data.name,p-data.sex,p-data.age,p-data.grade);
printf(OK\n);
}
}
void NextElem_L(Linklist L,stu cur_e)/*用next_e返回cur_e的后驱*/
{
Linklist p=L;
while(p(p-data).num!=cur_e.num)
p=p-next;
if(!p||!p-next)
printf(ERROR\n);
else
{
printf(cur_e的后驱为:\n);
printf(num:%d\nname:%s\nsex:%s\nage:%d\ngrade:%f\n,p-next-data.num,p-next-data.name,p-next-data.sex,p-next-d
显示全部