** 用链表做学生管理器**
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define NULL 0
int len;
struct student
{
char name[20];
int score;
struct student *next;
};
struct student *creadlist(int len)
{
struct student *head,*p,*q;
for(int i=0;i<len;i++)
{
p=(struct student *)malloc(sizeof(struct student));
if(i==0)
head=p;
else
q->next=p;
q=p;
scanf("%s%d",p->name,&p->score);
}
q->next=NULL;
return head;
}
struct student *insert(struct student *head)
{
struct student *p,*temp;
p=head;
temp=(struct student *)malloc(sizeof(struct student));
int n,i;
scanf("%d%s%d",&n,temp->name,&temp->score);
if(n==1)
{
temp->next=head;
head=temp;
}
else
{
i=1;
while(i<n-1)
{
p=p->next;
i++;
}
temp->next=p->next;
p->next=temp;
}
len++;
return head;
}
struct student *remove(struct student *head)
{
struct student *p;
p=head;
int n;
scanf("%d",&n);
if(n==1)
{
head=p->next;
}
else
{
int i=1;
while(i<n-1)
{
p=p->next;
i++;
}
p->next=p->next->next;
}
len--;
return head;
}
struct student *revise(struct student *head)
{
struct student *p,*temp;
p=head;
int n;
scanf("%d",&n);
if(n==1)
{
scanf("%s%d",p->name,&p->score);
}
else
{
int i=1;
while(i<n-1)
{
p=p->next;
i++;
}
temp=p->next;
scanf("%s%d",temp->name,&temp->score);
}
return head;
}
struct student *sort(struct student *head)
{
struct student *p,*q,temp;
for(p=head;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(strcmp(p->name,q->name)>0)
{
temp.score=p->score;
p->score=q->score;
q->score=temp.score;
strcpy(temp.name,p->name);
strcpy(p->name,q->name);
strcpy(q->name,temp.name);
}
}
}
return head;
}
void seek(struct student *head)
{
struct student *p,*temp;
p=head;
int n;
scanf("%d",&n);
printf("姓名\t得分\n");
if(n==1)
{
printf("%s\t%d\n",p->name,p->score);
}
else
{
int i=1;
while(i<n-1)
{
p=p->next;
i++;
}
temp=p->next;
printf("%s\t%d\n",temp->name,temp->score);
}
}
void output(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%s\t%d\n",p->name,p->score);
p=p->next;
}
}
void main()
{
system("color 3e");
SetConsoleTitle(L"链表学生信息管理系统");
struct student *head;
int n;
while(1)
{
printf("1_输入学生信息 \n2_输出全部学生的信息 \n3_查找学生信息 \n4_插入学生信息 \n5_删除学生信息 \n6_修改学生信息 \n7_排序学生信息(姓名的排序) \n8_退出学生信息管理器 \n请输入你的选择:\n");
scanf("%d",&n);
if(n==1)
{
printf("请输入要创建的长度:");
scanf("%d",&len);
printf("姓名\t得分\n");
head=creadlist(len);
}
if(n==2)
{
printf("姓名\t得分\n");
output(head);
}
if(n==3)
{
printf("请输入你要查找的节点:");
seek(head);
}
if(n==4)
{
printf("请输入你要插入的节点:");
head=insert(head);
}
if(n==5)
{
printf("请输入你要删除的节点:");
head=remove(head);
}
if(n==6)
{
printf("请输入你要修改的节点:");
head=revise(head);
}
if(n==7)
{
head=sort(head);
printf("排序成功");
}
if(n==8)
{
exit(0);
}
system("pause");
system("cls");
}
}