#include<stdio.h>
#include<string.h>
#include <malloc.h>
#include<stdlib.h>
struct node
{
int num;
char name[15];
node *next;
};
node *create()
{
struct node * head,* p,* q;
int number;
char sname[15];
head=(node *)malloc(sizeof(node));
if(head==NULL)
{printf("创建失败!!");
}
else
{
number=1;
printf("请输入姓名:");
scanf("%s",&sname);
head->num=number;
strcpy(head->name,sname);
head->next=head;
p=head;
while(1)
{
number++;
q=(node *)malloc(sizeof(node));
printf("请输入姓名(输入0时输入结束):");
scanf("%s",&sname);
if(strcmp(sname,"0")==0)
{
break;
}
q->num=number;
strcpy(q->name,sname);
q->next=p->next;
p->next=q;
p=q;
}
return head;
}
}
node * delete_name(node *head)
{
node *b,*p,*m,*q;
p=head;
char qname[15];
printf("请输入要删除学生姓名:");
scanf("%s",&qname);
while(1)
{
if(p==NULL)
{
return head;
}
if(strcmp(p->name,qname)==0)
{
if(p==head&&p->next==head)
{
head=NULL;
free(p);
return head;
}
else if(p==head)
{
p=head;
q=p->next;
m=q;
while(m->next!=head)
{
m=m->next;
}
head=q;
m->next=head;
free(p);
return head;
}
else
{
b->next=p->next;
free(p);
return head;
}
}
b=p;
p=p->next;
if(p==head)
break;
}
return head;
}
node * delete_i(node *head)
{
node *b,*p,*m,*q;
int j=1,i;
p=head;
printf("请输入要第几个删除学生:");
scanf("%d",&i);
while(p!=NULL&&j<i)
{
b=p;
p=p->next;
if(p==head)
break;
j++;
}
if(j==i&&i==1&&p->next==head)
{
head=NULL;
free(p);
}
else if(j==i&&i==1)
{
p=head;
q=p->next;
m=q;
while(m->next!=head)
{
m=m->next;
}
head=q;
m->next=head;
free(p);
return head;
}
else if(j==i)
{
b->next=p->next;
free(p);
}
return head;
}
int query_name(node * head)
{
struct node * p;
char sname[20];
p=head;
printf("请输入你要查找的姓名:");
scanf("%s",&sname);
while(p!=NULL)
{
if(strcmp(p->name,sname)==0)
{
printf(" %d %s\n",p->num,p->name);
}
p=p->next;
if(p==head)
break;
}
printf("没有找到!!!\n");
return 0;
}
int query_i(node * head)
{
struct node * p;
int i,j=1;
p=head;
printf("请输入你要查找的学号:");
scanf("%d",&i);
for(;j<i;j++)
{
p=p->next;
if(p==head)
break;
}
if(j==i)
{
printf(" %d %s\n",p->num,p->name);
return 1;
}
printf("没有找到!!!\n");
return 0;
}
int install_name(node * head)
{
struct node *p,*n;
p=head;
n=(node *)malloc(sizeof(node));
int number;
char sname[15],qname[15];
printf("请输入学号,姓名:");
scanf("%d",&number);
scanf("%s",&sname);
n->num=number;
strcpy(n->name,sname);
n->next=NULL;
printf("请输入要插入到那位学生姓名后面:");
scanf("%s",&qname);
while(p!=NULL)
{
if(strcmp(p->name,qname)==0)
{
p->next=n->next;
p->next=n;
return 1;
}
p=p->next;
if(p==head)
break;
}
printf("没有找到该学生!!\n");
return 0;
}
int install_i(node * head)
{
struct node *p,*n;
p=head;
n=(node *)malloc(sizeof(node));
int number,inno,j=1;
char sname[15];
printf("请输入学号,姓名:");
scanf("%d",&number);
scanf("%s",&sname);
n->num=number;
strcpy(n->name,sname);
n->next=NULL;
printf("请输入要插入到第几位学生姓名后面:");
scanf("%d",&inno);
while(p!=NULL&&j<inno)
{
p=p->next;
if(p==head)
break;
j++;
}
if(j==inno)
{
n->next=p->next;
p->next=n;
return 1;
}
printf("没有找到该学生!!\n");
return 0;
}
void printf(node * head)
{
struct node *q;
q=head;
while(head!=NULL)
{
printf("%d %s\n",q->num,q->name);
q=q->next;
if(q==head)
break;
}
}
int main()
{
struct node * head;
char name[15];
int m;
printf("\n*********学生成绩管理系统(循环链表)***********\n");
while(1)
{
printf("\n * 输入信息请按1 *\n");
printf(" * 根据姓名查找学生信息请按2 *\n");
printf(" * 根据学号查找学生信息请按3 *\n");
printf(" * 根据姓名删除学生信息请按4 *\n");
printf(" * 根据学号删除学生信息请按5 *\n");
printf(" * 插入学生信息到某姓名学生之后请按6 *\n");
printf(" * 插入学生信息到第几位学生之后请按7 *\n");
printf(" * 输出所有学生信息请按8 *\n");
printf(" * 退出请按0 *\n\n");
printf("请选择操作:");
scanf("%d",&m);
if(m==1)
head=create();
if(m==2)
query_name(head);
if(m==3)
query_i(head);
if(m==4)
head=delete_name(head);
if(m==5)
head=delete_i(head);
if(m==6)
install_name(head);
if(m==7)
install_i(head);
if(m==8)
printf(head);
if(m==0)
break;
}
system("pause");
}