Detailed explanation of C linked list

During the recent C language tutoring process, I found that many students have not been very clear about the C language linked list, and recently, some students who are about to graduate have encountered the problem of single linked list in the application test work, and they are not very proficient in this content. . Today, let's talk about the basic usage of singly linked list.

linked list
1. It consists of structures and pointers.
2. It consists of two parts, one is the data field and the pointer field.
3. There are two types of nodes in a linked list: head nodes and general nodes. The head node has no data field .
4. The basic operations are: initializing the linked list, adding and deleting nodes, finding the length of the linked list, and so on.
struct Linknode{
int data;
struct Linknode *next;
};
There is a knowledge point here: this is the data structure of the linked list, which is composed of structures and pointers. The name of the structure is Linknode . But there is no structure variable defined in it, and the structure can only be used if we define the structure variable.
How to define structure variables?
There are two ways:
1。 struct Linknode Linklist;
2.typedef struct linknode Linklist.
Generally, we want to use the second type. The advantage is: when we define the structure variable again, we can use: Linklist p; and if we use the first type, we must also use struct Linknode p; we can know the second by comparing Seed is much simpler. Then we all use the second way to define structure variables.
Above we have defined a linked list:
1. Initialize the linked list.
#include<stdio.h>
#include<stdlib.h>
int InitLinkList(Linklist **Lnode)
{
*Lnode=(Linklist)malloc(sizeof(Linklist));//*Lnode is equal to L, and the allocation space for *Lnode is equivalent to the allocation of space for L in the main function.
if(!*Lnode)
return 0;
(*Lnode)->next=NULL;
}
When initializing the linked list, we used a 2-level pointer. Why? Because we want the head node generated by the InitLinkList function, the main function can also point to this head node. If we use a first-level pointer, when using malloc to allocate space, it will return the first address of the allocated space to the pointer variable Lnode, and the space cannot be obtained by the pointer variable L in the main function. So we're going to use a level 2 pointer.
void main()
{
Linklist *L;
InitLikList(&L);
}
2. Add linked list node
Adding a linked list node is actually very simple. Generally, three structure pointer variables and a loop structure are used.
InsertLinkList(Linklist *Lnode)
{
Linklist *p,*q;
int d;
{
scanf("%d",&d);
if(d==-9999)
 break;
p=Lnode->next;//p points to the head node
//Locate the position of the node q to be inserted through the while loop and the pointer variable p.
while(p)
p=p->next;
//generate a node to be inserted
q=(Linklist)malloc(sizeof(Linklist));//Apply for the node space to be inserted
q->data=d;//Fill the data field to be inserted into the node
q->next=p->next;//First fill the pointer field to be inserted into the node q for filling.
p->next=q;//Then modify the positioned p pointer field to point to q.
 
}while(9);//The condition for loop exit is the input data -9999
 
}
void main()
{
Linklist *L;
InitLinkList(&L);//Generate a head node
InsertLinkList(L);//Insert node
}
3. Find the length of the linked list:
int LengthLinkList(Linklist *Lnode)
{
int i=0;
Linklist *p;
p=Lnode->next;//p points to the first node of the linked list.
while(p)
{
i++;
p=p->next;
}
return i;
}
void main()
{
Linklist *L;
InitLinkList(&L);//Generate a head node
InsertLinkList(L);//Insert a node
LengthLinkList(L)// Find the length of the linked list.
}
4. Delete the node
Deleting a linked list node is actually very simple, generally using three structure pointer variables and a loop structure.
DestroyLinkList(Linklist *Lnode)
{
Linklist *p,*q;
p=Lnode;//p points to the head node of the linked list
while(p)
{
q=p->next;//q points to the next node of the current node.
free(p);//Release the current node
p=q;//p points to the next node
}
}
void main()
{
Linklist *L;
InitLinkList(&L);//Generate a head node
InsertLinkList(L);//Insert node
LengthLinkList(L)// Find the length of the linked list.
DestroyLinkList(L);//Delete the linked list node
}
As long as you master the basic knowledge points of the singly linked list, all problems will be OK. The basic knowledge points include: structure variables, structure pointer variables, head nodes and general nodes, data fields and pointer fields , while loop.
I hope everyone can pass the test smoothly in the future interview process. In the future, I will list the interview questions about linked lists separately. I hope everyone can have the basic knowledge points listed in this article today.

During the recent C language tutoring process, I found that many students have not been very clear about the C language linked list, and recently, some students who are about to graduate have encountered the problem of single linked list in the application test work, and they are not very proficient in this content. . Today, let's talk about the basic usage of singly linked list.

linked list
1. It consists of structures and pointers.
2. It consists of two parts, one is the data field and the pointer field.
3. There are two types of nodes in a linked list: head nodes and general nodes. The head node has no data field .
4. The basic operations are: initializing the linked list, adding and deleting nodes, finding the length of the linked list, and so on.
struct Linknode{
int data;
struct Linknode *next;
};
There is a knowledge point here: this is the data structure of the linked list, which is composed of structures and pointers. The name of the structure is Linknode . But there is no structure variable defined in it, and the structure can only be used if we define the structure variable.
How to define structure variables?
There are two ways:
1。 struct Linknode Linklist;
2.typedef struct linknode Linklist.
Generally, we want to use the second type. The advantage is: when we define the structure variable again, we can use: Linklist p; and if we use the first type, we must also use struct Linknode p; we can know the second by comparing Seed is much simpler. Then we all use the second way to define structure variables.
Above we have defined a linked list:
1. Initialize the linked list.
#include<stdio.h>
#include<stdlib.h>
int InitLinkList(Linklist **Lnode)
{
*Lnode=(Linklist)malloc(sizeof(Linklist));//*Lnode is equal to L, and the allocation space for *Lnode is equivalent to the allocation of space for L in the main function.
if(!*Lnode)
return 0;
(*Lnode)->next=NULL;
}
When initializing the linked list, we used a 2-level pointer. Why? Because we want the head node generated by the InitLinkList function, the main function can also point to this head node. If we use a first-level pointer, when using malloc to allocate space, it will return the first address of the allocated space to the pointer variable Lnode, and the space cannot be obtained by the pointer variable L in the main function. So we're going to use a level 2 pointer.
void main()
{
Linklist *L;
InitLikList(&L);
}
2. Add linked list node
Adding a linked list node is actually very simple. Generally, three structure pointer variables and a loop structure are used.
InsertLinkList(Linklist *Lnode)
{
Linklist *p,*q;
int d;
{
scanf("%d",&d);
if(d==-9999)
 break;
p=Lnode->next;//p points to the head node
//Locate the position of the node q to be inserted through the while loop and the pointer variable p.
while(p)
p=p->next;
//generate a node to be inserted
q=(Linklist)malloc(sizeof(Linklist));//Apply for the node space to be inserted
q->data=d;//Fill the data field to be inserted into the node
q->next=p->next;//First fill the pointer field to be inserted into the node q for filling.
p->next=q;//Then modify the positioned p pointer field to point to q.
 
}while(9);//The condition for loop exit is the input data -9999
 
}
void main()
{
Linklist *L;
InitLinkList(&L);//Generate a head node
InsertLinkList(L);//Insert node
}
3. Find the length of the linked list:
int LengthLinkList(Linklist *Lnode)
{
int i=0;
Linklist *p;
p=Lnode->next;//p points to the first node of the linked list.
while(p)
{
i++;
p=p->next;
}
return i;
}
void main()
{
Linklist *L;
InitLinkList(&L);//Generate a head node
InsertLinkList(L);//Insert a node
LengthLinkList(L)// Find the length of the linked list.
}
4. Delete the node
Deleting a linked list node is actually very simple, generally using three structure pointer variables and a loop structure.
DestroyLinkList(Linklist *Lnode)
{
Linklist *p,*q;
p=Lnode;//p points to the head node of the linked list
while(p)
{
q=p->next;//q points to the next node of the current node.
free(p);//Release the current node
p=q;//p points to the next node
}
}
void main()
{
Linklist *L;
InitLinkList(&L);//Generate a head node
InsertLinkList(L);//Insert node
LengthLinkList(L)// Find the length of the linked list.
DestroyLinkList(L);//Delete the linked list node
}
As long as you master the basic knowledge points of the singly linked list, all problems will be OK. The basic knowledge points include: structure variables, structure pointer variables, head nodes and general nodes, data fields and pointer fields , while loop.
I hope everyone can pass the test smoothly in the future interview process. In the future, I will list the interview questions about linked lists separately. I hope everyone can have the basic knowledge points listed in this article today.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325941197&siteId=291194637
Recommended