数据结构图书管理系统(链表)

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

//-----------------------------------------------//

// 定义图书节点结构体

typedef struct Node{

int id; //图书编码

char name[50]; //图书名称

float price; //价格

struct Node* next; //指针

}Node;

//-----------------------------------------------//

//1.显示菜单

void Menu(){

printf("\n=====图书管理系统=====\n");

printf("0. 退出系统\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("9. 按货名查找货物\n");

printf("10. 按编码查找货物\n");

printf("请输入操作编号:");

}

//-----------------------------------------------//

//2.创建新节点

Node* createNode(int id, char* name, float price){

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->id = id;

strcpy(newNode->name, name);

newNode->price = price;

newNode->next = NULL;

return newNode;

}

//-----------------------------------------------//

//3.在链表尾部添加货物

void addBook(Node* *head, int id, char* name, float price){

Node* newNode = createNode(id, name, price);

if (*head == NULL){

*head = newNode;

return;

}

Node* current = *head;

while (current->next != NULL) {

current = current->next;

}

current->next = newNode;

}

//-----------------------------------------------//

//4.删除指定编码的货物

void deleteBook(Node* *head, int id){

if (*head == NULL) return;

Node* current = *head;

Node* prev = NULL;

while (current != NULL && current->id != id){

prev = current;

current = current->next;

}

if (current == NULL){

printf("未找到编码为%d的货物\n", id);

return;

}

if (prev == NULL){

*head = current->next;

} else {

prev->next = current->next;

}

free(current);

printf("删除成功\n");

}

//-----------------------------------------------//

//5.修改指定编码的货物价格

void ChangeNotePrice(Node* head, int id, float newPrice){

Node* current = head;

while (current != NULL && current->id != id){

current = current->next;

}

if (current == NULL){

printf("未找到编码为%d的货物\n", id);

return;

}

current->price = newPrice;

printf("价格修改成功\n");

}

//-----------------------------------------------//

//6.修改指定编码的货物名称

void ChangeNoteName(Node* head, int id, char* newName){

Node* current = head;

while (current != NULL && current->id != id){

current = current->next;

}

if (current == NULL){

printf("未找到编码为%d的货物\n", id);

return;

}

strcpy(current->name, newName);

printf("名称修改成功\n");

}

//-----------------------------------------------//

//7.按编码排序插入货物

void insertBook(Node* *head, int id, char* name, float price){

Node* newNode = createNode(id, name, price);

if (*head == NULL || id < (*head)->id) {

newNode->next = *head;

*head = newNode;

return;

}

Node* current = *head;

while (current->next != NULL && current->next->id < id){

current = current->next;

}

newNode->next = current->next;

current->next = newNode;

}

//-----------------------------------------------//

//8.按冒泡排序给价格排序

void MaopaoPrice(Node* head) {

if (head == NULL) return;

Node* i;

Node* j;

int maopao;

for (i = head; i->next != NULL; i = i->next) {

maopao = 0;

for (j = head; j->next != NULL; j = j->next) {

if (j->price > j->next->price) {

int ChangeId = j->id;

char ChangeName[100];

strcpy(ChangeName, j->name);

float ChangePrice = j->price;

j->id = j->next->id;

strcpy(j->name, j->next->name);

j->price = j->next->price;

j->next->id = ChangeId;

strcpy(j->next->name, ChangeName);

j->next->price = ChangePrice;

maopao= 1;

}

}

if (!maopao) break;

}

}

//-----------------------------------------------//

//9.按插入排序给编码排序

void InputId(Node** head) {

if (*head == NULL) return;

Node* input = NULL;

Node* current = *head;

while (current != NULL) {

Node* next = current->next;

if (input == NULL || current->id < input->id) {

current->next = input;

input = current;

} else {

Node* temp = input;

while (temp->next != NULL && temp->next->id < current->id) {

temp = temp->next;

}

current->next = temp->next;

temp->next = current;

}

current = next;

}

*head = input;

}

//-----------------------------------------------//

//10.按名称查找货物

Node* SearchByName(Node* head, char* name) {

Node* current = head;

while (current != NULL) {

if (strcmp(current->name, name) == 0) {

return current;

}

current = current->next;

}

return NULL;

}

//-----------------------------------------------//

//11.按编码查找货物

Node* SearchById(Node* head, int id) {

Node* current = head;

while (current != NULL) {

if (current->id == id) {

return current;

}

current = current->next;

}

return NULL;

}

//-----------------------------------------------//

//12.显示所有货物

void OutputBooks(Node* head) {

if (head == NULL) {

printf("没有货物信息");

return;

}

printf("\n=====货物列表=====\n");

Node* current = head;

while (current != NULL) {

printf("编码: %d, 名称: %s, 价格: %.2f\n", current->id, current->name, current->price);

current = current->next;

}

}

//-----------------------------------------------//

//主函数

int main(){

Node* head=NULL;

Node* found;

int choice;

int id;

char name[50];

float price;

int pos;

while (1) {

Menu();

scanf("%d",&choice);

switch (choice) {

case 10:

printf("请输入要查找的货物编码:");

scanf("%d", &id);

found =SearchById(head, id);

if (found != NULL){

printf("找到货物: 编码: %d, 名称: %s, 价格: %.2f\n", found->id, found->name, found->price);

} else {

printf("未找到货物\n");

}

break;

default:

printf("无效的选择\n");

case 0:

printf("退出系统\n");

exit(0);

case 1:

printf("请输入货物编码:");

scanf("%d", &id);

printf("请输入货物名称:");

scanf("%s", name);

printf("请输入货物价格:");

scanf("%f", &price);

addBook(&head, id, name, price);

break;

case 2:

printf("请输入要删除的货物编码:");

scanf("%d", &id);

deleteBook(&head, id);

break;

case 3:

printf("请输入要修改价格的货物编码:");

scanf("%d", &id);

printf("请输入新的价格:");

scanf("%f", &price);

ChangeNotePrice(head, id, price);

break;

case 4:

printf("请输入要修改名称的货物编码: ");

scanf("%d", &id);

printf("请输入新的名称: ");

scanf("%s", name);

ChangeNoteName(head, id, name);

break;

case 5:

printf("请输入货物编码: ");

scanf("%d", &id);

printf("请输入货物名称: ");

scanf("%s", name);

printf("请输入货物价格: ");

scanf("%f", &price);

insertBook(&head, id, name, price);

break;

case 6:

MaopaoPrice(head);

printf("按价格排序成功。\n");

break;

case 7:

InputId(&head);

printf("按编码排序成功。\n");

break;

case 8:

OutputBooks(head);

break;

case 9:

printf("请输入要查找的货物名称:");

scanf("%s", name);

Node* found = SearchByName(head, name);

if (found != NULL){

printf("找到货物: 编码: %d, 名称: %s, 价格: %.2f\n", found->id, found->name, found->price);

} else {

printf("未找到货物\n");

}

break;

}

}

return 0;

}

相关推荐
_WndProc8 分钟前
C++ 日志输出
开发语言·c++·算法
薄荷故人_10 分钟前
从零开始的C++之旅——红黑树及其实现
数据结构·c++
努力学习编程的伍大侠21 分钟前
基础排序算法
数据结构·c++·算法
XiaoLeisj1 小时前
【递归,搜索与回溯算法 & 综合练习】深入理解暴搜决策树:递归,搜索与回溯算法综合小专题(二)
数据结构·算法·leetcode·决策树·深度优先·剪枝
Jasmine_llq1 小时前
《 火星人 》
算法·青少年编程·c#
闻缺陷则喜何志丹1 小时前
【C++动态规划 图论】3243. 新增道路查询后的最短距离 I|1567
c++·算法·动态规划·力扣·图论·最短路·路径
Lenyiin2 小时前
01.02、判定是否互为字符重排
算法·leetcode
鸽鸽程序猿2 小时前
【算法】【优选算法】宽搜(BFS)中队列的使用
算法·宽度优先·队列
Jackey_Song_Odd2 小时前
C语言 单向链表反转问题
c语言·数据结构·算法·链表