C language to achieve linked list creation

Preface

Linked list is a very important content in data structure. To learn data structure, you must first understand linked list

Basic knowledge of linked lists

1. The composition of the linked list

The linked list is composed of a head pointer and a node, as shown in the figure:
Insert picture description here
and a node is a structure, including two parts: the data field and the pointer field. The pointer field stores the pointer to the next node.

2. For the operation of the linked list

1. Create a linked list
2, initialize a linked list
3, traverse the linked list
4, perform operations such as adding, deleting, and checking the linked list

C language's realization of linked list code

1. Create a structure and name it

Before creating a linked list, you need to create a structure as a node and head pointer:

typedef struct Node  //typedef方法函数可以对于struct Node进行重命名
{
    
    
    int i;
    struct Node *next;

}LNode,*LinkList;  //定义一个结构体指针方便后续的操作

2. Linked list initialization

Linked list initialization is to allocate space for the head pointer, and to ensure success

bool InitList(LinkList &L)   
{
    
    
    L=(LNode *)malloc(sizeof(LNode));   //为头结点动态分配内存
    if(L==NULL)
    {
    
    
        return false;
    }
    L->next=NULL;  //初始化时头结点一定要指向空
    return true;
}

3. Find the length of the linked list

By looping through the linked list and ending the traversal when the linked list is empty, the length is obtained

int GetListLength(LinkList L)
{
    
    

    int listlength=0;
    LNode *p=L->next;

    while(p)
    {
    
    
        listlength++;
        p= p->next;
    }
    return listlength;
}

4. Add value to the initial linked list

For adding values ​​to an empty linked list, there are head interpolation and tail interpolation

Head insertion method:

void HeadInsertList(LinkList &L,int n)  //n为插入数据的个数
{
    
    
    int data;
    //L->next=NULL; 初始化时已经指向空
    while(n--)
    {
    
    
        scanf("%d",&data);
        LNode *p=(LinkList)malloc(sizeof(LNode));
        p->i=data;
        p->next=L->next;
        L->next=p;
    }  
}

Head insertion method inserts data logic simple, but insert data and sort order in the linked listin contrast

Tail plug

void HeadInsertList(LinkList &L,int n)
{
    
    
    int data;
    LNode *End;
    End=L;
    while(n--)
    {
    
    
        scanf("%d",&data);
        LNode *p=(LinkList)malloc(sizeof(LNode));
        p->i=data;
        End->next=p;
        End=p;
    }
    End->next=NULL; //最后一个节点指针指向空
}

Compared with the head interpolation, the tail interpolation method is consistent with the data insertion sequence in the linked list.

5. Realize the traversal output of the linked list

void  TraverList(LinkList L)
{
    
    
    LNode *p=L->next;
    while(p)   //由于最后的节点指向空,可以通过这一特点结束遍历
    {
    
    
        printf("%d ",p->i);
        p=p->next;
    }
}

to sum up

The above are some processes in the creation of the linked list. You can use these methods to make your own linked list.

Guess you like

Origin blog.csdn.net/xinzhilinger/article/details/109011550