C語(yǔ)言單鏈表實(shí)現(xiàn)圖書管理系統(tǒng)
本文實(shí)例為大家分享了C語(yǔ)言單鏈表實(shí)現(xiàn)圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
單鏈表實(shí)現(xiàn)的圖書管理系統(tǒng)相比于結(jié)構(gòu)體實(shí)現(xiàn)的管理系統(tǒng),可以隨時(shí)開(kāi)辟新的空間,可以增加書的信息
單鏈表的實(shí)現(xiàn)
首先肯定還是打印單鏈表的常規(guī)操作,創(chuàng)建表頭,創(chuàng)建節(jié)點(diǎn),表頭法插入,特定位置刪除,打印鏈表
struct book
{
?? ?char name[20];
?? ?float price;
?? ?int num; ? ? ? ? ?//書的數(shù)量
};
//3 數(shù)據(jù)容器——鏈表
struct Node
{
?? ?struct book data;
?? ?struct Node*next;
};
void printflist(struct Node*headnode);
struct Node*headnode = NULL;
//創(chuàng)建表頭
struct Node*createlisthead()
{
?? ?//動(dòng)態(tài)內(nèi)存申請(qǐng)
?? ?struct Node*headnode = (struct Node*)malloc(sizeof(struct Node));
?? ?//變量的基本規(guī)則:使用前必須初始化
?? ?headnode->next = NULL;
?? ?return headnode;
}
//創(chuàng)建節(jié)點(diǎn),為插入做準(zhǔn)備
//把用戶的數(shù)據(jù)變?yōu)榻Y(jié)構(gòu)體變量
struct Node* createnewnode(struct book data)
{
?? ?struct Node*newnode = (struct Node*)malloc(sizeof(struct Node));
?? ?newnode->data = data;
?? ?newnode->next = NULL;
?? ?return newnode;
}
//表頭法插入
void insertbyhead(struct Node*headnode, struct book data)
{
?? ?struct Node* newnode = createnewnode(data);
?? ?//必須先連后斷
?? ?newnode->next = headnode->next;
?? ?headnode->next = newnode;
}
//指定位置刪除
void deletenodebyname(struct Node*headnode, char *bookname)
{
?? ?struct Node*posleftnode = headnode;
?? ?struct Node*posnode = headnode->next;
?? ?//字符串比較函數(shù)
?? ?while (posnode != NULL && strcmp(posnode->data.name,bookname))
?? ?{
?? ??? ?posleftnode = posnode;
?? ??? ?posnode = posnode->next;
?? ?}
?? ?//討論結(jié)果
?? ?if (posnode == NULL)
?? ?{
?? ??? ?printf("未找到數(shù)據(jù)");
?? ??? ?return ;
?? ?}
?? ?else
?? ?{
?? ??? ?posleftnode->next = posnode->next;
?? ??? ?free(posnode);
?? ??? ?posnode = NULL;
?? ?}
?? ?printflist(headnode);
}
//查找書籍
struct Node*searchbyname(struct Node*headnode, char *bookname)
{
?? ?struct Node *posnode = headnode->next;
?? ?while (posnode != NULL &&strcmp(posnode->data.name, bookname))
?? ?{
?? ??? ?posnode = posnode->next;
?? ?}
?? ?return posnode;
}
//打印鏈表——從第二個(gè)節(jié)點(diǎn)開(kāi)始打印
void printflist(struct Node*headnode)
{
?? ?struct Node* pmove = headnode->next;
?? ?printf("書名\t價(jià)格\t數(shù)量\n");
?? ?while (pmove!=NULL)
?? ?{
?? ??? ?printf("%s\t%.1f\t%d\n", pmove->data.name,pmove->data.price,pmove->data.num );
?? ??? ?pmove = pmove->next;
?? ?}
?? ?printf("\n");
}冒泡排序——通過(guò)價(jià)格
第一個(gè)for循環(huán)表示遍歷次數(shù),第二個(gè)for循環(huán)使相鄰的兩個(gè)元素進(jìn)行比較并交換
1 比較條件里,只用q指針即可
2 交換時(shí)需要?jiǎng)?chuàng)建一個(gè)臨時(shí)變量
//冒泡排序算法
void bubblesortlist(struct Node*headnode)
{
?? ?for (struct Node*p = headnode->next; p != NULL; p = p->next)
?? ?{
?? ??? ?for (struct Node*q = headnode->next; q->next != NULL; q = q->next)
?? ??? ?{
?? ??? ??? ?if (q->data.price > q->next->data.price)
?? ??? ??? ?{
?? ??? ??? ??? ?//交換
?? ??? ??? ??? ?struct book tempdata = q->data;
?? ??? ??? ??? ?q->data = q->next->data;
?? ??? ??? ??? ?q->next->data = tempdata;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?printflist(headnode);
}如果不儲(chǔ)存信息,那么每次在輸入信息完畢后關(guān)閉控制臺(tái),信息無(wú)法保留,所以我們通過(guò)文件的方式來(lái)儲(chǔ)存信息
文件寫操作
1 通過(guò)創(chuàng)建節(jié)點(diǎn)指針變量來(lái)遍歷輸出文件中的信息
2 通過(guò)fprintf可以將輸入的信息保持下來(lái)
//寫操作
void savefile(const char*filename, struct Node*headnode)
{
?? ?FILE*fp = fopen(filename, "w");
?? ?struct Node*pmove = headnode->next;
?? ?while (pmove != NULL)
?? ?{
?? ??? ?fprintf(fp, "%s\t%.1f\t%d\n", pmove->data.name, pmove->data.price, pmove->data.num);
?? ??? ?pmove = pmove->next;
?? ?}
?? ?fclose(fp);
?? ?fp = NULL;
}文件讀操作
1 當(dāng)用 “r”的形式打開(kāi)文件失敗時(shí),說(shuō)明沒(méi)有此文件,則可以用“w+”的形式打開(kāi),當(dāng)沒(méi)有文件時(shí),會(huì)創(chuàng)建一個(gè)文件
2 把讀取出的數(shù)據(jù)以表頭法插入到鏈表中則可以再次打印出信息
//文件讀操作
void readfile(const char *filename, struct Node*headnode)
{
?? ?FILE*fp = fopen(filename, "r");
?? ?if (fp == NULL)
?? ?{
?? ??? ?//不存在文件則創(chuàng)建
?? ??? ?fp = fopen(filename, "w+");
?? ?}
?? ?struct book tempdata;
?? ?while (fscanf(fp, "%s\t%f\t%d\n", tempdata.name, &tempdata.price, &tempdata.num) != EOF)
?? ?{
?? ??? ?insertbyhead(headnode, tempdata);
?? ?}
?? ?fclose(fp);
?? ?fp = NULL;
}剩余代碼
1 當(dāng)查找書籍時(shí)先用臨時(shí)指針接受找到書籍的指針,然后再打印書籍信息
//1 界面
void menu()
{
?? ?printf("---------------------------------\n");
?? ?printf("\t圖書管理系統(tǒng)\n");
?? ?printf("\t0.退出系統(tǒng)\n");
?? ?printf("\t1.登記書籍\n");
?? ?printf("\t2.瀏覽書籍\n");
?? ?printf("\t3.借閱書籍\n");
?? ?printf("\t4.歸還書籍\n");
?? ?printf("\t5.書籍排序\n");
?? ?printf("\t6.刪除書籍\n");
?? ?printf("\t7.查找書籍\n");
?? ?printf("---------------------------------\n");
?? ?printf("請(qǐng)輸入0~7\n");
}
//2 ?做交互
void keydown()
{
?? ?int input = 0;
?? ?struct book tempbook; ?//創(chuàng)建臨時(shí)變量,存儲(chǔ)書籍信息
?? ?struct Node*result = NULL; ? ? //創(chuàng)建臨時(shí)指針變量,指向查找書籍的節(jié)點(diǎn)
?? ?scanf("%d", &input);
?? ?switch (input)
?? ?{
?? ?case 0:
?? ??? ?printf("【退出】\n");
?? ??? ?printf("退出成功\n");
?? ??? ?system("pause");
?? ??? ?exit(0); ? //關(guān)閉整個(gè)程序
?? ??? ?break;
?? ?case 1:
?? ??? ?printf("【登記】\n");
?? ??? ?printf("輸入書籍的信息(name,price,num)");
?? ??? ?scanf("%s%f%d", tempbook.name, &tempbook.price, &tempbook.num);
?? ??? ?insertbyhead(headnode, tempbook);
?? ??? ?savefile("book.txt", headnode);
?? ??? ?break;
?? ?case 2:
?? ??? ?printf("【瀏覽】\n");
?? ??? ?printflist(headnode);
?? ??? ?break;
?? ?case 3:
?? ??? ?printf("【借閱】\n"); ? ? ? ? ?//書籍存在,數(shù)量-1
?? ??? ?printf("請(qǐng)輸入要借閱的書籍");
?? ??? ?scanf("%s", tempbook.name);
?? ??? ?result = searchbyname(headnode, tempbook.name);
?? ??? ?if (result == NULL)
?? ??? ?{
?? ??? ??? ?printf("沒(méi)有相關(guān)書籍,無(wú)法借閱");
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?if (result->data.num > 0)
?? ??? ??? ?{
?? ??? ??? ??? ?result->data.num--;
?? ??? ??? ??? ?printf("借閱成功");
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ??? ?printf("無(wú)庫(kù)存");
?? ??? ??? ?
?? ??? ?}
?? ??? ?break;
?? ?case 4:
?? ??? ?printf("【歸還】\n"); ? ? ?//書記歸還,數(shù)量+1
?? ??? ?printf("請(qǐng)輸入要?dú)w還的書籍");
?? ??? ?scanf("%s", tempbook.name);
?? ??? ?result = searchbyname(headnode, tempbook.name);
?? ??? ?if (result == NULL)
?? ??? ??? ?printf("來(lái)源非法");
?? ??? ?else
?? ??? ?{
?? ??? ??? ?result->data.num++;
?? ??? ??? ?printf("書籍歸還成功!");
?? ??? ?}
?? ?
?? ??? ?break;
?? ?case 5:
?? ??? ?printf("【排序】\n");
?? ??? ?bubblesortlist(headnode);
?? ??? ?savefile("book.txt", headnode);
?? ??? ?break;
?? ?case 6:
?? ??? ?printf("【刪除】\n");
?? ??? ?printf("輸入要?jiǎng)h除的書名");
?? ??? ?scanf("%s", tempbook.name);
?? ??? ?deletenodebyname(headnode, tempbook.name);
?? ??? ?savefile("book.txt", headnode);
?? ??? ?break;
?? ?case 7:
?? ??? ?printf("【查找】\n");
?? ??? ?printf("請(qǐng)輸入要查找的書籍");
?? ??? ?scanf("%s", tempbook.name);
?? ??? ?result = searchbyname(headnode, tempbook.name);
?? ??? ?if (result == NULL)
?? ??? ?{
?? ??? ??? ?printf("未找到相關(guān)信息!\n");
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf("書名\t價(jià)格\t數(shù)量\n");
?? ??? ??? ?printf("%s\t%.1f\t%d\t", result->data.name, result->data.price, result->data.num);
?? ??? ?}
?? ??? ?break;
?? ?default:
?? ??? ?printf("選擇錯(cuò)誤,請(qǐng)重新選擇:>");
?? ??? ?break;
?? ?}
}
int main()
{
?? ?headnode = createlisthead();
?? ?readfile("book.txt", headnode);
?? ?while (1)
?? ?{
?? ??? ?menu();
?? ??? ?keydown();
?? ??? ?system("pause");
?? ??? ?system("cls");
?? ?}
?? ?system("pause");
?? ?return 0;
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語(yǔ)言實(shí)現(xiàn)圖書管理系統(tǒng)
- C語(yǔ)言圖書管理系統(tǒng)簡(jiǎn)潔版
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單圖書管理系統(tǒng)
- C語(yǔ)言圖書管理系統(tǒng)課程設(shè)計(jì)
- C語(yǔ)言鏈表實(shí)現(xiàn)圖書管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的圖書管理系統(tǒng)
- C語(yǔ)言設(shè)計(jì)圖書登記系統(tǒng)與停車場(chǎng)管理系統(tǒng)的實(shí)例分享
- C語(yǔ)言實(shí)現(xiàn)圖書館管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)圖書管理系統(tǒng)(文件數(shù)據(jù)庫(kù))
- C語(yǔ)言實(shí)現(xiàn)圖書管理系統(tǒng)開(kāi)發(fā)
相關(guān)文章
C++課程設(shè)計(jì)之學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++課程設(shè)計(jì)之學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
用while判斷輸入的數(shù)字是否回文數(shù)的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了用while判斷輸入的數(shù)字是否回文數(shù)的簡(jiǎn)單實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
C語(yǔ)言動(dòng)態(tài)開(kāi)辟內(nèi)存詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言動(dòng)態(tài)開(kāi)辟內(nèi)存,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02
C/C++實(shí)現(xiàn)圖書信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了c/c++實(shí)現(xiàn)圖書信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
VS中PCL庫(kù)附加依賴項(xiàng)配置過(guò)程解析
這篇文章主要介紹了VS中PCL庫(kù)附加依賴項(xiàng)配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
C++ inline內(nèi)聯(lián)函數(shù)詳解
這篇文章主要介紹了C++ inline內(nèi)聯(lián)函數(shù)詳解,有感興趣的同學(xué)可以借鑒參考下2021-02-02

