您的位置:首页 > 其它

单链表中插入元素之头插法

2019-04-06 12:32 176 查看
版权声明:如有侵权,请及时删除,引用请标明出处,联系:2195971474@qq.com https://blog.csdn.net/khqxf/article/details/89054242

头插法

#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;		//需要由一个临时变量作为转换
*library = book;
book->next = temp;
}
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;
}

结果演示:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐