原文:http://blog.csdn.net/bdmh/article/details/6112631
看到論壇裏總有學生在尋求鏈表實現學生成績管理的帖子,動手寫一個,也算學習,雖然功能很少,以前還真沒寫過完整的,寫的比較粗糙,比較簡單,不合適的地方歡迎指正。
LinkTable.h
- class StudentInfo
- {
- public:
- char code[6];
- float chinesescore;
- float mathscore;
- StudentInfo* next;
- StudentInfo* prior;
- };
- //添加學生信息,如果prior爲NULL,則newinfo爲頭結點
- StudentInfo* AddInfo(StudentInfo* prior,StudentInfo* newinfo);
- //根據編號刪除學生信息
- bool DelStudentInfo(char* code,StudentInfo*& pHead);
- //根據編號查找學生信息
- StudentInfo* FindStudentInfoByCode(char* code,StudentInfo* pHead);
- //修改學生成績信息
- void ModifyStudentInfo(StudentInfo* p);
- //從頭結點開始打印學生信息
- void PrintStudentInfo(StudentInfo* pHead);
- //對指定的節點輸入學生信息
- void InputInfo(StudentInfo* pInfo);
- //獲取最後一個節點
- StudentInfo* GetLastStudentInfo(StudentInfo* pHead);
- //保存鏈表到文件,順序保存
- void SaveStudentInfoToFile(char* filename,StudentInfo* pHead);
- //從文件中讀取鏈表
- void LoadStudentInfoFromFile(char* filename,StudentInfo*& pHead);
- //操作提示
- void ShowOperateInfo(void);
LinkTable.cpp
- #include "LinkTable.h"
- #include <stdio.h>
- #include <string.h>
- //添加學生信息,如果prior爲NULL,則newinfo爲頭結點
- StudentInfo* AddInfo(StudentInfo* prior,StudentInfo* newinfo)
- {
- if (!prior)
- {
- newinfo->next = NULL;
- newinfo->prior = NULL;
- }
- else
- {
- prior->next = newinfo;
- newinfo->prior = prior;
- newinfo->next = NULL;
- }
- memset(newinfo->code,0,sizeof(newinfo->code));
- InputInfo(newinfo);
- return newinfo;
- }
- //根據編號刪除學生信息
- bool DelStudentInfo(char* code,StudentInfo*& pHead)
- {
- StudentInfo* pDel = FindStudentInfoByCode(code,pHead);
- if (pDel == NULL)
- return false;
- else
- {
- if (pDel == pHead)
- pHead = pDel->next;
- else
- {
- pDel->prior->next = pDel->next;
- if (pDel->next != NULL)
- pDel->next->prior = pDel->prior;
- }
- delete pDel;
- pDel = NULL;
- return true;
- }
- }
- //根據編號查找學生信息
- StudentInfo* FindStudentInfoByCode(char* code,StudentInfo* pHead)
- {
- StudentInfo* p = pHead;
- while (p != NULL)
- {
- if (strcmp(p->code,code) == 0)
- {
- return p;
- }
- p = p->next;
- }
- return NULL;
- }
- //修改學生成績信息
- void ModifyStudentInfo(StudentInfo* p)
- {
- printf("%s號學生當前信息:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);
- printf("%s","請輸入修改後的語文成績:");
- scanf("%f",&p->chinesescore);
- printf("%s","請輸入修改後的數學成績:");
- scanf("%f",&p->mathscore);
- printf("修改完畢,%s號學生最新信息:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);
- }
- //從頭結點開始打印學生信息
- void PrintStudentInfo(StudentInfo* pHead)
- {
- StudentInfo* p = pHead;
- while (p!=NULL)
- {
- printf("%s號學生:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);
- p = p->next;
- }
- }
- //對指定的節點輸入學生信息
- void InputInfo(StudentInfo* pInfo)
- {
- printf("%s","請輸入學生編號:");
- scanf("%s",pInfo->code);
- printf("%s","請輸入語文成績:");
- scanf("%f",&pInfo->chinesescore);
- printf("%s","請輸入數學成績:");
- scanf("%f",&pInfo->mathscore);
- }
- //獲取最後一個節點
- StudentInfo* GetLastStudentInfo(StudentInfo* pHead)
- {
- if (pHead == NULL)
- return NULL;
- StudentInfo* p = pHead;
- while (p)
- {
- if (p->next == NULL)
- return p;
- p = p->next;
- }
- }
- //保存鏈表到文件,順序保存
- void SaveStudentInfoToFile(char* filename,StudentInfo* pHead)
- {
- FILE* f;
- f= fopen(filename,"w");
- StudentInfo* p = pHead;
- while (p)
- {
- fwrite(p->code,sizeof(char),sizeof(p->code),f);
- fwrite(&p->chinesescore,sizeof(float),1,f);
- fwrite(&p->mathscore,sizeof(float),1,f);
- //fwrite("/n",sizeof(char),1,f);
- p=p->next;
- }
- //fwrite("/0",sizeof(char),1,f);
- fclose(f);
- }
- //從文件中讀取鏈表
- void LoadStudentInfoFromFile(char* filename,StudentInfo*& pHead)
- {
- FILE* f;
- f= fopen(filename,"r");
- if (f == NULL)
- return;
- fseek(f, 0, SEEK_SET);
- StudentInfo* p = NULL;
- StudentInfo* newp = NULL;
- int c;
- //c = fgetc(f);
- while (!feof(f))
- {
- //因爲不能完全依賴feof判斷文件是否到結尾,所以用fgetc的返回值來判斷,
- //當讀取到結尾時,feof實際並沒有獲得到達結尾的信息,所以這裏用fgetc繼續讀取一個字符,來判斷是否結束
- //如果不是-1(結尾),那麼回退一個文件位置,如果是-1,退出
- c = fgetc(f);
- if (c != -1)
- fseek(f, -1, SEEK_CUR);
- else
- return;
- if (pHead == NULL)
- {
- pHead = new StudentInfo;
- pHead->next = NULL;
- pHead->prior = NULL;
- memset(pHead->code,0,sizeof(pHead->code));
- fread(pHead->code,sizeof(char),sizeof(pHead->code),f);
- fread(&pHead->chinesescore,sizeof(float),1,f);
- fread(&pHead->mathscore,sizeof(float),1,f);
- p = pHead;
- }
- else
- {
- newp = new StudentInfo;
- newp->next = NULL;
- newp->prior = NULL;
- newp->prior = p;
- memset(newp->code,0,sizeof(newp->code));
- fread(newp->code,sizeof(char),sizeof(newp->code),f);
- fread(&newp->chinesescore,sizeof(float),1,f);
- fread(&newp->mathscore,sizeof(float),1,f);
- p->next = newp;
- p = newp;
- }
- }
- fclose(f);
- }
- //操作提示
- void ShowOperateInfo(void)
- {
- printf("%s/n","1:增加信息");
- printf("%s/n","2:刪除信息");
- printf("%s/n","3:查找信息");
- printf("%s/n","4:修改信息");
- printf("%s/n","5:保存信息");
- printf("%s/n","9:打印信息");
- printf("%s/n","0:結束");
- }
Main方法
- #include "LinkTable.h"
- int main()
- {
- ShowOperateInfo();
- int i;
- //curInfo當前節點,用來添加節點用
- StudentInfo* curInfo = new StudentInfo;
- StudentInfo* pHead = NULL;
- LoadStudentInfoFromFile("e://link.txt",pHead);
- //將curInfo指向最後一個節點
- if (pHead != NULL)
- curInfo = GetLastStudentInfo(pHead);
- char code[10]={0};
- while (true)
- {
- printf("%s","請輸入操作號:");
- scanf("%d",&i);
- switch (i)
- {
- //退出
- case 0:
- exit(0);
- //添加
- case 1:
- if (pHead == NULL)
- {
- pHead = new StudentInfo;
- curInfo = pHead = AddInfo(NULL,pHead);
- }
- else
- {
- StudentInfo* newInfo = new StudentInfo;
- curInfo = AddInfo(curInfo,newInfo);
- }
- break;
- //刪除
- case 2:
- printf("%s","請輸入要刪除的學生編號:");
- scanf("%s",code);
- DelStudentInfo(code,pHead);
- curInfo = GetLastStudentInfo(pHead);
- break;
- //查找
- case 3:
- printf("%s/n","請輸入要查找的學生編號:");
- scanf("%s",code);
- {
- StudentInfo* p = FindStudentInfoByCode(code,pHead);
- if (p == NULL)
- printf("編號%s不存在",code);
- else
- {
- printf("%s號學生:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);
- }
- }
- break;
- //修改
- case 4:
- printf("%s/n","請輸入要修改的學生編號:");
- scanf("%s",code);
- {
- StudentInfo* p = FindStudentInfoByCode(code,pHead);
- if (p == NULL)
- printf("編號%s不存在",code);
- else
- {
- ModifyStudentInfo(p);
- }
- }
- break;
- case 5:
- SaveStudentInfoToFile("e://link.txt",pHead);
- break;
- //打印
- case 9:
- PrintStudentInfo(pHead);
- break;
- default:
- exit(0);
- }
- }
- }
運行結果: