您好,登錄后才能下訂單哦!
本篇文章為大家展示了C語言中怎么利用鏈表實(shí)現(xiàn)一個(gè)學(xué)生信息管理系統(tǒng),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
代碼實(shí)現(xiàn)的功能:
1.插入學(xué)生信息 2.顯示學(xué)生信息 3.刪除學(xué)生信息 4.在指定位置插入學(xué)生信息 5.查找學(xué)生信息
代碼內(nèi)容:
#include <stdio.h>#include <stdlib.h>#include <string.h>#define Max_Student_Num 10#define Max_Str_len 20typedef struct T_student{ int number; char name [Max_Student_Num]; char phone[Max_Student_Num];};typedef struct T_Node{ struct T_student s; struct T_Node * next;};char command_str[]={"\n1 display all member;\n2 insert member;\n3 del member;\n4 exit\nCommand selection:"};struct T_student students[Max_Student_Num];struct T_Node * head = NULL;int main(int argc, char* argv[]){ int command, i; struct T_student student; struct T_Node * pStu =head; memset(&student,0,sizeof(student)); while(1){ printf("%s",command_str); scanf("%d", &command); switch(command) { case 1: if(head==NULL){ printf("empty!!!!!!!!!!!!\n"); break; } if(head->next==head){ display_student(head); }else{ pStu=head->next; do { display_student(pStu); pStu=pStu->next; }while(pStu!= head->next);// } break; case 2: printf("enter new student number:"); scanf("%d", &student.number); printf("enter new student name:"); scanf("%s", &student.name); if(strlen(student.name) > Max_Str_len) { printf("name is too long!!\n"); continue; } printf("enter new student phone:"); scanf("%s", &student.phone); if(strlen(student.phone) > Max_Str_len) { printf("phone is too long!!\n"); continue; } printf("\n"); if(student.number != 0) insert_student(student); break; case 3: printf("Inter deleted student number:"); scanf("%d", &student.number); del_student(student); break; case 4: return 0; default: printf("error command, try again\n"); break; } }}void display_student( struct T_Node * pStu){ printf("number:%d name:%s phone:%s \n",pStu->s.number,pStu->s.name,pStu->s.phone);}void insert_student(struct T_student student){ struct T_Node* pNode ; struct T_Node* pStu =NULL; int size = sizeof(struct T_Node); pStu=(struct T_Node *)malloc (size); if(pStu == NULL){ return ; } memcpy(&pStu->s,&student,sizeof(student)); if(head==NULL){ pStu->next=head; head=pStu; head->next=head; return ; } pStu->next = head->next; head->next=pStu;}void del_student(struct T_student student){ struct T_Node *pNode =NULL,*p=NULL; if(head->next==head && head->s.number==student.number){ pNode=head; head=NULL; free(pNode); printf("success"); return; } for(pNode=head->next;pNode != head;pNode=pNode->next){ if( pNode->next->s.number == student.number){ p=pNode->next->next; free(pNode->next); pNode->next=p; printf("Delete success!\n"); return; } } printf("Not Found\n");}
上述內(nèi)容就是C語言中怎么利用鏈表實(shí)現(xiàn)一個(gè)學(xué)生信息管理系統(tǒng),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。