#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Student
{
int number;
char *name;
char *addr;
int math;
int computer;
struct Student *next;
};
struct Student *create_head(struct Student *head)
{
head=NULL;
return head;
}
//增加学生
void add_stu(struct Student **head,int number,char *name,char *addr,int math,int computer)
{
struct Student *p=(struct Student *)malloc(sizeof(struct Student));
struct Student *p2=*head;
if(p==NULL)
{
printf("内存空间分配失败!\n");
exit(0);
}
p->number=number;
p->name=(char *)malloc(strlen(name)+1);
strcpy(p->name,name);
p->addr=(char *)malloc(strlen(addr)+1);
strcpy(p->addr,addr);
p->math=math;
p->computer=computer;
p->next=NULL;
if(*head==NULL)
{
*head=p;
return;
}
else
{
while(p2->next!=NULL)
{
p2=p2->next;
}
p2->next=p;
}
return;
}
//打印学生
void print_stu(struct Student *head)
{
struct Student *p=head;
if(head==NULL)
{
printf("链表为空!\n");
return;
}
else
{
while(p!=NULL)
{
printf("学号:%d\t姓名:%s\t地址:%s\t数学成绩:%d\t计算机成绩:%d\n",p->number,p->name,p->addr,p->math,p->computer);
p=p->next;
}
}
printf("\n");
return;
}
//根据姓名删除
void delete_by_name(struct Student *head,char *name)
{
struct Student *p=head;
struct Student *p2;
if(head==NULL)
{
return;
}
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
p2->next=p->next;
free(p->addr);
free(p->name);
free(p);
break;
}
else
{
p2=p;
p=p->next;
}
}
return;
}
//删除所有
void delete_all(struct Student **head)
{
struct Student *p=*head;
if(*head==NULL)
{
printf("链表为空!\n");
return;
}
while((*head)!=NULL) //你原本的链表并没有虚的头结点,但是你总是从第二个结点删起所以会剩一个结点。这里改为总是删除头结点就可以将链表完全删除
{
p=(*head)->next;
free((*head)->addr);
free((*head)->name);
free(*head);
*head=p;
}
return;
}
//根据姓名打印
void print_name(struct Student *head,char *name)
{
struct Student *p=head;
int flag=1;
if(head==NULL)
{
printf("链表为空!\n");
return;
}
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
flag=0;
printf("学号:%d\t姓名:%s\t地址:%s\t数学成绩:%d\t计算机成绩:%d\n",p->number,p->name,p->addr,p->math,p->computer);
}
p=p->next;
}
if(flag)
{
printf("没有 %s 的信息!\n",name);
}
}
int main()
{
int j=0;
int snum,smath,scomputer;
char sname[20]; //如果定义成指针,输入时会出现错误
char saddr[40];
int number1=1111;
char *name1="zhangsan";
char *addr1="yinchuan";
int math1=88;
int computer1=73;
struct Student *head=create_head(head);
add_stu(&head,number1,name1,addr1,math1,computer1);
add_stu(&head,1112,"lisi","shanghai",92,83);
add_stu(&head,1113,"wangwu","beijing",85,91);
add_stu(&head,1113,"lili","luoyang",89,98);
print_stu(head);
// delete_by_name(head,"lisi");
//print_stu(head);
//delete_all(&head);
//print_stu(head);
//print_name(head,"lisiwww");
printf(" 1) 输入1增加学生\n 2) 输入2打印学生\n 3) 输入3 根据姓名打印学生\n 4) 输入4根据姓名删除学生\n 5) 输入5删除所有学生\n 6) 输入其他数字退出:\n");
scanf("%d",&j);
switch(j)
{
case 1:
printf("请输入学号(int),姓名(char),地址(char),数学成绩(int),计算机成绩(int)\n");
scanf
("%d%s%s%d%d",&snum,sname,saddr,&smath,&scomputer);
//字符串的输入不能用逗号隔开
add_stu(&head,snum,sname,saddr,smath,scomputer);
break;
case 2:
print_stu(head);
//打印学生
break;
case 3:
scanf("%s",sname);
print_name(head,sname);
break;
case 4:
scanf("%s",sname);
delete_by_name(head,sname);
print_stu(head);
break;
case 5:
delete_all(&head);
print_stu(head);
break;
default:
exit(0);
break;
}
return 0;
}