📄 新建 文本文档.txt
字号:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct date {int month;int day;int year;};
struct student
{
int num;
char name[20];
int tel;
struct date birthday;
int dor;
char addr[30];
int math;
int Eng;
int phy;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc (LEN);
printf("num:"); scanf("%d",&p1->num);
printf("name:"); scanf("%s",p1->name);
printf("tel:"); scanf("%d",&p1->tel);
printf("year:"); scanf("%d",&p1->birthday.year);
printf("month:"); scanf("%d",&p1->birthday.month);
printf("day:"); scanf("%d",&p1->birthday.day);
printf("dor:"); scanf("%d",&p1->dor);
printf("addr:"); scanf("%s",p1->addr);
printf("math:"); scanf("%d",&p1->math);
printf("Eng:"); scanf("%d",&p1->Eng);
printf("phy:"); scanf("%d",&p1->phy);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
printf("num:"); scanf("%d",&p1->num);
printf("name:"); scanf("%s",p1->name);
printf("tel:"); scanf("%d",&p1->tel);
printf("year:"); scanf("%d",&p1->birthday.year);
printf("month:"); scanf("%d",&p1->birthday.month);
printf("day:"); scanf("%d",&p1->birthday.day);
printf("dor:"); scanf("%d",&p1->dor);
printf("addr:"); scanf("%s",p1->addr);
printf("math:"); scanf("%d",&p1->math);
printf("Eng:"); scanf("%d",&p1->Eng);
printf("phy:"); scanf("%d",&p1->phy);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p1;
printf("\n now,these %d students' information are:\n",n);
p1=head;
if(head!=NULL)
do
{printf("num:%d ",p1->num);
printf("name:%s ",p1->name);
printf("tel:%d ",p1->tel);
printf("year:%d",p1->birthday.year);
printf("month:%d",p1->birthday.month);
printf("day:%d",p1->birthday.day);
printf("dor:%d ",p1->dor);
printf("addr:%s ",p1->addr);
printf("math:%d ",p1->math);
printf("Eng:%d ",p1->Eng);
printf("phy:%d\n",p1->phy);
p1=p1->next;
}while(p1!=NULL);
}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
if(head==NULL)
{printf("\nlise null!\n");return(NULL);}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(num==p1->num)
{if(p1==head)head=p1->next;
else p2->next=p1->next;
printf("delete:%d\n",num);
n=n-1;
}
else printf("%d not been found!\n",num);
return(head);
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}
if(p0->num<=p1->num)
{if(head==p1)head=p0;
else p2->next=p0;p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
return(head);
}
struct student * search(struct student *head,int num)
{struct student *p1,*p2;
if(head==NULL)printf("\nlist null!\n");
p1=head;
while(num!=p1->num&&p1->next!=NULL){p2=p1;p1=p1->next;}
if(num==p1->num)
{if(p1==head)p1->next=NULL;
else {head=p2->next;p1->next=NULL;}}
else printf("%ld student is not exsit!\n",num);
return(head);
}
struct student * edit(struct student *head,struct student *stud)
{struct student *p0,*p1,*p2;
p1=head;p0=stud;
if(head==NULL)printf("\nlist null!\n");
else
{ while(p0->num!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(p0->num==p1->num)
{if(head==p1){head=p0;p0->next=p1->next;}
else {p2->next=p0;p0->next=p1->next;} }
else printf("This student is not exsit!\n");
return(head);}
}
void main()
{
struct student *head,p1,*stu;int sel,del_num,num;
while(1)
{printf("===================main menu=================\n");
printf("=== 1.creat a new linked lise. ===\n");
printf("=== 2.these students' infornation are:===\n");
printf("=== 3.insert a new student. ===\n");
printf("=== 4.deldte a student. ===\n");
printf("=== 5.search a student's infornation ===\n");
printf("=== 6.edit a student. ===\n");
printf("=== 0.exit. ===\n");
printf("=============================================\n");
printf("please input a number(0~4)?");
scanf("%d",&sel);
if(sel==0)break;
switch(sel)
{case 1:
printf("input records:\n");
head=creat();
break;
case 2:
printf("these students' infornation are:\n");
print(head);
break;
case 3:
printf("please input the iserted record:\n");
printf("num:"); scanf("%d",&p1.num);
printf("name:"); scanf("%s",p1.name);
printf("tel:"); scanf("%d",&p1.tel);
printf("year:"); scanf("%d",&p1.birthday.year);
printf("month:"); scanf("%d",&p1.birthday.month);
printf("day:"); scanf("%d",&p1.birthday.day);
printf("dor:"); scanf("%d",&p1.dor);
printf("addr:"); scanf("%s",p1.addr);
printf("math:"); scanf("%d",&p1.math);
printf("Eng:"); scanf("%d",&p1.Eng);
printf("phy:"); scanf("%d",&p1.phy);
head=insert(head,&p1);
print(head);
break;
case 4:
printf("\ninput the deleted number:");
scanf("%d",&del_num);
head=del(head,del_num);
print(head);
break;
case 5:
printf("\ninput the search number:");
scanf("%d",&num);
head=search(head,num);
printf("This student's record:\n");
print(head);
break;
case 6:
printf("\ninput the edited record:\n");
stu=(struct student *)malloc(LEN);
printf("num:"); scanf("%d",&p1.num);
printf("name:"); scanf("%s",p1.name);
printf("tel:"); scanf("%d",&p1.tel);
printf("year:"); scanf("%d",&p1.birthday.year);
printf("month:"); scanf("%d",&p1.birthday.month);
printf("day:"); scanf("%d",&p1.birthday.day);
printf("dor:"); scanf("%d",&p1.dor);
printf("addr:"); scanf("%s",p1.addr);
printf("math:"); scanf("%d",&p1.math);
printf("Eng:"); scanf("%d",&p1.Eng);
printf("phy:"); scanf("%d",&p1.phy);
head=edit(head,stu);
print(head);break;
default:
printf("Your selecation is wrong.\n");
}
printf("\n press any key,continue...\n");
getch();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -