Basic use of linear singly linked list of data structure (creation, insertion, search, deletion)

1. Single linked list creation

There are two ways to create a singly linked list, namely the head insertion method and the tail insertion method.

1.1 Plug-in method

Every time a new node is inserted, it will be the new first data node, that is, it will come from behind.
The creation code is as follows, the creation method will contain the head node:

#include<stdio.h>
#include<stdlib.h>
typedef int ElementType;
typedef struct Lnode{
    
    
    ElementType data;
    struct Lnode *next;
}LNode, *Linklist;

Linklist Create_list_head(Linklist head){
    
      //头插法(含头结点)
    head = (Linklist)malloc(sizeof(LNode)); //为头指针开辟内存空间
    LNode * node = NULL;   //定义工作指针
    int count = 0;         //定义创建的结点数量
    head->next = NULL;
    scanf("%d", &count);
    for (int i = 0; i < count; i++){
    
    
        node = (LNode *)malloc(sizeof(LNode)); //创建新结点
        node->data = i;   //为新结点的数据域赋值
        node->next = head->next; //将新结点插入到头结点和第一个结点之间
        head->next  = node;  //更新头结点的next值为新结点
    }
    return head;
}

1.2 Tail insertion method

Each new node inserted will be added to the end of the data.
The creation code is as follows, the creation method will contain the head node:

#include<stdio.h>
#include<stdlib.h>
typedef int ElementType;
typedef struct Lnode{
    
    
    ElementType data;
    struct Lnode *next;
}LNode, *Linklist;

Linklist Create_list_tail(Linklist head){
    
    
	//尾插法(含头结点)
    head = (Linklist)malloc(sizeof(LNode));
    Linklist node = NULL;
    Linklist end = NULL;
    head->next = NULL;  //若没有后续结点,则将next值为null
    end = head; //若没有后续结点,则记录尾结点为头结点。
    int count = 0;  //结点个数
    scanf("%d", &count);
    for (int i = 0; i < count; i++){
    
    
        node = (LNode *)malloc(sizeof(LNode));  //为新结点开辟空间
        node->data = i;      //为新的结点数据域赋值
        end->next = node;   // 将新结点的值给上一个结点的next
        end = node;         //将新结点标记为尾结点
    }
    end->next = NULL;      //将尾结点的next值赋为NULL
    return head;
}

2. Singly linked list search

2.1 Search by serial number

Find the element with the serial number i (1<=i<=n) in the singly linked list L with the head node

bool Get_elem_l(Linklist L, int i){
    
     
    //在带头节点的单链表L中查找序号为i(1<=i<=n)的元素
    ElementType e;
    LNode * p;
    p = L->next;
    int j = 1;
    while (p && j < i){
    
      
        p = p->next;
        j++;
    }
    if (!p || j > i) return false;
    e = p->data;
    printf("元素已经找到,为:%d\n",e);
    return true;
}

2.2 Find by value

Find the element with the sequence number i (1<=i<=n) in the singly linked list L with the head node

bool Get_elem_l(Linklist L, int i){
    
     
    //在带头结点的单链表L中查找序号为i(1<=i<=n)的元素
    ElementType e;
    LNode * p;
    p = L->next;
    int j = 1;
    while (p && j < i){
    
      
        p = p->next;
        j++;
    }
    if (!p || j > i) return false;
    e = p->data;
    printf("元素已经找到,为:%d\n",e);
    return true;
}

3. Insert elements

Insert data element e at position i (1<=i<=n) in the singly linked list with the head node

bool List_insert(Linklist L, int i, ElementType e){
    
    
    //在带头结点的单链表中的第i(1<=i<=n)个位置插入数据元素e 
    LNode * p = L;
    int j = 0;
    while (p && j < i -1) {
    
    
        p = p->next;
        j++;
    }
    if (!p || j > i - 1) return false;
    LNode * new;
    new = (LNode *)malloc(sizeof(LNode));
    new->data = e;
    new->next = p->next;
    p->next = new;
    return true;
}


4. Delete elements

4.1 Delete in bit order

Delete the i (1<=i<=n) data element in the singly linked list L

bool List_delete_by_index(Linklist L, int i){
    
    
    //在单链表L中删除第i(1<=i<=n)个数据元素,此方法未考虑输入参数i大于链表元素个数的情况。
    LNode * p = L;
    ElementType e;
    int j = 0;
    while(p && j < i - 1){
    
    
        p = p->next;
        j++;
    }
    if (!(p->next) || j > i - 1) return false;
    LNode * q = p->next;
    p->next = q->next;
    e = q->data;
    printf("删除了位序为%d,其值为%d\n", i, e);
    free(q);
    return true;
}

4.2 Delete by value

Delete the first array element whose value is e in the singly linked list L

bool List_delete_by_v(Linklist L, ElementType e){
    
    
    LNode * p = L, * q = p->next;
    while (q != NULL && q->data != e){
    
    
        p = q;
        q = q->next;
    }
    if (!q) {
    
      // 此处若q指针为NULL,则说明找到了末尾仍未找到
        printf("删除元素%d不存在!\n", e);
        return false;
    }
    else{
    
    
        p->next = q->next;
        free(q);
        printf("删除元素%d成功!\n",e);
        return false;
    }
}

5. Access all elements in the linked list

Output the values ​​of all elements of a linked list with the head node.

void print_list(Linklist L){
    
    
    //访问单链表L的所有元素
    LNode * p;
    p = L->next;
    while (p){
    
    
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

6. Comparison with sequence table

The main advantages of linked list over sequential list are as follows:

  • When deleting the specified element, there is no need to move the element;
  • When inserting the specified element, there is no need to move the element.

The disadvantages of linked list compared with sequential list are as follows:

  • To access data elements at a certain location, a loop must be used;
  • It is more troublesome when exchanging the position of elements.

Guess you like

Origin blog.csdn.net/qq_41780234/article/details/127230956
Recommended