Single-linked watch head insertion method-small turtle

Head Insertion of Single Linked List

#include<stdio.h> 
#include<malloc.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[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,*temp;//定义一个结构体变量book,和一个临时用来存放头指针指向的地址的临时变量 
	book =(struct Book *) malloc(sizeof(struct Book));//为定义的结构体变量申请空间,存放于堆中 
	getInput(book);//为结构体变量book填充内容 
	
	if( *library != NULL)
	{
    
    
		temp = *library;//头指针指向的地址,存放于变量temp中 
		*library = book;//头指针指向新生成的book节点 
		book->next = temp;//新生成book节点的指针域指向,头指针指向的地址
	}
	else
	{
    
    
		*library = book;//头指针指向新生成的book节点 
		book->next = NULL;//新生成的节点的指针域指向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",book->author );
		book = book->next ;
		count++;
		printf("\n");
	}
}
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 i;
	for(i=0;i<3;i++) //3次循环,申请三个新节点
		addBook(&library);//修改指针的值,把指针的地址传进去 
	printLibrary(library);//传递头指针给打印函数 
	releaseLibrary(&library);//释放申请的空间 
	return 0;
}

Insert picture description here
This is what a singly linked list looks like. The head is the head pointer, which points to the information field of the first node, and the pointer field of the first node points to the information field of the next node. Knowing that the pointer field of the last node points to NULL, this linked list ends.
Explanation of this function:
Step 1: Declare that the head pointer points to NULL. Call the addBook function to add a new node. Note: (library is the address pointed to by the pointer variable library, &library is the address of the pointer variable, if you want to modify the content of the head pointer, you must pass the address of the head pointer, so what is passed in addBook is &library);
Step 2: In addBook Define a new node book, and apply for memory space for the book. Use the getInput function to fill in the content of the newly generated node. Note: (Due to the problem of changing the direction of the head pointer, the secondary pointer is used to accept the address of the head pointer).
Step 3: Add new nodes in the next cycle. Change the direction of the head pointer again. Point to the newly generated node, and the pointer field of the newly generated node points to the information field of the previous node. So insert the input later and output first. This is called the head insertion method of a singly linked list. As shown in the figure: the
Insert picture description here
dotted line connects the new node. Change the head pointer and the pointer field of the new node, and the new node is inserted.

Guess you like

Origin blog.csdn.net/qq_52208569/article/details/110187745