单链表插入元素之尾插法

尾插法单链表插入元素之尾插法

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