1. 程式人生 > 實用技巧 >連結串列操作(改)--學生管理

連結串列操作(改)--學生管理

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define N 100
void tillinsert(int id,char *name,float score);//尾部插入結點
void print();//輸出連結串列全部資訊 
void headinsert(int id,char *name,float score);//頭部插入結點 
void nodeinsert(int id,char *name,float score,int idtemp);//從一個結點前插入 
void deletenode(int
id);//刪除結點 void deletehead();//刪除頭結點 void deletetill(); //刪除尾結點 //建立結構體 struct student{ int id; char name[N]; float score; struct student* next; }; //初始化頭尾結點 struct student* head=NULL; struct student* end=NULL; //主函式 int main(){ int id; char name[N]; float score; int m; printf(
"1.輸入1,輸入學生資訊。\n2.輸入2,從尾部插入一個節點。\n3.輸入3,從頭部插入一個結點。\n4.輸入4,從一個指定結點前插入一個結點。\n5.輸入5,刪除一個指定結點。\n6.輸入6,顯示當前連結串列資訊。\n7.輸入7,退出程式。\n"); while(m!=7){ scanf("%d",&m); if(m==1){ printf("請輸入學生總數:\n"); int i,n; scanf("%d",&n); printf("請按順序輸入學生的id,姓名,分數,中間空格隔開\n"); for(i=1
;i<=n;i++){ scanf("%d",&id); scanf("%s",name); scanf("%f",&score); tillinsert(id,name,score); } } if(m==2){ printf("從頭部插入結點,請輸入資料:\n"); scanf("%d",&id); scanf("%s",name); scanf("%f",&score); headinsert(id,name,score); } if(m==3){ printf("從尾部插入結點,請輸入資料:\n"); scanf("%d",&id); scanf("%s",name); scanf("%f",&score); tillinsert(id,name,score); } if(m==4){ printf("從任意一個前插入新的結點,請輸入該結點中id:\n"); int idtemp; scanf("%d",&idtemp); printf("請輸入要插入結點中的資料:\n"); scanf("%d",&id); scanf("%s",name); scanf("%f",&score); nodeinsert(id,name,score,idtemp); } if(m==5){ printf("請輸入所要刪除的結點的id:\n"); scanf("%d",&id); deletenode(id); } if(m==6){ print(); } if(m!=7){ printf("程式執行完畢,請進行下一步操作:\n");} } return 0; } //從尾部插入結點 void tillinsert(int id,char *name,float score){ //建立結點 開闢空間 struct student* p1=(struct student*)malloc(sizeof(struct student)); //參量賦值 //由於字元陣列的特殊性,採用strcpy函式來賦值 p1->id=id; strcpy(p1->name,name); p1->score=score; p1->next=NULL; if(head==NULL||end==NULL){ head=p1; end=p1; } else{ end->next=p1; end=p1; } } //輸出連結串列資訊 void print(){ struct student* p=head; printf("該連結串列的資訊目前為:\n"); while(p!=NULL){ printf("id:%d name:%s score:%f\n",p->id,p->name,p->score); p=p->next; } } //頭部插入結點 void headinsert(int id,char *name,float score){ struct student* p2=(struct student*)malloc(sizeof(struct student)); p2->id=id; strcpy(p2->name,name); p2->score=score; if(head==NULL||end==NULL){ head=p2; end=p2; } else{ p2->next=head; head=p2; } } //從一個結點前插入新的結點 idtemp為所要插入的結點對應的id void nodeinsert(int id,char *name,float score,int idtemp){ struct student* p3=head; //特殊情況:對應id為頭結點的id,即剛好要插入頭結點前 if(p3->id==idtemp){ headinsert(id,name,score); return; } //非特殊情況:先遍歷查詢指定位置 while(p3->next!=NULL){ if(p3->next->id==idtemp){ break; } p3=p3->next; } //如若沒有查詢到指定結點 if(p3->next==NULL){ printf("該結點不存在\n"); return; } //已經查詢到指定結點,建立要插入的結點並賦值 struct student* ptemp=(struct student*)malloc(sizeof(struct student)); ptemp->id=id; strcpy(ptemp->name,name); ptemp->score=score; //連線結點 ptemp->next=p3->next; p3->next=ptemp; } void deletehead(){//定義函式刪除頭結點,主要為後續刪除結點做準備 struct student* ptemp=head; head=head->next; free(ptemp); } void deletetill(){//刪除尾結點,為後續刪除結點做準備 //第一種情況下,只有一個結點 if(head==end){ free(head); head=NULL; end=NULL; } else{ //找到尾巴前一個結點 struct student* ptemp=head;//ptemp為尾巴前面的一個結點 struct student* pt=end;//pt為尾結點 while(ptemp->next!=end){ ptemp=ptemp->next; } end=ptemp;//尾巴前移 free(pt);//釋放舊的尾結點 end->next=NULL; } } //刪除結點 void deletenode(int id){ //如果連結串列為空 if(head==NULL){ printf("該連結串列為空\n"); return; } //查詢是否有該結點 struct student* p=head; while(p!=NULL){ if(p->id==id){ break; } p=p->next; } if(p==NULL){ printf("未找到此結點\n"); return; } //判斷特殊情況, 即p是否為頭尾結點中的一個 else if(p==head){ deletehead(); } else if(p==end){ deletetill(); } else{ //非特殊情況,需要找到所刪除結點的前一個結點 struct student* ptemp=head; while(ptemp->next!=p){ ptemp=ptemp->next; } ptemp->next=p->next; free(p); } }