基於鏈表的學生資訊管理,鏈表學生資訊管理

來源:互聯網
上載者:User

基於鏈表的學生資訊管理,鏈表學生資訊管理

供C語言的初學者熟悉鏈表以及結構體,僅供參考,不足之處,敬請諒解!

#include <stdio.h>#include <stdlib.h>#include <string.h>struct stuNode{int number;char name[20];int age;struct stuNode *next;};struct stuNode * newChain(){int n,i;struct stuNode *head, *cur, *temp;printf("input the number of students:");scanf("%d",&n);for(i=0;i<n;i++){temp=(struct stuNode *)malloc(sizeof(struct stuNode));if(temp!=NULL){printf("input the number:");scanf("%d",&temp->number);getchar();printf("input the name:");gets(temp->name);printf("input the age:");scanf("%d",&temp->age);temp->next=NULL;if(i==0)head=cur=temp;else{cur->next=temp;cur=cur->next;}}}return head;}int display(struct stuNode * head){struct stuNode *cur;cur=head;while(cur!=NULL){printf("%5d\t%20s\t%d\n",cur->number,cur->name,cur->age);cur=cur->next;}return 0;}int search(struct stuNode * head){struct stuNode *cur;cur=head;char name[20];printf("input the name you want to search:");getchar();gets(name);while(cur!=NULL){if(strcmp(cur->name,name)==0)break;cur=cur->next;}if(cur==NULL)printf("no found.\n");else{printf("%5d\t%20s\t%d\n",cur->number,cur->name,cur->age);}return 0;}struct stuNode * dele(struct stuNode * head){struct stuNode *cur,*temp;cur=head;int number;printf("input the number u want to delete:");scanf("%d",&number);if(head->number==number){head=head->next;free(cur);}else{while(cur->next!=NULL && cur->next->number!=number)cur=cur->next;if(cur->next!=NULL){temp=cur->next;cur->next=temp->next;free(temp);}}return head;}int add(struct stuNode * head){struct stuNode *cur,*temp;cur=head;while(cur->next!=NULL)cur=cur->next;temp=(struct stuNode *)malloc(sizeof(struct stuNode));if(temp!=NULL){printf("input the number:");scanf("%d",&temp->number);getchar();printf("input the name:");gets(temp->name);printf("input the age:");scanf("%d",&temp->age);temp->next=NULL;cur->next=temp;}return 0;}int update(struct stuNode * head){struct stuNode *cur;int number;cur=head;printf("input the number of the student:");scanf("%d",&number);while(cur!=NULL && cur->number!=number)cur=cur->next;if(cur!=NULL){printf("input the name of the student:");getchar();gets(cur->name);printf("input the age of the student:");scanf("%d",&cur->age);}return 0;}int main(){int sel;struct stuNode *head;while(1){printf("\n\t=================\n");printf("\t 1. new chain\n");printf("\t 2. display chain\n");printf("\t 3. search\n");printf("\t 4. delete\n");printf("\t 5. add\n");printf("\t 6. update chain\n");printf("\t 7. quit\n");printf("\t input your choice:");scanf("%d",&sel);switch(sel){case 1: head=newChain(); break;case 2: display(head); break;case 3: search(head); break;case 4: head=dele(head); break;case 5: add(head); break;case 6: update(head); break;case 7: return 0; }}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.