小甲鱼 图书馆书籍管理,c语言实现书籍的增删查(指针+结构体+单链表相关知识,改暂时未做)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//实现单链表节点的增删改查操作 
struct Book
{
	char title[120];
	char author[40];
	struct Book *next;
};

void getInput(struct Book *book)        //输入一本书的相关信息 
{
	printf("请输入书名:");
	scanf("%s",(*book).title);
	printf("请输入作者:");
	scanf("%s",book->author);
}

void addBook(struct Book **library)      //添加一本书 
{
	struct Book *book;		//设置tail指针永远指向尾部  
	static struct Book *tail;  //这里很关键,必须是static类型的,不没有的话会造成每一次调用add函数时,tail重新初始化为空指针。 
	book=(struct Book *)malloc(sizeof(struct Book));
	if(book==NULL)
	{
		printf("内存分配失败了!");
		exit(1);
	}
	getInput(book);
	// 尾插法相应的操作        			
	if(*library==NULL)
	{
		*library=book;
		book->next=NULL;	
	}	
	else
	{             
		tail->next=book;
		book->next=NULL; 
	}
	tail=book;													
}

void printBook(struct Book *library)     //打印出所有书的信息 
{
	struct Book *book=library;
	while(book!=NULL)
	{
		printf("书名是:%s\n",book->title);
		printf("作者是:%s\n\n",book->author);
		book=book->next;              //遍历整个单链表,打印出所有的书籍! 
	}
} 

struct Book *searchBook(struct Book *library,char *target)     //一次搜索并返回一个符合条件的书籍(节点) 
{
	struct Book *book=library;
	while(book!=NULL)		//循环遍历单链表找到和关键字一致的节点 
	{
		if(!strcmp(book->title,target) || !strcmp(book->author,target))  	 
		{
			return book;
		}
		book=book->next;   //设置循环条件,使其指向下一个节点并继续查找 
	}
}

void printJieguo(struct Book *book)  //打印出搜索的结果 
{
	printf("书名是:%s\n",book->title);
	printf("作者是:%s\n\n",book->author);
}

//删除书籍功能 
struct Book *deleteBook(struct Book **library,char *target)
{
	struct Book *previous,*current,*fhz;
	previous=NULL;
	current=*library;
	while(current!=NULL && strcmp(current->title,target) && strcmp(current->author,target))
	{
		previous=current;
		current=current->next;
	}
	if(current==NULL)
	{
		fhz=NULL;
		return fhz;
	}
	else
	{
		if(previous==NULL)
		{
			fhz=*library;
			*library=current->next;
		}
		else
		{
			fhz=previous;
			previous->next=current->next;
		}
	}
	free(current);
	return fhz;
}

void releaseBook(struct Book **library)     //因为是释放空间,对它是写操作,所以实参传入的是头指针的地址 
{ 								//遍历整个单链表进行逐一的释放空间 
	struct Book *temp; 
	while(*library!=NULL)
	{
		temp=*library;
		*library=temp->next;
		free(temp);
	}
} 

int main()
{
 	struct Book *library=NULL,*book,*fhz; //library是头指针,指向单链表第一个元素,即存储的是单链表第一个元素的地址 
 	char ch;
	char target[100];	
	while(1)		//增加用户体验的操作,可以添加想要输入的书籍并一次性打印出来!!! 
	{
		printf("\n是否添加书籍(Y/N):");
		do							//防止用户乱输入字符 
		{
			ch=getchar();
		}while(ch!='Y' && ch!='N' && ch!='y' &&ch!='n');
		if(ch=='Y' || ch=='y')
		{
			addBook(&library);
		} 
		else
		{
			break;
		}
	}
	
	printf("\n是否打印书籍信息(Y/N):");
	do
	{
		ch=getchar();
	}while(ch!='Y' && ch!='N' && ch!='y' &&ch!='n');
	if(ch=='Y' || ch=='y')
	{
		printBook(library);		//不需要改值,所以直接传library值,即其指向的节点的地址;将单链表存储的内容一一打印出来
	}
	
	while(1)
	{
		printf("\n是否搜索相关书籍/作者(Y/N):");
		do
		{
			ch=getchar();
		} while(ch!='Y' && ch!='N' && ch!='y' &&ch!='n');
		if(ch=='Y' || ch=='y')
		{
			printf("请输入关键字:");
			scanf("%s",target);
			printf("以下为搜索结果:\n");
			book=searchBook(library,target);
			if(book==NULL)
			{
				printf("很抱歉,没能找到!");
			}
			else
			{
				printf("已找到符合条件的书籍……\n");
				do					//多次循环找到所有符合的条件,因为search函数一次只能找到一个符合条件的节点(书籍) 
				{
					printJieguo(book);
					book=searchBook(book->next,target);     
				}while(book!=NULL);	
			} 	
		}
		else
		{
			break;
		}
	}
	
	while(1)
	{
		printf("\n是否要删除书籍或某作者的所有书(Y/N):");
		do
		{
			ch=getchar();
		}while(ch!='Y' && ch!='N' && ch!='y' &&ch!='n');
		if(ch=='Y' || ch=='y')
		{
			printf("请输入你想删除的书籍或者作者:");
			scanf("%s",&target);
			fhz=deleteBook(&library,target);
			if(fhz==NULL)
			{
				printf("没找到!\n");
			} 
			else
			{
				printf("删除成功!结果如下……\n");	
				do
				{
					fhz=deleteBook(&fhz,target);
				}while(fhz!=NULL);
				printBook(library);
			}	
		}
		else
		{
			break;
		}
	}
	

	
	releaseBook(&library); 			//释放空间,刚刚malloc分配了内存空间 
	return 0;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值