用带头结点的链表实现图书管理系统 数据结构作业

本文介绍了一个简单的图书管理系统的设计与实现过程,使用C++语言定义了图书结构,并通过链表进行图书数据的增删查操作。文章详细展示了如何创建图书节点、插入图书记录、删除指定图书以及显示所有图书信息。
/*
	Author:Skysys
	School:NCEPU
*/
#include<bits/stdc++.h>
using namespace std;
////////BASIC INFO//////////
#define ISBN_LENGTH 13
#define NAME_LENGTH 20
#define PRESS_LENGTH 20
#define AUTHOR_LENGTH 20
typedef  struct{
	char book_number[ISBN_LENGTH];
	char book_name[NAME_LENGTH];
	char pub_press[PRESS_LENGTH];
	char author_name[AUTHOR_LENGTH];
	double book_price;
}Book;
struct List{
	Book data;
	struct List* next;
};
bool initList(List** p){
	*p=(List*)malloc(sizeof(List));
	if(*p==NULL)return 0;
	(*p)->next=NULL; return 1;
}
bool insertList(List* it,Book tBook){
	while(it->next!=NULL){it=it->next;}
	it->next=(List*)malloc(sizeof(List));
	it->next->data=tBook; it->next->next=NULL;
	return 1;
}
bool deleteItem(List* it,char* ISBN){
	do{
		if(strcmp(ISBN,it->data.book_number)==0){
			while(it->next!=NULL)it=it->next; free(it);
			return 1;
		}
		it=it->next;
	}while(it!=NULL);
	return 0;
}
void displayList(List* it){
	int i = 0;it = it->next;
	do{
		Book& p = it->data;
		printf("[ID#%d %s %s %s %s %lf]\n",i+1,p.book_number,p.book_name,
			p.pub_press,p.author_name,p.book_price);
		it=it->next;
	}while(it!=NULL);
}
int main(){ 
	List *it;	Book tBook;		char ch;	int cnt=0;	initList(&it);
	do{
		cout<<"#DATA Format: book_number(str) book_name(str) pub_press(str) author_name(str) book_price(double)"<<endl;
		cout<<"[INPUT]>>>";
		cin>>tBook.book_number>>tBook.book_name>>tBook.pub_press
		   >>tBook.author_name>>tBook.book_price;getchar();
		if(!insertList(it,tBook)){cerr<<"INSERT ERROR";return 0;};
		cout<<"[Notice]Push Y/y for continue,others for break."<<endl;
		cout<<"[INPUT]>>>";
		ch=getchar();
	}while(ch=='Y'||ch=='y');
#define TEST_DEL
#define TEST_DEL	
	char ISBN[ISBN_LENGTH];
	cin>>ISBN;
	deleteItem(it,ISBN);
	displayList(it);
#define endif
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真·skysys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值