Implementation of linear linked list

Introduction to linear linked lists

The linked storage structure of a linear list is called a linear linked list, as shown in Figure 1. The linear linked list divides the storage space into several small blocks, each block occupies several bytes, and these small blocks are called storage nodes. The storage node is divided into two parts. One part is used to store the value of the data element, called the data domain; the other part is used to store the before and after relationship between elements, that is, the next element is stored in the storage sequence number (storage address). ), pointing to the consequent node. In the linear linked list, a special pointer HEAD is used to point to the node of the first data element in the linear linked list (that is, the address where the first element is stored). The last element in the linear list has no successor, so the pointer field of the last node in the linear linked list is empty (represented by NULL or 0), indicating the end of the chain.
Insert image description here
The storage sequence number of each element in the linear linked list is discontinuous, and the relationship between elements is inconsistent. The relationship between elements is indicated by the pointer of each node. The pointer HEAD points to the first element of the list. It is called the head pointer. When HEAD=NULL, it means that the linked list is empty. For a linear linked list, you can start from the head pointer and scan along the pointers of each node to all nodes in the linked list. This kind of linear linked list is called a linear singly linked list, that is, all nodes in the linked list can be scanned backward from the head of the list, but elements before the node cannot be scanned forward from the middle or end node.

Implementation of linear linked list

1. Insert node

When a node needs to be inserted into a linked list, it can be divided into three situations depending on the insertion position: inserting into the head of the linked list, that is, between the head node and the first node; inserting into a position in the middle of the linked list; Insert into the end of the linked list. Although the insertion position is different, the insertion method used is the same, as shown in Figure 2. First, you need to point the next pointer of the new node to the node after the inserted position; then insert the next pointer of the node before the position. Point to the insertion node to complete the insertion operation.
Insert image description here

link * insertElem(link * p,int elem,int add){
    
      
    link * temp=p; //创建临时结点temp  
    //首先找到要插入位置的上一个结点  
    for (int i=1; i<add; i++) {
    
      
        if (temp==NULL) {
    
      
            printf("插入位置无效\n");  
            return p;  
        }  
        temp=temp->next;  
    }      
    //创建插入结点c  
    link * c=(link*)malloc(sizeof(link));  
    c->elem=elem;  
    //向链表中插入结点  
    c->next=temp->next;  
    temp->next=c;  
    return  p;  
}  

2. Search and update

When you need to find certain data in a linear linked list, generally the linked list can only be accessed through the head node or head pointer, so the most common way to find a node is to traverse the nodes in the linked list one by one. The code example is as follows:

int selectElem(link * p,int elem){
    
      
    link * t=p;  
    int i=1;  
    while (t->next) {
    
      
        t=t->next;  
        if (t->elem==elem) {
    
      
            return i;  
        }  
        i++;  
    }  
    return -1;  
}  

If we need to modify and update the data of a node in the linked list, we first need to find the corresponding node through traversal, and then update its data field. The code example is as follows:

//更新函数,其中,add 表示更改结点在链表中的位置,newElem 为新的数据域的值  
link *amendElem(link * p,int add,int newElem){
    
      
    link * temp=p;  
    temp=temp->next;//在遍历之前,temp指向首元结点  
    //遍历到被删除结点  
    for (int i=1; i<add; i++) {
    
      
        temp=temp->next;  
    }  
    temp->elem=newElem;  
    return p;  
}  

Implementation of linear table insertion function:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stu{
    
    
    char  name[32];
    struct stu  *next;
};

/*创建链表,参数n是初始化链表时建立的数据个数
prev:指向当前结点的前一个结点
cur:指向当前结点
head:保存表头结点的指针*/
struct stu* CreatList(int n){
    
    
    int i;
    char age[12];
    struct stu *prev,*cur,*head;
    head=(struct stu*)malloc(sizeof(struct stu));
    if(head==NULL){
    
    
        printf("Can't alloc memory\n");
        return NULL;
    }
    prev=head;
    head->name[0]='\0';
    head->next=NULL;
    for(i=1;i<=n;i++){
    
    
        cur=(struct stu*)malloc(sizeof(struct stu));
        if(cur==NULL){
    
    
            printf("Can't alloc memory\n");
            return NULL;                
        }
    scanf("%s",cur->name);
    // 请在下面的Begin-End之间补充代码,插入结点。
    /********** Begin *********/
    cur->next = NULL;
    prev->next = cur;
    prev = cur;
    /********** End **********/
    }
    printf("线性链表创建成功!\n");
    return head;
}
/*遍历链表,打印链表数据*/
void Print(struct stu *head){
    
    
    struct stu *cur;
    cur=head->next;
    while(cur!=NULL){
    
    
        printf("%s\n",cur->name);
        cur=cur->next;
    }
}
int main(){
    
    
    int number=3;
    char _name[32];
    struct stu *head,*cur,*fro;
    head=CreatList(number);
    if(head==NULL)
        return -1;
    Print(head);
    return 0;   
}

Guess you like

Origin blog.csdn.net/qq_45257495/article/details/131879467