C链表-图书信息管理

供后续遗忘复习

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Book
{
	char id[20];
	char name[50];
	int price;
};
typedef struct LineList
{
	struct LineList* line;
	struct Book data;
}LineList;
LineList* TailSetList(LineList* Head)
{
	int n;
	LineList* r = Head;
	printf("请输入要添加几个对象:>");
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		LineList* node = (LineList*)malloc(sizeof(LineList));
		if (node == NULL)
		{
			return NULL;
		}
		printf("请输入要添加的书的书号:>");
		scanf("%s", node->data.id);
		printf("请输入要添加的书的书名:>");
		scanf("%s", node->data.name);
		printf("请输入要添加的书的价格:>");
		scanf("%d", &node->data.price);
		node->line = NULL;
		r->line = node;
		r = node;
	}
	return r;
}
void ShowLine(LineList* Head)
{
	LineList* p = Head->line;
	if (p == NULL)
	{
		printf("链表为空\n");
		return;
	}
	printf("%-20s\t%-20s\t%-5s\n", "书号", "书名", "价格");
	while (p)
	{
		printf("%-20s\t%-20s\t%-5d\n", p->data.id, p->data.name, p->data.price);
		p = p->line;
	}
	printf("\n");
}
int InsertLine(LineList* Head)
{
	int i, len = 0;
	LineList* p = Head;
	printf("请输入要插入的位置:>");
	scanf("%d", &i);
	while (p)
	{
		len++;
		if (i == len)
		{
			break;
		}
		p = p->line;
	}
	if (p == NULL || i != len)
	{
		return -1;
	}
	LineList* node = (LineList*)malloc(sizeof(LineList));
	if (node == NULL)
	{
		return -1;
	}
	printf("请输入要插入的书的书号:>");
	scanf("%s", node->data.id);
	printf("请输入要插入的书的书名:>");
	scanf("%s", node->data.name);
	printf("请输入要插入的书的价格:>");
	scanf("%d", &node->data.price);
	node->line = p->line;
	p->line = node;
	return 1;
}
int DeleteLine(LineList* Head)
{
	LineList* p = Head;
	LineList* r = p;
	int i, len = 0;
	if (p->line == NULL)
	{
		printf("链表为空\n");
		return 0;
	}
	printf("请输入要删除的结点:>");
	scanf("%d", &i);
	while (p && p->line)
	{
		len++;
		if (i == len)
		{
			break;
		}
		p = p->line;
	}
	if (p == NULL || p->line == NULL || i != len)
	{
		return -1;
	}
	r = p->line;
	p->line = p->line->line;
	free(r);
	r = NULL;
	return 1;
}
LineList* ClearList(LineList* Head)
{
	LineList* p = Head->line;
	LineList* r = Head->line;
	Head->line = NULL;
	while (p)
	{
		p = p->line;
		free(r);
		r = p;
	}
	return Head;
}
void menu2()
{
	printf("0-按书编号查找\n");
	printf("1-按书名称查找\n");
	printf("2-按书价格查找\n");
}
int Find_by_num(LineList* Head)
{
	char id2[20];
	int len = 0;
	LineList* p = Head->line;
	printf("请输入书号从左到右五位数:>");
	scanf("%s", id2);
	printf("%-4s\t%-20s\t%-20s\t%-5s\n", "位置", "书号", "书名", "价格");
	while (p)
	{
		len++;
		if (strncmp(p->data.id, id2, 5) == 0)
		{
			printf("%-4d\t%-20s\t%-20s\t%-5d\n", len, p->data.id, p->data.name, p->data.price);
		}
		p = p->line;
	}
	return 1;
}
int Find_by_name(LineList* Head)
{
	char name[50];
	int len = 0;
	LineList* p = Head->line;
	printf("请输入书名:>");
	scanf("%s", name);
	printf("%-4s\t%-20s\t%-20s\t%-5s\n", "位置", "书号", "书名", "价格");
	while (p)
	{
		len++;
		if (strcmp(p->data.name, name) == 0)
		{
			printf("%-4d\t%-20s\t%-20s\t%-5d\n", len, p->data.id, p->data.name, p->data.price);
		}
		p = p->line;
	}
	return 1;
}
int Find_by_price(LineList* Head)
{
	int price;
	int len = 0;
	LineList* p = Head->line;
	printf("请输入书的价格:>");
	scanf("%d", &price);
	printf("%-4s\t%-20s\t%-20s\t%-5s\n", "位置", "书号", "书名", "价格");
	while (p)
	{
		len++;
		if (p->data.price == price)
		{
			printf("%-4d\t%-20s\t%-20s\t%-5d\n", len, p->data.id, p->data.name, p->data.price);
		}
		p = p->line;
	}
	return 1;
}
int EditList(LineList* Head)
{
	int input, n, len = 0, ret;
	LineList* p = Head->line;
	int(*r[3])(LineList*) = { Find_by_num,Find_by_name,Find_by_price };
	menu2();
	printf("请选择:>");
	scanf("%d", &input);
	if (input < 0 || input>2)
	{
		printf("非法输入!\n");
		return -1;
	}
	ret = r[input](Head);
	if (ret == 1)
	{
		printf("查找完成!\n");
	}
	printf("请输入要修改的书的位置,没有则未找到!:>");
	scanf("%d", &n);
	while (p)
	{
		len++;
		if (len == n)
		{
			printf("请输入要修改的书的书号:>");
			scanf("%s", p->data.id);
			printf("请输入要修改的书的书名:>");
			scanf("%s", p->data.name);
			printf("请输入要修改的书的价格:>");
			scanf("%d", &p->data.price);
			break;
		}
		p = p->line;
	}
	if (p == NULL || n != len)
	{
		printf("非法输入!\n");
		return -1;
	}
	return 1;
}
void FindList(LineList* Head)
{
	int input, len = 0, ret;
	LineList* p = Head->line;
	int(*r[3])(LineList*) = { Find_by_num,Find_by_name,Find_by_price };
	menu2();
	printf("请选择:>");
	scanf("%d", &input);
	if (input < 0 || input>2)
	{
		printf("非法输入!\n");
		return;
	}
	ret = r[input](Head);
	if (ret == 1)
	{
		printf("查找完成!\n");
	}
	printf("如果列表为空则为未找到\n");
}
void SaveList(LineList* Head)
{
	FILE* pfwrite = fopen("book.dat", "wb");
	LineList* p = Head->line;
	if (pfwrite == NULL)
	{
		perror("SaveFile");
		return;
	}
	while (p)
	{
		fwrite(&(p->data), sizeof(struct Book), 1, pfwrite);
		p = p->line;
	}
	fclose(pfwrite);
	pfwrite = NULL;
	printf("保存成功!\n");
}
LineList* Loadbookdata(LineList* Head)
{
	int ret = 0;
	FILE* pfread = fopen("book.dat", "rb");
	LineList* r = Head;
	if (pfread == NULL)
	{
		perror("LoadData");
		return;
	}
	while (1)
	{
		LineList* node = (LineList*)malloc(sizeof(LineList));
		if (node == NULL)
		{
			return NULL;
		}
		ret = fread(&node->data, sizeof(struct Book), 1, pfread);
		if (ret == 0)
		{
			break;
		}
		node->line = NULL;
		r->line = node;
		r = node;
		
	}
	fclose(pfread);
	pfread = NULL;
	printf("加载完成!\n");
	return r;
}
void DestoryList(LineList* Head)
{
	LineList* r = Head;
	LineList* p = Head;
	while (p)
	{
		p = p->line;
		free(r);
		r = p;
	}
}
int main()
{
	LineList* Head = (LineList*)malloc(sizeof(LineList));
	LineList* r = Head;
	if (Head == NULL)
	{
		return 0;
	}
	Head->line = NULL;
	r = Loadbookdata(Head);
	int input, ret;
	do
	{
		printf("请选择 :>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			r = TailSetList(r);
			if (r == NULL)
			{
				r = Head;
			}
			break;
		case 2:
			ShowLine(Head);
			break;
		case 3:
			ret = InsertLine(Head);
			if (ret == -1)
			{
				printf("插入位置非法或插入失败!\n");
			}
			else
			{
				printf("插入成功!\n");
			}
			break;
		case 4:
			ret = DeleteLine(Head);
			if (ret == -1)
			{
				printf("删除位置非法或删除失败!\n");
			}
			else if (ret == 0);
			else
			{
				printf("删除成功!\n");
			}
			break;
		case 5:
			r = ClearList(Head);
			if (r->line == NULL)
			{
				printf("清空完成!\n");
			}
			else
			{
				printf("可能未完全清空!!\n");
			}
			break;
		case 6:
			ret = EditList(Head);
			if (ret == 1)
			{
				printf("编辑完成!\n");
			}
			else
			{
				printf("未成功编辑\n");
			}
			break;
		case 7:
			FindList(Head);
			break;
		case 8:
			SaveList(Head);
			break;
		case 0:
			SaveList(Head);
			DestoryList(Head);
			break;
		}
	} while (input);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值