双向循环链表实现—通讯录(学生管理系统,自行车管理系统,影院管理系统)—C语言课设(万能模板)—数据结构—用文件存储数据
生活随笔
收集整理的這篇文章主要介紹了
双向循环链表实现—通讯录(学生管理系统,自行车管理系统,影院管理系统)—C语言课设(万能模板)—数据结构—用文件存储数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
大家好,如果覺得我這篇文章寫的不錯并且對你有幫助的話就關注一下唄。
這是我關于雙向循環鏈表的博客,可以點進去康康啦
編譯器是VS2019,依舊是分為三個文件
我先把三個文件的原碼放出來,然后對于函數 一 一 解釋。
申請空間
struct Contact* BuyNewNode(char* name, int age, char* sex, char* tel, char* adr) {struct Contact* newnode = (struct Contact*)malloc(sizeof(struct Contact));assert(newnode);newnode->next = newnode;newnode->front = newnode;strcpy(newnode->name , name);newnode->age = age;strcpy(newnode->sex , sex);strcpy(newnode->tel , tel);strcpy(newnode->adr , adr);return newnode; }先將文件中的數據存到鏈表中
這里用 feof判斷文件光標后面是否有內容,如果光標后面沒有內容則返回非0,如果有內容則返回0,黨光標后面有內容的時候我們需要讀取文件,所以循環條件為 while(!feof)
void SaveFileData(PC*phead) {FILE* fp = fopen("data.txt", "r");assert(phead);if (fp == NULL){printf("%s", strerror(errno));return;}getc(fp);if (!feof(fp)){rewind(fp);}while (!feof(fp)){PC* newnode = BuyNewNode("000000", 0, "000000", "00000", "000000");fscanf(fp, "%s ", newnode->name);fscanf(fp, "%d ", &(newnode->age));fscanf(fp, "%s ", newnode->sex);fscanf(fp, "%s ", newnode->tel);fscanf(fp, "%s ", newnode->adr);PC* head = phead->front;head->next = newnode;newnode->front = head;newnode->next = phead;phead->front = newnode;}fclose(fp);fp = NULL; }1.添加聯系人
void ADDPc(PC* phead) {PC* newnode = BuyNewNode("000000", 0, "000000", "00000", "000000");assert(phead);assert(newnode);printf("請輸入姓名\n");scanf("%s", newnode->name);printf("請輸入年齡\n");scanf("%d", &(newnode->age));printf("請輸入性別\n");scanf("%s", newnode->sex);printf("請輸入電話\n");scanf("%s", newnode->tel);printf("請輸入地址\n");scanf("%s", newnode->adr);PC* head = phead->front;head->next = newnode;newnode->front = head;newnode->next = phead;phead->front = newnode; printf("添加成功\n"); }2.刪除聯系人
void DelPc(PC* pos) {assert(pos);PC* Front = pos->front;PC* Next = pos->next;Front->next = Next;Next->front = Front;free(pos);pos = NULL; }3.查找聯系人
PC* Find_by_name( PC* phead, char* name) {assert(phead);PC* pos = phead->next;while (pos !=phead){if (0 == strcmp(name, pos->name)){return pos;}pos = pos->next;}return NULL; }4.修改聯系人
void MoDify(PC* pos) {char str_name2[NAM_MAX] = "0";char str_sex2[SEX_MAX] = "0";char str_tel2[TEL_MAX] = "0";char str_adr2[ADR_MAX] = "0";assert(pos);printf("請輸入新的姓名\n");scanf("%s", str_name2);strcpy(pos->name, str_name2);printf("請輸入新的年齡\n");scanf("%d", &(pos->age));printf("請輸入新的性別\n");scanf("%s", str_sex2);strcpy(pos->sex, str_sex2);printf("請輸入新的電話\n");scanf("%s",str_tel2);strcpy(pos->tel, str_tel2);printf("請輸入新的地址\n");scanf("%s", str_adr2);strcpy(pos->adr, str_adr2);}5.顯示所有聯系人
void Print(PC* phead) {PC* cur = phead->next;assert(phead);if (cur == phead){printf("還沒有添加信息\n");}printf("姓名 年齡 性別 電話 地址\n");while (cur != phead){printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", cur->name, cur->age, cur->sex, cur->tel, cur->adr);cur = cur->next;} }6.清空所有聯系人
void DesTroy(PC* phead) {assert(phead);PC* cur=phead->next;while (cur != phead){PC* Next = cur->next;free(cur);cur = Next;}phead->next = phead;phead->front = phead; }7.按名字排序所有聯系人
由于qsort是用于一個連續的空間,所以,這里用冒泡排序。
算個數的時候要加上phead,但是排序的時候不要排phead
8.將數據保存在文件中
void SaveData(PC* phead) {FILE* fp = NULL;PC* cur = phead->next;assert(phead);fp = fopen("data.txt", "w");if (fp == NULL){printf("%s", strerror(errno));return;}while (cur != phead){fprintf(fp, "%s ", cur->name);fprintf(fp, "%d ", cur->age);fprintf(fp, "%s ", cur->sex);fprintf(fp, "%s ", cur->tel);fprintf(fp, "%s ", cur->adr);fprintf(fp, "\n");//換行cur = cur->next;}fclose(fp);fp = NULL; }
contact.h文件中包含所需要的頭文件以及函數的聲明:
contact.h文件
以下就是contact.h的內容
#pragma once #include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> #include<windows.h> #include<errno.h>#define NAM_MAX 20 #define SEX_MAX 10 #define TEL_MAX 20 #define ADR_MAX 20typedef struct Contact {struct Contact* front;char name[NAM_MAX];int age;char sex[SEX_MAX];char tel[TEL_MAX];char adr[ADR_MAX];struct Contact* next; }PC;//申請空間√ PC* BuyNewNode(char* name, int age, char* sex, char* tel, char* adr); void ADDPc(PC* phead);//增加聯系人√ void DelPc(PC* pos);//刪除聯系人√ PC* Find_by_name(PC* phead,char* name);//通過姓名尋找聯系人√ void Print(const PC* phead);//打印聯系人√ void DesTroy(PC* phead);//銷毀通訊錄√ void MoDify(PC* pos);//修改聯系人信息√ int Size(PC* phead);//計算數據數量√ void Compare_ByName(PC*phead);//按名字排序 abcdefg......√ void SaveData(PC*phead);// 保存數據√ void SaveFileData(PC*phead);//先將文件中的數據存到鏈表中√contac.c是每個函數的實現:
contac.c文件
#define _CRT_SECURE_NO_WARNINGS 1 #include"contact.h" struct Contact* BuyNewNode(char* name, int age, char* sex, char* tel, char* adr) {struct Contact* newnode = (struct Contact*)malloc(sizeof(struct Contact));assert(newnode);newnode->next = newnode;newnode->front = newnode;strcpy(newnode->name , name);newnode->age = age;strcpy(newnode->sex , sex);strcpy(newnode->tel , tel);strcpy(newnode->adr , adr);return newnode; } void SaveFileData(PC*phead) {FILE* fp = fopen("data.txt", "r");assert(phead);if (fp == NULL){printf("%s", strerror(errno));return;}getc(fp);while (!feof(fp)){rewind(fp);PC* newnode = BuyNewNode("000000", 0, "000000", "00000", "000000");fscanf(fp, "%s ", newnode->name);fscanf(fp, "%d ", &(newnode->age));fscanf(fp, "%s ", newnode->sex);fscanf(fp, "%s ", newnode->tel);fscanf(fp, "%s ", newnode->adr);PC* head = phead->front;head->next = newnode;newnode->front = head;newnode->next = phead;phead->front = newnode;}fclose(fp);fp = NULL; } void Print(PC* phead) {PC* cur = phead->next;assert(phead);if (cur == phead){printf("還沒有添加信息\n");}printf("姓名 年齡 性別 電話 地址\n");while (cur != phead){printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", cur->name, cur->age, cur->sex, cur->tel, cur->adr);cur = cur->next;} } void ADDPc(PC* phead) {PC* newnode = BuyNewNode("000000", 0, "000000", "00000", "000000");assert(phead);assert(newnode);printf("請輸入姓名\n");scanf("%s", newnode->name);printf("請輸入年齡\n");scanf("%d", &(newnode->age));printf("請輸入性別\n");scanf("%s", newnode->sex);printf("請輸入電話\n");scanf("%s", newnode->tel);printf("請輸入地址\n");scanf("%s", newnode->adr);PC* head = phead->front;head->next = newnode;newnode->front = head;newnode->next = phead;phead->front = newnode; printf("添加成功\n"); } PC* Find_by_name( PC* phead, char* name) {assert(phead);PC* pos = phead->next;while (pos !=phead){if (0 == strcmp(name, pos->name)){return pos;}pos = pos->next;}return NULL; } void DelPc(PC* pos) {assert(pos);PC* Front = pos->front;PC* Next = pos->next;Front->next = Next;Next->front = Front;free(pos);pos = NULL; } void DesTroy(PC* phead) {assert(phead);PC* cur=phead->next;while (cur != phead){PC* Next = cur->next;free(cur);cur = Next;}free(phead);phead = NULL; } void MoDify(PC* pos) {char str_name2[NAM_MAX] = "0";char str_sex2[SEX_MAX] = "0";char str_tel2[TEL_MAX] = "0";char str_adr2[ADR_MAX] = "0";assert(pos);printf("請輸入新的姓名\n");scanf("%s", str_name2);strcpy(pos->name, str_name2);printf("請輸入新的年齡\n");scanf("%d", &(pos->age));printf("請輸入新的性別\n");scanf("%s", str_sex2);strcpy(pos->sex, str_sex2);printf("請輸入新的電話\n");scanf("%s",str_tel2);strcpy(pos->tel, str_tel2);printf("請輸入新的地址\n");scanf("%s", str_adr2);strcpy(pos->adr, str_adr2);} int Size(PC* phead) {int sz = 0;PC* cur = phead->next;assert(phead);while (cur != phead){sz++;cur = cur->next;}return sz; }void Compare_ByName(PC*phead) {int i =0;int j = 0;PC* cur = phead->next;PC* Next = cur->next;char Name_s[NAM_MAX] = "0";int age_s=0;char sex_s[SEX_MAX] = "0";char tel_s[TEL_MAX] = "0";char adr_s[ADR_MAX] = "0";assert(phead);for (i = 0; i < Size(phead)+1; i++){for (j = 0; j < Size(phead) - i ;j++){if ((strcmp(cur->name, Next->name) > 0) && (cur != phead) && (Next != phead)){strcpy(Name_s, cur->name);strcpy(cur->name, Next->name);strcpy(Next->name, Name_s);age_s = cur->age;cur->age = Next->age;Next->age = age_s;strcpy(sex_s, cur->sex);strcpy(cur->sex, Next->sex);strcpy(Next->sex, sex_s);strcpy(tel_s, cur->tel);strcpy(cur->tel, Next->tel);strcpy(Next->tel, tel_s);strcpy(adr_s, cur->adr);strcpy(cur->adr, Next->adr);strcpy(Next->adr, adr_s);}cur = cur->next;Next = Next->next;}} }void SaveData(PC* phead) {FILE* fp = NULL;PC* cur = phead->next;assert(phead);fp = fopen("data.txt", "w");if (fp == NULL){printf("%s", strerror(errno));return;}while (cur != phead){fprintf(fp, "%s ", cur->name);fprintf(fp, "%d ", cur->age);fprintf(fp, "%s ", cur->sex);fprintf(fp, "%s ", cur->tel);fprintf(fp, "%s ", cur->adr);fprintf(fp, "\n");//換行cur = cur->next;}fclose(fp);fp = NULL; }test.c是各個函數之間的調用:
test.c文件
#define _CRT_SECURE_NO_WARNINGS 1 #include"contact.h" enum MyEnum {Exit,Add,Dele,Find,Modify,Show,Destroy,Sort,Save };void Menu() {//system("cls");printf("*******************************\n");printf("*********1.添加聯系人*********\n");printf("*********2.刪除聯系人***********\n");printf("*********3.查找聯系人************\n");printf("*********4.修改聯系人*******\n");printf("*********5.顯示所有聯系人*******\n");printf("*********6.清空所有聯系人*******\n");printf("*********7.按名字排序所有聯系人*******\n");printf("*********8.將數據保存在文件中*******\n");printf("*********0.退出*******\n");printf("************************\n"); } int main(void) {PC* phead = BuyNewNode("000000", 0, "000000", "00000", "000000");//初始化一個頭節點SaveFileData(phead);int input = 0;int sz = 0;//求聯系人的個數char str_name[NAM_MAX] = "0";//用來通過姓名尋找do{Sleep(500);Menu();printf("請輸入你的選擇->");scanf("%d", &input);switch (input){case Exit:printf("即將退出程序\n");for (int i = 0; i < 10; i++){printf("-");Sleep(100);}break;case Add:if (phead == NULL){phead= BuyNewNode("000000", 0, "000000", "00000", "000000");}ADDPc(phead);break;case Dele:printf("請輸入你要刪除的姓名:\n");scanf("%s", str_name);PC* pos = Find_by_name(phead, str_name);if (pos){printf("姓名 年齡 性別 電話 地址\n");printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", pos->name, pos->age, pos->sex, pos->tel, pos->adr);DelPc(pos);printf("刪除成功\n");}else{printf("沒有找到\n");}break;case Show:sz = Size(phead);printf("共有%d個聯系人\n", sz);Print(phead);break;case Find:printf("請輸入你要尋找的姓名:\n");scanf("%s", str_name);PC* pos1=Find_by_name(phead,str_name);if (pos1){printf("姓名 年齡 性別 電話 地址\n");printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", pos1->name, pos1->age, pos1->sex, pos1->tel, pos1->adr);}else{printf("沒有找到\n");}break;case Modify:printf("請輸入你要尋找的姓名:\n");scanf("%s", str_name);PC* pos2 = Find_by_name(phead, str_name);if (pos2){printf("姓名 年齡 性別 電話 地址\n");printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", pos2->name, pos2->age, pos2->sex, pos2->tel, pos2->adr);MoDify(pos2);}else{printf("沒有找到\n");}break;case Destroy:DesTroy(phead);printf("銷毀成功\n");break;case Sort:sz = Size(phead);printf("共有%d個聯系人\n", sz);Compare_ByName(phead);Print(phead);break;case Save:SaveData(phead);printf("保存成功\n");break;default:printf("選擇錯誤,請重新選擇\n");break;}} while (input);return 0; }總結
以上是生活随笔為你收集整理的双向循环链表实现—通讯录(学生管理系统,自行车管理系统,影院管理系统)—C语言课设(万能模板)—数据结构—用文件存储数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3G技术及IPV6
- 下一篇: web大作业:基于html+css+ja