欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言單鏈表實(shí)現(xiàn)圖書管理系統(tǒng)

 更新時間:2022年03月11日 12:27:33   作者:不可觸碰的殤  
這篇文章主要為大家詳細(xì)介紹了C語言單鏈表實(shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語言單鏈表實(shí)現(xiàn)圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

單鏈表實(shí)現(xiàn)的圖書管理系統(tǒng)相比于結(jié)構(gòu)體實(shí)現(xiàn)的管理系統(tǒng),可以隨時開辟新的空間,可以增加書的信息

單鏈表的實(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()
{
?? ?//動態(tài)內(nèi)存申請
?? ?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;
}
//打印鏈表——從第二個節(jié)點(diǎn)開始打印
void printflist(struct Node*headnode)
{
?? ?struct Node* pmove = headnode->next;
?? ?printf("書名\t價格\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");
}

冒泡排序——通過價格

第一個for循環(huán)表示遍歷次數(shù),第二個for循環(huán)使相鄰的兩個元素進(jìn)行比較并交換
1 比較條件里,只用q指針即可
2 交換時需要創(chuàng)建一個臨時變量

//冒泡排序算法
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);
}

如果不儲存信息,那么每次在輸入信息完畢后關(guān)閉控制臺,信息無法保留,所以我們通過文件的方式來儲存信息

文件寫操作

1 通過創(chuàng)建節(jié)點(diǎn)指針變量來遍歷輸出文件中的信息
2 通過fprintf可以將輸入的信息保持下來

//寫操作
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”的形式打開文件失敗時,說明沒有此文件,則可以用“w+”的形式打開,當(dāng)沒有文件時,會創(chuàng)建一個文件
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)查找書籍時先用臨時指針接受找到書籍的指針,然后再打印書籍信息

//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("請輸入0~7\n");

}
//2 ?做交互
void keydown()
{
?? ?int input = 0;
?? ?struct book tempbook; ?//創(chuàng)建臨時變量,存儲書籍信息
?? ?struct Node*result = NULL; ? ? //創(chuàng)建臨時指針變量,指向查找書籍的節(jié)點(diǎn)
?? ?scanf("%d", &input);
?? ?switch (input)
?? ?{
?? ?case 0:
?? ??? ?printf("【退出】\n");
?? ??? ?printf("退出成功\n");
?? ??? ?system("pause");
?? ??? ?exit(0); ? //關(guān)閉整個程序
?? ??? ?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("請輸入要借閱的書籍");
?? ??? ?scanf("%s", tempbook.name);
?? ??? ?result = searchbyname(headnode, tempbook.name);
?? ??? ?if (result == NULL)
?? ??? ?{
?? ??? ??? ?printf("沒有相關(guān)書籍,無法借閱");
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?if (result->data.num > 0)
?? ??? ??? ?{
?? ??? ??? ??? ?result->data.num--;
?? ??? ??? ??? ?printf("借閱成功");
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ??? ?printf("無庫存");
?? ??? ??? ?
?? ??? ?}

?? ??? ?break;
?? ?case 4:
?? ??? ?printf("【歸還】\n"); ? ? ?//書記歸還,數(shù)量+1
?? ??? ?printf("請輸入要?dú)w還的書籍");
?? ??? ?scanf("%s", tempbook.name);
?? ??? ?result = searchbyname(headnode, tempbook.name);
?? ??? ?if (result == NULL)
?? ??? ??? ?printf("來源非法");
?? ??? ?else
?? ??? ?{
?? ??? ??? ?result->data.num++;
?? ??? ??? ?printf("書籍歸還成功!");
?? ??? ?}
?? ?
?? ??? ?break;
?? ?case 5:
?? ??? ?printf("【排序】\n");
?? ??? ?bubblesortlist(headnode);
?? ??? ?savefile("book.txt", headnode);
?? ??? ?break;
?? ?case 6:
?? ??? ?printf("【刪除】\n");
?? ??? ?printf("輸入要刪除的書名");
?? ??? ?scanf("%s", tempbook.name);
?? ??? ?deletenodebyname(headnode, tempbook.name);
?? ??? ?savefile("book.txt", headnode);
?? ??? ?break;
?? ?case 7:
?? ??? ?printf("【查找】\n");
?? ??? ?printf("請輸入要查找的書籍");
?? ??? ?scanf("%s", tempbook.name);
?? ??? ?result = searchbyname(headnode, tempbook.name);
?? ??? ?if (result == NULL)
?? ??? ?{
?? ??? ??? ?printf("未找到相關(guān)信息!\n");

?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf("書名\t價格\t數(shù)量\n");
?? ??? ??? ?printf("%s\t%.1f\t%d\t", result->data.name, result->data.price, result->data.num);
?? ??? ?}
?? ??? ?break;
?? ?default:
?? ??? ?printf("選擇錯誤,請重新選擇:>");
?? ??? ?break;
?? ?}
}
int main()
{
?? ?headnode = createlisthead();
?? ?readfile("book.txt", headnode);
?? ?while (1)
?? ?{
?? ??? ?menu();
?? ??? ?keydown();
?? ??? ?system("pause");
?? ??? ?system("cls");

?? ?}
?? ?system("pause");
?? ?return 0;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論