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

#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;

}

相关推荐
YuTaoShao32 分钟前
【LeetCode 热题 100】48. 旋转图像——转置+水平翻转
java·算法·leetcode·职场和发展
生态遥感监测笔记1 小时前
GEE利用已有土地利用数据选取样本点并进行分类
人工智能·算法·机器学习·分类·数据挖掘
Tony沈哲1 小时前
macOS 上为 Compose Desktop 构建跨架构图像处理 dylib:OpenCV + libraw + libheif 实践指南
opencv·算法
刘海东刘海东2 小时前
结构型智能科技的关键可行性——信息型智能向结构型智能的转变(修改提纲)
人工智能·算法·机器学习
pumpkin845142 小时前
Rust 调用 C 函数的 FFI
c语言·算法·rust
挺菜的2 小时前
【算法刷题记录(简单题)003】统计大写字母个数(java代码实现)
java·数据结构·算法
mit6.8242 小时前
7.6 优先队列| dijkstra | hash | rust
算法
2401_858286113 小时前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
guygg884 小时前
基于matlab的FIR滤波器
开发语言·算法·matlab
双叶8364 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++