C language linked list 1

Posted by izzy on Sat, 05 Feb 2022 11:37:43 +0100

1. Introduction of linked list

1.1 defects of array
(1) Array has two defects: all element types in an array must be the same; The number of elements in the array must be specified in advance and cannot be changed once it is specified
(2) Solution to array defects: the first defect is solved by the structure, which allows different types of elements.
(3) Solution to array defect: the second defect is to make a linked list, because the linked list is actually an array with variable number of elements.
1.2 what is a linked list
(1) A linked list is a list connected by chains. The table here refers to nodes one by one. There is some memory in the node that can be used to store data; The chain here refers to the method of linking each table. The method used to connect two tables in C language is pointer.
(2) The linked list is composed of several nodes (each node structure of the linked list is completely similar), and the nodes are composed of valid data and pointers. The effective data area is used to store information to complete the task, and the pointer area is used to point to the next node of the linked list to form the linked list.
1.3 function of linked list
(1) The linked list is to solve the problem that the size of the array cannot be dynamically expanded, so in fact, the linked list is used as an array. The linked list can be completed with array, and the linked list that can be completed with array can also be completed, but the linked list is more flexible than array.
(2) Linked list is used to store data. The advantage of linked list is flexible, and the advantage of array is simple to use
1.4 implementation of single linked list
1.4.1 node composition of single linked list
(1) The linked list is composed of nodes, which contain valid data and pointers.

#include <stdio.h>

//Build a node
struct node
{
	int data;			//Valid data
	struct node*pnext;	//Pointer to the next node
};

(2) The defined struct node is just a structure. It has no variable generation and does not occupy memory. The structure definition is equivalent to defining a template for the linked list, but there is not a node yet. When a node is needed to create the linked list in the future, you can use this template to copy one.

1.4.2 application and use of heap memory
(1) The memory requirements of the linked list are relatively flexible. You can't use stack or data segment. Only heap memory can be used.
(2) To create a linked list node using heap memory:

1,Apply for heap memory, which is the size of a node (check whether the application result is correct);
2,Clean up the heap memory applied for;
3,Treat the applied heap memory as a new node;
4,Fill in the valid data and pointer area of your new node.
#include <stdio.h>

//Build a node
struct node
{
	int data;			//Valid data
	struct node*pnext;	//Pointer to the next node
};

int main(void)
{
	//Create a linked list node
	struct node *p=(struct node*)malloc(sizeof(struct node));
	if(NULL==p)
	{
		printf("malloc error:\n");
		return -1;
	}
	
	//Clean up the heap memory requested
	bzero(p, sizeof(struct node));

	//Fill node
	p->data=1;
	p->pnext=NULL;		//The first address to point to the next node in the future
						//In practice, assign the pointer returned by the next node malloc to this node
	
}

1.4.3 head pointer of linked list
(1) The header pointer is not a node, but an ordinary pointer, accounting for only 4 bytes. The type of header pointer is struct node *, so it can point to the node of the linked list.
(2) A typical implementation of linked list is: the head pointer points to the first node of the linked list, then the pointer in the first node points to the next node, and so on until the last node. This forms a chain.
1.4.4 actual combat: build a simple single linked list

#include <stdio.h>

//Build a node
struct node
{
	int data;			//Valid data
	struct node*pnext;	//Pointer to the next node
};

int main(void)
{
	//Define pointer
	struct node *pHeader=NULL;

	//Create a linked list node
	struct node *p=(struct node*)malloc(sizeof(struct node));
	if(NULL==p)
	{
		printf("malloc error:\n");
		return -1;
	}
	
	//Clean up the heap memory requested
	bzero(p, sizeof(struct node));

	//Node fill
	p->data=1;
	p->pnext=NULL;		//The first address to point to the next node in the future
						//In practice, assign the pointer returned by the next node malloc to this node
	
	pHeader=p; 			//Associate this node with the header pointer in front of it


	//Each time a new node is created, the new node is associated with its previous node
	//Create a linked list node
	struct node *p1=(struct node*)malloc(sizeof(struct node));
	if(NULL==p1)
	{
		printf("malloc error:\n");
		return -1;
	}
	
	//Clean up the heap memory requested
	bzero(p1, sizeof(struct node));

	//Fill node
	p1->data=2;
	p1->pnext=NULL;		//The first address to point to the next node in the future
						//In practice, assign the pointer returned by the next node malloc to this node
	
	p->Next=p2; 			//Associate this node with the header pointer in front of it


	//Each time a new node is created, the new node is associated with its previous node
	//Create a linked list node
	struct node *p2=(struct node*)malloc(sizeof(struct node));
	if(NULL==p2)
	{
		printf("malloc error:\n");
		return -1;
	}
	
	//Clean up the heap memory requested
	bzero(p2, sizeof(struct node));

	//Fill node
	p2->data=3;
	p1->pnext=p2;		//The first address to point to the next node in the future
						//In practice, assign the pointer returned by the next node malloc to this node
	
	//So far, a linked list with 1 header pointer + 3 complete nodes has been created
}

Topics: C