C语言的链表学习,以前刚学习C语言的时候是写的。里面存在的很多不足,放这里做个记录。能对刚学习的道友有帮助就更好,老道友们欢迎指出错误。
#include<stdio.h>
#include<string.h>
struct stu
{
unsigned int xh;
unsigned char name[10];
unsigned char ywcj;
unsigned char sxcj;
unsigned char yycj;
unsigned char pjcj;
unsigned int zcj;
struct stu *next;
}*head;
struct stu *creat(unsigned char n);
void cx(struct stu *head,unsigned int xh);
struct stu *dele(struct stu *head,unsigned int xh);
struct stu *sr(struct stu *head);
void xs(struct stu *head);
main()
{
unsigned char k;
unsigned int g;
while(1)
{
printf("**********************************\n");
printf(" 1.成绩录入 \n");
printf(" 2.查询信息 \n");
printf(" 3.删除信息 \n");
printf(" 4.插入信息 \n");
printf(" 5.显示信息 \n");
printf("**********************************\n");
printf("请选择你的操作 ");
scanf("%d",&k);
switch(k)
{
case 1:
head=creat(6);break;
case 2:
printf("请输入你要查找的学号");
scanf("%d",&g);
cx(head,g);
break;
case 3:
printf("请输入你要删除的学号");
scanf("%d",&g);
head=dele(head,g);
break;
case 4:head=sr(creat(1));
break;
case 5:xs(head);break;
default:;
}
}
}
struct stu *creat(unsigned char n)
{
struct stu *pb,*head,*pf;
unsigned char i;
for(i=0;i<n;i++)
{
pb=(struct stu*)malloc(sizeof(struct stu));
printf("请输入学号 ");
scanf("%s",&pb->xh);
printf("请输入姓名 ");
scanf("%s",&pb->name);
printf("请输入语文成绩 ");
scanf("%d",&pb->ywcj);
printf("请输入数学成绩 ");
scanf("%d",&pb->sxcj);
printf("请输入英语成绩 ");
scanf("%d",&pb->yycj);
printf("请输入平均成绩 ");
scanf("%d",&pb->pjcj);
printf("请输入总成绩 ");
scanf("%d",&pb->zcj);
printf("_______________________\n");
if(i=0)
{
head=pb=pf;
}
else
{
pb->next=pb;
}
pf=pb;
}
return head;
}
void cx(struct stu *head,unsigned int xh)
{
struct stu *p;
p=head;
while(p->xh!=xh&&p->next!=0)
{
p=p->next;
}
if(p!=0)
{
printf("该学生的信息如下\n");
printf("学号: %d\n",p->xh);
printf("姓名: %s\n",p->name);
printf("语文成绩: %d\n",p->ywcj);
printf("数学成绩: %d\n",p->sxcj);
printf("英语成绩: %d\n",p->yycj);
printf("平均成绩: %d\n",p->pjcj);
printf("总成绩: %d",p->zcj);
}
else
{
printf("没有找到该学号学生的信息");
}
}
struct stu *dele(struct stu *head,unsigned int xh)
{
struct stu *p,*pf;
p=head=pf;
while(p->xh!=xh&&p->next!=0)
{
pf=p;
p=p->next;
}
if(p!=0)
{
if(p=head)
{
head=p->next;
}
else
{
pf->next =p->next ;
}
}
else
{
printf("没有找到符合条件的学生");
}
return head;
}
struct stu *sr(struct stu *head)
{
struct stu *p;
p->next=head;
p=head;
return head;
}
void xs(struct stu *head)
{
struct stu *p;
p=head;
while(p!=0)
{
printf("------------------------------------");
printf("学号: %d\n",p->xh);
printf("姓名: %s\n",p->name);
printf("语文成绩: %d\n",p->ywcj);
printf("数学成绩: %d\n",p->sxcj);
printf("英语成绩: %d\n",p->yycj);
printf("平均成绩: %d\n",p->pjcj);
printf("总成绩: %d",p->zcj);
p=p->next;
}
}