单链表插入元素之尾插法

尾插法在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void getinput(struct Book *book);
void addBook(struct Book **library); 
void printLibrary(struct Book *library);
void releaseLibrary(struct Book **library);


struct Book
{
	char title[128];
	char author[40];
	struct Book *next;
};

void getinput(struct Book *book)
{
	printf("\n请输入书名:");
	scanf("%s",book->title);		//scanf中不要有换行符
	printf("\n请输入作者:");
	scanf("%s",book->author);
}
void addBook(struct Book **library) //采用头插法,往链表中插入元素,library作为头指针,两层解引用,因为它是指向结构体指针的指针
{
	struct Book *book,*temp;
	book = (struct Book *)malloc(sizeof(struct Book));		//为要插入的元素动态申请一块内存空间
	if(book == NULL)
	{
		printf("内存分配失败!\n");
		exit(1);
	}			
	getinput(book);				//获取插入元素的内容
	if(*library != NULL)		//如果插入的不是NULL链表
	{
		temp = *library;
		//定位单链表尾部的位置
		while(temp!=NULL)
		{
			temp = temp->next;
		}
		//插入数据
		temp->next = book;
		book->next = NULL;
	}
	else
	{
		*library = book;
		book->next = NULL;
	}
}
void printLibrary(struct Book *library)
{
	struct Book *book;
	int count = 1;
	book = library;
	while(book!=NULL)
	{
		printf("\nBook%d:\n",count);
		printf("\n书名:%s\n",book->title);
		printf("\n作者:%s\n",book->author);
		book = book ->next;
		count++; 
	}
}

void releaseLibrary(struct Book **library)
{
	struct Book *temp;
	while(*library != NULL)
	{
		temp = *library;
		*library = (*library)->next;	//考虑运算符的优先级,->的高于解引用*,故需要加括号。
		free(temp);
	}
}
int main(void)
{
	struct Book *library = NULL;
	int ch;
	while(1)
	{
		printf("\n请问是否需要输入书籍或者作者(Y/N): ");
		do
		{
			ch = getchar();
		}while(ch!='Y'&&ch!='N');
		if(ch=='Y')
		{
			addBook(&library);
		}
		else
		{
			break;
		}
	}
	printf("\n请问是否需要打印书籍信息(Y/N): ");
	do
		{
			ch = getchar();
		}while(ch!='Y'&&ch!='N');
		if(ch=='Y')
		{
			printLibrary(library);
		}
		else
		{
			releaseLibrary(&library);
		}

	return 0;
}

执行结果:
在这里插入图片描述
上面的方式,每次插入新元素时,都会遍历整个链表,看是否到达链表尾部,才执行插入操作,效率低,改进的方式是在addBook()中引入一个静态的结构体指针,始终指向链表的尾部。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void getinput(struct Book *book);
void addBook(struct Book **library); 
void printLibrary(struct Book *library);
void releaseLibrary(struct Book **library);


struct Book
{
	char title[128];
	char author[40];
	struct Book *next;
};

void getinput(struct Book *book)
{
	printf("\n请输入书名:");
	scanf("%s",book->title);		//scanf中不要有换行符
	printf("\n请输入作者:");
	scanf("%s",book->author);
}
void addBook(struct Book **library) //采用头插法,往链表中插入元素,library作为头指针,两层解引用,因为它是指向结构体指针的指针
{
	struct Book *book;
	static struct Book *tail;		//定义静态结构体指针,始终指向链表尾部
	book = (struct Book *)malloc(sizeof(struct Book));		//为要插入的元素动态申请一块内存空间
	if(book == NULL)
	{
		printf("内存分配失败!\n");
		exit(1);
	}			
	getinput(book);				//获取插入元素的内容
	if(*library != NULL)		//如果插入的不是NULL链表
	{
		//插入数据
		tail->next = book;
		book->next = NULL;
	}
	else
	{
		*library = book;
		book->next = NULL;
	}
	tail = book;		//保证tail指向链表尾部
}
void printLibrary(struct Book *library)
{
	struct Book *book;
	int count = 1;
	book = library;
	while(book!=NULL)
	{
		printf("\nBook%d:\n",count);
		printf("\n书名:%s\n",book->title);
		printf("\n作者:%s\n",book->author);
		book = book ->next;
		count++; 
	}
}

void releaseLibrary(struct Book **library)
{
	struct Book *temp;
	while(*library != NULL)
	{
		temp = *library;
		*library = (*library)->next;	//考虑运算符的优先级,->的高于解引用*,故需要加括号。
		free(temp);
	}
}
int main(void)
{
	struct Book *library = NULL;
	int ch;
	while(1)
	{
		printf("\n请问是否需要输入书籍或者作者(Y/N): ");
		do
		{
			ch = getchar();
		}while(ch!='Y'&&ch!='N');
		if(ch=='Y')
		{
			addBook(&library);
		}
		else
		{
			break;
		}
	}
	printf("\n请问是否需要打印书籍信息(Y/N): ");
	do
		{
			ch = getchar();
		}while(ch!='Y'&&ch!='N');
		if(ch=='Y')
		{
			printLibrary(library);
		}
		else
		{
			releaseLibrary(&library);
		}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值