This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
If you want to keep writing, write a series. What can I write about? Program = algorithm + data structure, which shows the importance of algorithm. This series of old poems tries to explain the algorithm clearly in the simplest language, from shallow to deep, if you are interested, you can pay attention to the column.
Several algorithms have been written in the previous columns, bubble sort, selection sort, insertion sort, and quicksort. In fact, there are still a lot of sorting, but personally feel that other sorting is not so important, unless the conditions are very harsh, otherwise we rarely encounter other sorting. So it’s enough to write the sorting algorithm here. Today I’m going to tell you the basic operation of a linked list.
Description of a linked list
What is a linked list? Sometimes the data in memory is not contiguous. In this case, we need to add a property to the data structure that records the address of the next data. Once you have the address, all the data is strung together like a chain, so the address property acts as a thread. As shown below, he is a piece of application address, connected in series.
The structure of a linked list
typedef struct _LINK_NODE
{
int data;
struct _LINK_NODE* next;
}LINK_NODE;
Copy the code
There’s data in the list and it doesn’t have to be an int, it can be some other type of data. Then there is a list pointer, which is used to connect to the next list.
Create a list
LINK_NODE* pLinkNode = NULL;
pLinkNode = (LINK_NODE*)malloc(sizeof(LINK_NODE));
pLinkNode->data = value;
pLinkNode->next = NULL;
Copy the code
Here is to add a new list node, the new added list node splicing on the original list has been on the line.
List insert
p=head;
while(p->next!=null)
{
p=p->next;
if(p->data ==value)
{
LINK_NODE* pLinkNode = NULL;
pLinkNode = (LINK_NODE*)malloc(sizeof(LINK_NODE));
pLinkNode->data = value;
pLinkNode->next = p->next;
p->next =pLinkNode;
}
}
Copy the code
I have drawn a sketch of the above code. In fact, its operation is that P is the scanning pointer. When the scanned position to be inserted is found, a new node is created.
Deletion of a linked list
p=head;
while(p->next!=null)
{
if(p->next->data ==value)
{
needdel = p->next;
p->next=needdel->next;
free(needdel);
}
}
Copy the code
Figure, the code above does the above.
Lists in general are not too difficult, but many students get confused when it comes to data structures, because they don’t know the list, and then a bunch of problems are hard to solve. In fact, we only need to step by step to simulate the pointer pointing, as long as enough care, it will be clear logic.
The picture is a temporary drawing, if the drawing is not good, I will be sorry.
To learn more algorithm problems, or to more project source code, please move to the public number: poetic code.
Now that I’m in, it’s not easy to be original. Let’s go with a “like”.