|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- /************************************创建一个有n个节点的动态链表***********************************************/
- #include<stdio.h>
- #include<stdlib.h>
- #include<malloc.h>
- #include<string.h>
- #define LEN sizeof(struct student)
- int n; // 定义n为全局变量
- struct student *head;
- struct student *creat(void);
- int print(struct student *head);
- struct student *del(struct student * head);
- struct student *Insert(struct student * head);
- int searbynum(struct student * head);
- int searbyname(struct student *head);
- void menu();
- struct student
- {
- int num;
- int age;
- char sex[20];
- char name[20];
- struct student *next;
-
- };
- int main()
- {
-
- char choose_1;
- int choose_2;
-
- menu();
- scanf("%c",&choose_1);
-
-
- switch(choose_1)
- {
- case 'A': {
- creat();
- break;
- }
-
- case 'B': {
- print(head);
- break;
- }
-
- case 'C': {
- del(head);
- printf("The information after delete is :\n");
- print(head);
- break;
- }
-
- case 'D': {
- Insert(head);
- printf("The information after delete is :\n");
- print(head);
- break;
- }
-
- case 'E': {
- printf("1,search by number . 2,search by name .\n");
- printf("please input your choice !\n");
- scanf("%d",&choose_2);
- switch(choose_2)
- {
- case 1 :searbynum(head);break;
- case 2 :searbyname(head);break;
- }
- break;
- }
-
- case 'F': {
- exit(1);
- break;
- }
-
- default :{
- printf("You input a wrong choose !\n");
- break;
-
- }
-
- }
-
- return 0;
- }
-
- /*********************************************主菜单函数*****************************************************************/
- void menu()
- {
- printf("\t\tWelcome to go into our system !\n");
-
- printf("**************************************************************\n*\n*\n");
- printf("* A : Input information ! B : Output information ! *\n*\n");
- printf("* C : Delete information! D : Insert information ! *\n*\n");
- printf("* E : Search information! F : Exit ! *\n*\n*\n");
- printf("**************************************************************\n\n\n");
-
- printf("please input the operation you choose !");
-
- }
- /******************************************创建链表*********************************************************/
- struct student *creat(void)
- {
- int i;
- struct student *p1,*p2;
-
- printf("please input the total number of student :\n");
- scanf("%d",&n);
-
- p1=p2=(struct student *)malloc(LEN);
-
- printf("please input the 1 student's num,age,sex and name :\n");
- scanf("%d,%d,%s,%s",&p1->num,&p1->age,p1->sex,p1->name);
-
- head=NULL;
- i=1;
-
- if(n==1)
- {
- head=p1;
- p2=p1;
- }
-
- else
- do
- {
- if(i==1) head=p1;
- else
- p2->next=p1;
-
- p2=p1;
-
- p1=(struct student *)malloc(LEN);
-
- printf("please input the %d student's num,age,sex and name :\n",i+1);
- scanf("%d,%d,%s,%s",&p1->num,&p1->age,p1->sex,p1->name);
-
- i=i+1;
- }while(i<n);
- p2->next=NULL;
- return (head);
-
- }
- /*********************************打印链表*********************************************/
- int print(struct student *head)
- {
- struct student *p;
- printf("\nThere are %d records!\n\n" ,n);
- p = head;
-
-
- for(;p!=NULL;p=p->next)
- printf("%d,%d,%s,%s",p->num,p->age,p->sex,p->name);
- return 0;
- }
- /***********************************************删除链表中某节点*************************************************************/
- struct student *del(struct student *head)
- {
- struct student *p1,*p2;
- int number;
-
- p1=head;
-
- if(head==NULL)
- {
- printf("this is a null linklist !");
- exit(0);
- }
-
- else
- {
- p1=head;
-
- printf("please input the num of the student you want to delete :\n");
- scanf("%d",&number);
-
- while(number!=p1->num&&p1!=NULL)
- {
- p2=p1;
- p1=p1->next;
- }
- if(p1==NULL)
- printf("There is no node you want to delete!");
- else
- {
- if(p1==head)
- head=p1->next;
- else
- p2->next=p1->next;
- }
-
- }
-
- n=n-1;
-
- return (head);
- }
- /**********************************************插入节点**************************************************************/
- struct student *Insert(struct student * head)
- {
- struct student *p1,*p2,*p;
- p=NULL;
-
- printf("please input the student's num,age,sex and name you want to insert :\n");
- scanf("%d,%d,%s,%s",&p->num,&p->age,p->sex,p->name);
-
- p1=p2=head;
-
-
- if(NULL==head) head=p;
- else
- {
- while (p->num>p1->num&&p1->next!=NULL)
- {
- p1=p2;
- p1=p1->next;
- }
-
- if (p->num<=p1->num)
- {
- if(head==p1)
- {
- head=p;
- p->next=p1;
- }
- else
- {
- p2->next=p;
- p->next=p1;
- }
- }
- else
- {
- p1->next=p;
- p->next=NULL;
- }
-
-
- }
-
- n=n+1;
-
-
- return (head);
- }
- /**************************************通过学号查找**************************************************************************/
- int searbynum(struct student * head)
- {
- struct student *p1,*p2,*p;
- p=NULL;
- printf("please input the number of the student you want to search :\n");
- scanf("%d",&p->num);
-
- p1=head;
-
-
- if(NULL==p1)
- printf("This is a null linklist !");
-
- else
- {
- while(p->num!=p1->num&&p1->next!=NULL)
- {
- p2=p1;
- p1=p1->next;
- }
- if(NULL==p1->next)
- {
- if(p->num!=p1->num)
- printf("There is no student you want to search\n\n !");
-
- else
- {
- printf("Searching successful !\n");
- printf("%d,%d,%s,%s\n\n\n",p1->num,p1->age,p1->sex,p1->name);
- }
- }
-
- else
- printf("Searching successful !\n");
- printf("%d,%d,%s,%s\n\n\n",p1->num,p1->age,p1->sex,p1->name);
-
- }
- return 0;
-
- }
- /*********************************************通过姓名查找*****************************************************************/
- int searbyname(struct student *head)
- {
- struct student *p1,*p2,*p;
-
- p=NULL;
- printf("please input the name of the student you want to search :\n");
- scanf("%s",p->name);
-
- p1=head;
-
-
- if(NULL==p1)
- printf("This is a null linklist !\n");
-
- else
- {
- while((strcmp(p->name,p1->name)!=0)&&p1->next!=NULL)
- {
- p2=p1;
- p1=p1->next;
- }
-
- if(NULL==p1->next)
- {
- if(strcmp(p->name,p1->name)!=0)
- printf("There is no student you want to search !\n");
-
- else
- {
- printf("Searching successful !!\n");
- printf("%d,%d,%s,%s",p1->num,p1->age,p1->sex,p1->name);
- }
- }
- else
- {
- printf("Searching successful !!\n");
- printf("%d,%d,%s,%s",p1->num,p1->age,p1->sex,p1->name);
- }
- }
-
- return 0;
- }
- /******************************修改学生信息(通过学号查找)*************************************************/
- struct student *chabynum(struct student * head)
- {
-
- struct student *p1,*p2,*p;
-
- p=NULL;
- printf("please input the number of the student you want to change :\n");
- scanf("%d",&p->num);
-
- p1=head;
-
-
- if(NULL==p1)
- printf("This is a null linklist !");
-
- else
- while(p->num!=p1->num&&p1->next!=NULL)
- {
- p2=p1;
- p1=p1->next;
- }
- if(NULL==p1->next)
- { if(p->num!=p1->num)
- printf("There is no student you want to change !\n\n ");
-
- else
- {
- printf("Searching successful !\n");
- printf("please input the %d number student's age :\n",p1->num);
- scanf("%d",&p->age);
- p1->age=p->age;
-
- printf("please input the %d number student's sex :\n",p1->num);
- scanf("%s",p->sex);
- strcpy(p1->sex,p->sex);
-
- printf("please input the %d number sthdent's name :\n",p1->num);
- scanf("%s",p->name);
- strcpy(p1->name,p->name);
-
- }
- }
-
- else
- {
- printf("Searching successful !\n");
- printf("please input the %d number student's age :\n",p1->num);
- scanf("%d",&p->age);
- (p1->age)=(p->age);
-
- printf("please input the %d number student's sex :\n",p1->num);
- scanf("%s",p->sex);
- strcpy(p1->sex,p->sex);
-
- printf("please input the %d number sthdent's name :\n",p1->num);
- scanf("%s",p->name);
- strcpy(p1->name,p->name);
-
- }
-
- return 0;
-
- }
- /*************************************修改信息(通过姓名)******************************************/
- struct student *chabyname(struct student *head)
- {
- struct student *p1,*p2,*p;
-
- p=NULL;
- printf("please input the name of the student you want to search :\n");
- scanf("%s",p->name);
-
- p1=head;
-
-
- if(NULL==p1)
- printf("This is a null linklist !\n");
-
- else
- {
- while((strcmp(p->name,p1->name)!=0)&&p1->next!=NULL)
- {
- p2=p1;
- p1=p1->next;
- }
-
- if(NULL==p1->next)
- {
- if(strcmp(p->name,p1->name)!=0)
- printf("There is no student you want to change !");
-
- else
- {
- printf("Searching successful !\n");
-
- printf("please input the %s student's number :\n",p1->name);
- scanf("%d",&p->num);
- p1->num=p->num ;
-
- printf("please input the %s student's age :\n",p1->name);
- scanf("%d",&p->age);
- p1->age=p->age;
-
- printf("please input the %s student's sex :\n",p1->name);
- scanf("%s",p->sex);
- strcpy(p1->sex,p->sex);
-
-
- }
-
- }
- else
- {
- printf("Searching successful !\n");
-
- printf("please input the %s student's number :\n",p1->name);
- scanf("%d",&p->num);
- p1->num=p->num;
-
- printf("please input the %s student's age :\n",p1->name);
- scanf("%d",&p->age);
- p1->age=p->age;
-
- printf("please input the %s student's sex :\n",p1->name);
- scanf("%s",p->sex);
- strcpy(p1->sex,p->sex);
- }
- }
-
- return(head);
- }
-
- 最近自己敲了一个学生成绩管理系统的代码,可是运行的时候总是运行不了,求各位大牛指点迷津
复制代码 |
|