[C Language/PTA] Single linked list node deletion

[C Language/PTA] Single linked list node deletion

Topic requirements

This question requires the implementation of two functions, which respectively store the read data as a singly linked list and delete all nodes in the linked list that store a given value. Linked list nodes are defined as follows:

struct ListNode { int data; ListNode *next; }; Function interface definition: struct ListNode *readlist(); struct ListNode *deletem( struct ListNode *L, int m ); The function readlist reads a series of positive integers from the standard input, according to Create a singly linked list in read order. When −1 is read, it means that the input is over, and the function should return a pointer to the head node of the singly linked list.






The function deletem deletes all the nodes storing m in the singly linked list L. Returns a pointer to the head node of the resulting linked list.

Sample referee test procedure:

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    
    
    int data;
    struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
    
    
     struct ListNode *p = L;
     while (p) {
    
    
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    
    
    int m;
    struct ListNode *L = readlist();
    scanf("%d", &m);
    L = deletem(L, m);
    printlist(L);

    return 0;
}

/* 你的代码将被嵌在这里 */

Input example:
10 11 10 12 10 -1
10

Sample output:
11 12

problem solving ideas

Combined with the given code, the code implementation process can be analyzed as follows:

Store the input data in the linked list through the readlist() function, then call the deletem() function to delete the specified elements in the linked list, and finally output the final linked list result through the printlist() function.

Among them, struct ListNode is a linked list node structure, which contains two member variables: int data indicates the data stored in the current node, and struct ListNode *next indicates the pointer to the next node.

  • The function void printlist(struct ListNode *L) is used to traverse the entire linked list and print out the value of each node;

  • The function struct ListNode *readlist() is used to read the input data and generate a linked list;

  • The function struct ListNode *deletem(struct ListNode *L, int m) is used to delete all nodes whose value is m in the linked list and return the processed linked list.

the code

struct ListNode *readlist()             // 读取链表
{
    
    
    struct ListNode *head,*p1,*p2;      // 定义头结点和两个临时指针变量
    int n=0;                            // 定义节点数量
    head = NULL;                        // 初始化头结点为空指针
    p1 = p2 = (struct ListNode*)malloc(sizeof(struct ListNode));  // 开辟新空间
    scanf("%d",&p1->data);              // 从键盘上读入第一个节点的数据
    while(p1->data!=-1)                 // 如果当前节点不是最后一个节点
    {
    
    
        n++;                            // 记录节点个数
        if(n==1)                        // 如果是头节点
            head = p1;                  // 将当前节点作为头结点
        else
            p2->next = p1;              // 将前一个节点指针指向当前节点

        p2 = p1;                        // 将 p1 赋值给 p2,以便在下一次循环中使用
        p1 = (struct ListNode*)malloc(sizeof(struct ListNode));   // 为下一节点开辟新空间
        scanf("%d",&p1->data);          // 读入下一个节点的数据
    }
    p2->next = NULL;                    // 最后一个节点指向 NULL
    return head;                        // 返回头结点指针
}

struct ListNode *deletem( struct ListNode *L, int m )    // 删除指定元素
{
    
    
    struct ListNode *p1,*p2;            // 定义两个指针变量
    p1 = L;                             // 将 L(链表的头结点)赋值给 p1
    while(p1!=NULL)                     // 循环遍历链表
    {
    
    
        if(p1->data==m)                 // 如果当前节点存储的数据等于待删除的数 m
        {
    
    
            if(p1==L)                   // 如果当前节点是头节点
                L = p1->next;           // 将头指针指向当前节点的下一个节点,即删除头节点
            else
                p2->next = p1->next;    // 将前一个节点的指针指向当前节点的下一个节点,即删除中间节点或尾节点
        }
        else
        {
    
    
            p2 = p1;                    // 将 p1 赋值给 p2,以便在下一次循环中使用
        }
        p1 = p1->next;                  // 将 p1 指向下一个节点
    }
    return L;                           // 返回操作后的链表
}

The complete code is as follows:

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    
    
    int data;                           // 当前节点存储的数据
    struct ListNode *next;              // 指向下一个节点的指针
};

struct ListNode *readlist();             // 读取链表
struct ListNode *deletem( struct ListNode *L, int m );   // 删除指定元素
void printlist( struct ListNode *L )     // 输出链表
{
    
    
     struct ListNode *p = L;
     while (p) {
    
                            // 循环遍历链表
           printf("%d ", p->data);      // 输出当前节点的数据
           p = p->next;                 // 寻找下一个节点
     }
     printf("\n");
}

int main()
{
    
    
    int m;                              // 定义待删除的数
    struct ListNode *L = readlist();    // 读取链表
    scanf("%d", &m);                    // 从键盘上输入待删除的数
    L = deletem(L, m);                  // 删除待删除的数并返回操作后的链表
    printlist(L);                       // 输出最终的链表

    return 0;
}

struct ListNode *readlist()             // 读取链表
{
    
    
    struct ListNode *head,*p1,*p2;      // 定义头结点和两个临时指针变量
    int n=0;                            // 定义节点数量
    head = NULL;                        // 初始化头结点为空指针
    p1 = p2 = (struct ListNode*)malloc(sizeof(struct ListNode));  // 开辟新空间
    scanf("%d",&p1->data);              // 从键盘上读入第一个节点的数据
    while(p1->data!=-1)                 // 如果当前节点不是最后一个节点
    {
    
    
        n++;                            // 记录节点个数
        if(n==1)                        // 如果是头节点
            head = p1;                  // 将当前节点作为头结点
        else
            p2->next = p1;              // 将前一个节点指针指向当前节点

        p2 = p1;                        // 将 p1 赋值给 p2,以便在下一次循环中使用
        p1 = (struct ListNode*)malloc(sizeof(struct ListNode));   // 为下一节点开辟新空间
        scanf("%d",&p1->data);          // 读入下一个节点的数据
    }
    p2->next = NULL;                    // 最后一个节点指向 NULL
    return head;                        // 返回头结点指针
}

struct ListNode *deletem( struct ListNode *L, int m )    // 删除指定元素
{
    
    
    struct ListNode *p1,*p2;            // 定义两个指针变量
    p1 = L;                             // 将 L(链表的头结点)赋值给 p1
    while(p1!=NULL)                     // 循环遍历链表
    {
    
    
        if(p1->data==m)                 // 如果当前节点存储的数据等于待删除的数 m
        {
    
    
            if(p1==L)                   // 如果当前节点是头节点
                L = p1->next;           // 将头指针指向当前节点的下一个节点,即删除头节点
            else
                p2->next = p1->next;    // 将前一个节点的指针指向当前节点的下一个节点,即删除中间节点或尾节点
        }
        else
        {
    
    
            p2 = p1;                    // 将 p1 赋值给 p2,以便在下一次循环中使用
        }
        p1 = p1->next;                  // 将 p1 指向下一个节点
    }
    return L;                           // 返回操作后的链表
}

Summarize

This question examines the basic operations of the linked list, including the creation, traversal, and deletion of specified elements of the linked list.
In addition, it also involves knowledge points such as how to use dynamic memory allocation functions mallocand function pointers.

I am Qiu said , see you next time.

Guess you like

Origin blog.csdn.net/2301_77485708/article/details/131220233