C language foundation-linked list (1) origin, definition, difference between linked list and array

origin

The emergence of linked lists is because in many business scenarios,Add and delete operations, If you use an array, either causeMass data movement, Or cause a lot ofWaste of storage space. So the linked list came into being.

Linked list is a kind of linear list, it is like a chain, each node includesTwo parts: One is to store data elementsData field(May store multiple different types of data), the other is to store the address of the next nodePointer field. Very easy to add and delete operations, of course, because of the chain structure, it creates a general linked listDon't havePass the array subscript directly like an arrayRandom accessAbility.

definition

typedef struct Node{
    
    
        int data;
        struct Node *next;
}LNode, *LinkList;
int main()
{
    
    
 LNode point1 = {
    
    1,NULL};
}

Comparison of linked list and array

Array:

       int i;
        int array[] = {
    
    1,2,3};
        for(i=0;i<sizeof(array)/sizeof(array[0]);i++){
    
    
                printf("array[%d]=%d\n",i,array[i]);
        }
:Wq

Insert picture description here

Linked list

  LNode point1 = {
    
    1,NULL};
        LNode point2 = {
    
    2,NULL};
        LNode point3 = {
    
    3,NULL};

        point1.next = &point2;
        point2.next = &point3;
        printf("p1data=%d,p2data=%d,p3data=%d\n",point1.data,point1.next->data,point1.next->next->data);

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44933419/article/details/112393958