C 语言实例6——单链表 尾插法
1. 遍历方式(不推荐,效率比较低。需要遍历整个链表。)
// 链表的尾插入
#include<stdio.h>
#include<stdlib.h>
// 单链表声明
struct Book
{
//信息域
char title[128];
char author[40];
//指针域
struct Book * next;
};
void getInput(struct Book *book);
void addBook(struct Book **library) ;
void printlibrary(struct Book *library);
void relsaselibrary(struct Book **library);
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s",book->title);
printf("请输入作者:");
scanf("%s",book->author);
}
void addBook(struct Book **library) //参数library (head指针)是head指针的值。
{
struct Book *book, *temp;
// 在堆里面申请新的节点
book =(struct Book *)malloc(sizeof(struct Book));
if(book==NULL)
{
printf("内存分配失败");
exit(1);
}
// 为新节点填充信息域内容
getInput(book);
if(*library != NULL)
{
// 把新的节点插入到链表的尾部
temp = *library; //保存原来head指针指向的地址
while(temp->next !=NULL) // 定义到链表尾部
{
temp=temp->next;
}
// 插入数据
temp->next = book;
book->next=NULL;
}
else // 如果为NULL 空的单链表
{
*library = book;
book->next=NULL;
}
}
void printlibrary(struct Book *library)
{
struct Book *book;
int count =1;
book = library;
while(book != NULL)
{
printf("Book%d: " ,count);
printf("书名:%s",book->title);
printf("作者:%s\n",book->author);
book = book->next;
count++;
}
}
void relsaselibrary(struct Book **library)
{
struct Book *temp; // 临时变量
while(*library !=NULL) //直到library头指针指向NULL
{
temp = *library;
*library =(*library)->next;
free(temp);
}
}
int main()
{
struct Book *library = NULL;
int ch;
while(1)
{
printf("请问是否需要录入书籍信息(Y/N):");
do
{
ch = getchar();
}
while( ch !='Y' && ch!='N');
if(ch == 'Y')
{
addBook(&library);
}
else
{
break;
}
}
printlibrary(library);
relsaselibrary(&library);
return 0;
}
2. 不需要遍历,添加指向链表尾的指针(推荐使用,效率高)。
// 链表的尾插入(不需要遍历)
#include<stdio.h>
#include<stdlib.h>
// 单链表声明
struct Book
{
//信息域
char title[128];
char author[40];
//指针域
struct Book * next;
};
void getInput(struct Book *book);
void addBook(struct Book **library) ;
void printlibrary(struct Book *library);
void relsaselibrary(struct Book **library);
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s",book->title);
printf("请输入作者:");
scanf("%s",book->author);
}
void addBook(struct Book **library) //参数library (head指针)是head指针的值。
{
struct Book *book;
static struct Book *tail; //指向链表尾部
// 在堆里面申请新的节点
book =(struct Book *)malloc(sizeof(struct Book));
if(book==NULL)
{
printf("内存分配失败");
exit(1);
}
// 为新节点填充信息域内容
getInput(book);
if(*library != NULL)
{
// 插入数据
tail->next = book;
book->next=NULL;
}
else // 如果为NULL 空的单链表
{
*library = book;
book->next=NULL;
}
tail =book;
}
void printlibrary(struct Book *library)
{
struct Book *book;
int count =1;
book = library;
while(book != NULL)
{
printf("Book%d: " ,count);
printf("书名:%s",book->title);
printf("作者:%s\n",book->author);
book = book->next;
count++;
}
}
void relsaselibrary(struct Book **library)
{
struct Book *temp; // 临时变量
while(*library !=NULL) //直到library头指针指向NULL
{
temp = *library;
*library =(*library)->next;
free(temp);
}
}
int main()
{
struct Book *library = NULL;
int ch;
while(1)
{
printf("请问是否需要录入书籍信息(Y/N):");
do
{
ch = getchar();
}
while( ch !='Y' && ch!='N');
if(ch == 'Y')
{
addBook(&library);
}
else
{
break;
}
}
printlibrary(library);
relsaselibrary(&library);
return 0;
}
输出结果: