生活随笔
收集整理的這篇文章主要介紹了
无空头的链表代码:学生管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
/**程序*/
//********學員管理
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "he.c"
typedef struct STU{int num;//學號char name[20];//姓名double score;//分數struct STU *next;
}node,*linklist;
int node_count=0;
int getlen(linklist head)
{node *p;int count=0;p=head->next;while(p!=NULL){count++;p=p->next;}return count;
}int menu()
{int select;printf("1,錄入學員信息\n");printf("2,顯示學員信息\n");printf("3,修改學員信息\n");printf("4,刪除學員信息\n");printf("5, 排序\n");printf("6,退出\n");printf("請輸入你的選擇:");scanf("%d",&select);return select;
}int menu2()
{int select;printf("請輸入查找依據\n");printf("1,按編號查找\n");printf("2,按姓名查找\n");printf("3,按學號查找\n");printf("4,返回上級菜單\n");printf("請輸入你的選擇:");scanf("%d",&select);return select;
}int menu3()
{int select;printf("1,按姓名排序\n");printf("2,按學號排序\n");printf("3,按分值排序\n");printf("4, 返回上級菜單\n");printf("請輸入你的選擇:");scanf("%d",&select);return select;
}void input(node *p)
{printf("\n");printf("請輸入學員學號\n");scanf("%d",&p->num);//fflush(stdin);//清空輸入緩沖區,清除多余的換行符printf("請輸入學員姓名\n");scanf("%s",p->name);printf("請輸入學員分數\n");scanf("%lf",&p->score);
}void output(node *p)
{printf("--------------------------------\n");printf("姓名:%s\n",p->name);printf("學號:%d\n",p->num);printf("成績:%f\n",p->score);printf("--------------------------------\n");
}void initiallist(linklist *p)
{(*p)=(node *)malloc(sizeof(node));(*p)->next=NULL;
}int mod(linklist L,int i)
{node *p;int j;if(i<1||i>getlen(L)){printf("輸入信息非法!\n");return 0;}p=L->next;for(j=1;j<i;j++){p=p->next;}printf("請輸入第%d同學的更改信息",i);input(p);return 1;
}int inputlist(linklist L)
{node *p,*q;int j;//static int node_count=0;node_count++;printf("請輸入第%d個學生的信息\n",node_count);p=L;for(j=1;j<node_count;j++){p=p->next;}q=(node *)malloc(sizeof(node));p->next=q;p=q;input(p);p->next=NULL;return 0;
}int del(linklist L,int i)
{node *p,*q;int j;if(i<1||i>getlen(L)){printf("輸入不合法\n");return 0;}p=L;q=L->next;for(j=1;j<i;j++){p=q;q=q->next; /*p->next=q->next; p=q;q=q->next;*/}p->next=q->next;free(q); /*p->next=NULL;free(p);*/node_count--;return 1;
}int outputlist(linklist L)
{int count=1;while(L->next!=NULL){L=L->next;printf("**************第%d個學生********\n",count);output(L);count++;}return 0;
}node *SearchBySerial(linklist L,int num)
{int j=1;node *p;p=L;if(num<1||(getlen(L)<num)){printf("輸入越界!");return NULL;}while(p->next!=NULL&&j<=num){p=p->next;j++;}return p;
}
node *SearchByName(linklist L,char *name[])
{node *p;p=L->next;while(p!=NULL&&strcmp(name,p->name)!=0){p=p->next;}return p;
}node *SearchByID(linklist L,int stuID)
{int j=1;node *p;p=L->next;while(p!=NULL&&stuID!=p->num){p=p->next;j++;}return p;
}
/*------------------------------------------------------------------------------這是我的代碼*------------------------------------------------------------------------------*/
int bubblesort(linklist L)
{int len,i,j;node *tmp,*pf,*p,*tail=NULL;len=getlen(L);/*pf=L;p=pf->next;*/for(i=0;i<len-1;i++){ pf=L;p=pf->next;for(j=1;j<len-i;j++){if(strcmp(p->name,p->next->name)==1){pf->next=p->next;p->next=pf->next->next;pf->next->next=p;p=pf->next;/* 也可以這一段 *//*pf->next=p->next;tmp=p->next->next;p->next->next=p;p->next=tmp;p=pf->next;*/}pf=pf->next;p=p->next;}}
}int main()
{linklist head;int choose=0,choose1=0,choose3=0,del_i,j=0;int num,stuID;char name[20];initiallist(&head);while(choose != 6){choose=menu();switch(choose){case 1:{ inputlist(head);}break;case 2:{outputlist(head);}break;case 3:choose1=0;while(choose1!=4){choose1=menu2();j=0;switch(choose1){case 1:{printf("請輸入要查找的編號:");scanf("%d",&num);if(SearchBySerial(head,num)!=NULL){printf("------------根據編號查找,找到,請修改--------------\n");output(SearchBySerial(head,num));mod(head,num);}elseprintf("按編號未找到!\n");}break;case 2:{printf("請輸入要查找的姓名:");scanf("%s",name);if(SearchByName(head,name)!=NULL){printf("------------根據姓名查找,找到,請修改--------------\n");output(SearchByName(head,name));input(SearchByName(head,name));}elseprintf("按姓名未找到!\n");}break;case 3:{printf("請輸入要查找的學號:");scanf("%d",&stuID);if(SearchByID(head,stuID)!=NULL){printf("------------根據學號查找,找到,請修改--------------\n");output(SearchByID(head,stuID));input(SearchByID(head,stuID));}elseprintf("按姓名未找到!\n");}break;default:break;}}break;case 4:{printf("輸入要刪除的學生的編號\n");scanf("%d",&del_i);del(head,del_i);}case 5:{choose3=0;while(choose3!=4){choose3=menu3();j=0;bubblesort(head);}}break;//......default:break;}}
}
轉載于:https://my.oschina.net/libowen/blog/91311
總結
以上是生活随笔為你收集整理的无空头的链表代码:学生管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。