鏈表實現簡單學生資訊管理

來源:互聯網
上載者:User
#include <stdio.h>#include <malloc.h>#include <string.h>#define len sizeof(struct stu)typedef long long ll;struct stu                   //建立學生資訊的結構體,包含學號,姓名 ,成績三個子項 {ll num;char a[50];double score;struct stu *next;} ;struct stu *creat()                  //建立一個鏈表 {struct stu *head;struct stu *p;struct stu *tail;head=tail=NULL;                //表頭表尾指標初始化為空白 printf("請按照學號從小到達的順序輸入學生的資訊,中間用空格隔開,學號輸入0表示輸入結束:\n學號:\t姓名:\t分數:\n");ll x;scanf("%lld",&x);             //輸入學號 getchar();while(x!=0){p=(struct stu*)malloc(len);        //以學號輸入0為結束,當學號不為0時,建立一個新的節點 p->num=x;                          if(head==NULL)                    //如果頭指標為空白,則把頭指標指向第一個節點    head=p;if(tail!=NULL)                      tail->next=p;tail=p;                                //尾指標也指向建立立的節點 scanf("%s%lf%lld",p->a,&p->score,&x);  //輸入姓名成績和下一個學號 }if(tail!=NULL)                       //將尾節點的後繼設定為空白 tail->next=NULL; return head;}void list(struct stu *p)                //顯示鏈表裡的資訊 {struct stu *head;head=p;if(head==NULL)                 printf("您並沒有輸入任何資訊\n");    //如果頭為空白則證明沒有輸入任何和資訊 else{printf("學號:\t姓名:\t分數:\n");while(head!=NULL){printf("%lld\t%s\t%5.1lf\n",head->num,head->a,head->score);  //輸出節點裡的學生資訊 head=head->next;      //讀取下一個節點 }     }}void query(char c[],struct stu *head)   //按姓名尋找學生資訊 {struct stu *p;p=head;while(strcmp(p->a,c)!=0){p=p->next;}printf("學號:\t姓名:\t分數:\n");printf("%lld\t%s\t%5.1lf\n",p->num,p->a,p->score);}struct stu *delet(char c[],struct stu *head){struct stu *p,*temp;                //設定兩個結構體指標,分別指向要刪除學生的資訊節點和它的前驅節點 p=temp=head;if(p==NULL){printf("並沒有任何學生資訊\n"); //如果鏈表為空白則按情況輸出 }else{    while(strcmp(p->a,c)!=0 && p->next!=NULL)  //當沒有查詢到指定學生並且沒有遍曆鏈表時一直向後查詢     {    temp=p;    p=p->next;}    if(strcmp(p->a,c)==0)    {    if(p==head)                     //如果要刪除的學生資訊位於鏈表的前端節點時直接刪除並將前端節點設定為下一個節點     {    printf("姓名為:%s的學生資訊已刪除,按2查看\n",c);head=head->next;                free(p);}else                          //其他情況下將刪除掉節點的前驅節點的尾指標指向刪除節點的下一個節點     {        printf("姓名為:%s的學生資訊已刪除,按2查看\n",c);        temp->next=p->next;        free(p);}}else{printf("沒有找到這個名字的學生\n");   //如果沒有找到則按情況輸出 }}return head;}struct stu *add(ll k,char name[],int scor,struct stu *head) {struct stu *p,*temp,*q; p=temp=head;if(p->num > k)                                //按學號新增學生資訊,當前端節點的學號大於當前學號時插入到表頭 {p=(struct stu *)malloc(len);p->next=head;head=p;        p->num=k; strcpy(p->a,name); p->score=scor;        printf("學生資訊已添加,按2查看\n"); }else{while(p->next!=NULL){temp=p;p=p->next;if(temp->num < k && p->num>k)               //當要插入的學號位於某兩個學號之間時插入到這兩個節點之間 {q=(struct stu *)malloc(len);temp->next=q;q->next=p;q->num=k; strcpy(q->a,name); q->score=scor;printf("學生資訊已添加,按2查看\n");}}if(p->num < k)                                  //當要添加的學生學號大於表尾學號時插入到表尾 {q=(struct stu *)malloc(len);p->next=q;  q->next=NULL;q->num=k; strcpy(q->a,name); q->score=scor;printf("學生資訊已添加,按2查看\n");}}return head;}struct stu *pos1(ll k, char name[], int scor, int pos, struct stu *head){struct stu *p,*temp; p=temp=head;             //按位置新增學生資訊 int num=0;while(p!=NULL){p=p->next;num++;}p=temp=head;if(pos==1)                               //當位置是1時添加到表頭 {p=(struct stu *)malloc(len);p->next=head; head=p;strcpy(p->a,name);p->num=k; p->score=scor;printf("學生資訊已添加,按2查看:") ;}else if(pos>1 && pos<=num)                //當要添加的學生位置在表中時添加到表中 {int l=0;while(p!=NULL){temp=p;p=p->next; l++;if(l=pos){    struct stu *q=(struct stu *)malloc(len);    q->next=p;  temp->next=q;    strcpy(q->a,name);        q->num=k; q->score=scor;        printf("學生資訊已添加,按2查看:") ;        break;}}}else if(pos==num+1)                           //當位置在最後時添加到表尾 {struct stu *q=(struct stu *)malloc(len);while(p->next!=NULL){p=p->next;}p->next=q; q->next=NULL;strcpy(q->a,name);q->num=k; q->score=scor;printf("學生資訊已添加,按2查看:") ;}elseprintf("對不起,您輸入的位置無效\n");                 //其他情況下輸出位置無效的資訊 return head; }struct stu *pos2(int n,struct stu *head)                   //按位置查詢學生資訊,遍曆鏈表,當位置符合時輸出學生資訊 {struct stu *p,*temp;p=head; int m=0,ans=0;if(p==NULL)printf("沒有任何學生資訊\n");else{while(p!=NULL){temp=p;p=p->next;m++;if(m==n){        printf("學號:\t姓名:\t分數:\n");         printf("%lld\t%s\t%5.1lf\n",temp->num,temp->a,temp->score);         ans=1;         break;}}if(ans==0)printf("對不起,您輸入的位置無效\n");    }    return head;} void query2(struct stu *head)                   //查詢學生數量 {struct stu *p;p=head;int n=0;while(p!=NULL){n++;p=p->next;}printf("學生數量為:%d個\n",n);} int main(){printf("\t\t\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\n");printf("\n\n\t\t\t\t學產生績管理系統\n\n\n");printf("\t\t\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\n");printf("\n\n\n\t\t\t\t1:輸入學生資訊\n") ;printf("\n\t\t\t\t2:顯示學生資訊\n");printf("\n\t\t\t\t3:根據姓名查詢學生資訊\n");printf("\n\t\t\t\t4:根據姓名刪除學生資訊\n");printf("\n\t\t\t\t5:根據學號新增學生資訊\n");printf("\n\t\t\t\t6:輸入位置新增學生資訊\n");printf("\n\t\t\t\t7:輸入位置查詢學生資訊\n");    printf("\n\t\t\t\t8:查詢學生數量\n");printf("\n\t\t\t\t0:退出系統\n");printf("\n\t\t\t\t請按0-4選擇功能表項目:\n");int n,k,scor,pos,m=0; char b[50],name[50];struct stu *head; while(scanf("%d",&n)){    if(n==0)       break;    if(m==0&&n!=1)    {    printf("請先輸入成績後才能進行操作\n");    continue;}m=1;switch(n){case 1: head=creat(); break; case 2: list(head); break;case 3: printf("請輸入要查詢的學生姓名:");             scanf("%s",b); query(b,head); break;    case 4: printf("請輸入要刪除的學生姓名:");                    scanf("%s",b); head=delet(b,head);  break;        case 5: printf("請輸入要添加的學生的資訊:學號:\t姓名:\t分數:");                    scanf("%lld",&k); getchar(); scanf("%s%d",name,&scor); head=add(k,name,scor,head);  break;         case 6: printf("請輸入要添加的學生的資訊:學號:\t姓名:\t分數:\t位置:");                    scanf("%lld",&k); getchar(); scanf("%s%d%d",name,&scor,&pos); head=pos1(k,name,scor,pos,head); break;         case 7: printf("請輸入要查詢的學生的位置:");                    scanf("%d",&pos);  head=pos2(pos,head); break;        case 8: query2(head);            }}return 0;} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.