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

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

}

相关推荐
计算机小白一个6 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^6 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
大数据追光猿8 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!9 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉9 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生9 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴9 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
Leuanghing9 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
qy发大财9 小时前
加油站(力扣134)
算法·leetcode·职场和发展
王老师青少年编程9 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛