Wangdao Postgraduate Entrance Examination--》Single Linked List After-Class Exercises C Language Code Implementation (Sprint)

        Postgraduate entrance exams are a way for many computer science students to pursue higher academic qualifications and seek better employment prospects. In the postgraduate entrance examination process, data structure is a very important subject, and code implementation questions are one of the difficulties. In this article, we will explore how to improve your postgraduate entrance examination scores by implementing data structure code questions. Regardless of whether you have programming experience or not, this article will provide you with some simple but practical tips to help you deal with the data structure questions you encounter in the postgraduate entrance examination. Let’s embark on this challenging learning journey together!

Table of contents

First introduction to singly linked list

Question 1) Recursively delete the specified value in the linked list of nodes without the head

Question 2) Delete the specified value from the leading node linked list

Question 3) Reverse output of linked list values

Question 4) Delete the minimum node from the singly linked list with the head node

Question 5) Find the intersection of two linked lists

Question 6) Sorting of singly linked list

Question 7) Delete duplicate nodes

Question 8) Delete nodes with equal absolute values


First introduction to singly linked list

        Singly linked list is a common basic data structure. It consists of a series of nodes, each node contains two parts: data ( i.e. the stored element) and a reference to the next node (pointer or link). The nodes in a singly linked list are connected in order of their location in memory to form a linked structure.

The first node in a singly linked list is called the head node, and the pointer part of the last node points to a null value (usually expressed as null), indicating the end of the linked list.

Characteristics of singly linked lists:

1)Dynamics: The length of a singly linked list can be increased or decreased dynamically without pre-specifying the size.

2)Insertion and deletion are efficient: In a singly linked list, when inserting or deleting elements, you only need to modify the pointers between nodes, time The complexity is O(1).

3)Low random access efficiency: Unlike arrays, elements in singly linked lists are not stored in continuous memory space. Therefore, it is difficult to access quickly through the index. It needs to be traversed in order starting from the head node, and the time complexity is O(n).

Implementing a singly linked list usually includes the following steps(C language implementation):

1)Define the node structure:
First, you need to define a structure to represent the linked list node. This structure usually contains two parts: data members and pointer members pointing to the next node.

2)Creation and initialization of nodes:
can be done through dynamic memory allocation (malloc function) Create a new node and initialize the data and pointers in the node through assignment operations.

3)Inserting and deleting nodes:
You can write functions to insert new nodes in the linked list Or delete an existing node. These operations typically involve adjustments to pointers between nodes.

4)Traversal and access:
In order to access all nodes in the linked list, you need to use a loop Structure or recursion to access each node in turn and perform corresponding operations.

5)Release memory:
When the linked list is no longer needed, you need to ensure that all nodes are released. occupied memory to avoid memory leaks.

The specific code is implemented as follows:

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

// 定义链表节点的结构体
struct Node {
    int data;
    struct Node* next;
};

// 在链表末尾插入新节点
void appendNode(struct Node** headRef, int newData) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    struct Node* last = *headRef;

    newNode->data = newData;
    newNode->next = NULL;

    if (*headRef == NULL) {
        *headRef = newNode;
        return;
    }

    while (last->next != NULL) {
        last = last->next;
    }

    last->next = newNode;
}

// 打印链表中的所有节点数据
void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    // 初始化头节点
    struct Node* head = NULL;

    // 在链表末尾依次插入节点
    appendNode(&head, 1);
    appendNode(&head, 2);
    appendNode(&head, 3);

    // 打印链表
    printf("Linked list: ");
    printList(head);

    return 0;
}

Question 1) Recursively delete the specified value in the linked list of nodes without the head

Design a recursive algorithm to delete all nodes with value x in a singly linked list L without a head node.

Realistic path follow

First: The code defines a singly linked list data structure, each node contains an integer data and a pointer to the next node.
Then: In the deleteNodes function, first check whether the linked list is empty. If empty, returns empty. Otherwise, call the deleteNodes function recursively to delete the next node in the linked list.
Then: In the deleteNodes function, if the data of the current node is equal to the element x to be deleted, then the current node is deleted. The specific operation is to first save the pointer of the next node, then release the memory of the current node, and finally return the pointer of the next node.
Finally: In the main function, first obtain the length and elements of the singly linked list input by the user, then call the deleteNodes function to delete the specified element, and finally print the deleted list. Linked list results.

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

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

struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("链表为空,请重新创建:\n");
        exit(1);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

void insertNode(struct Node** head, int data) {
    struct Node* newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    struct Node* curr = *head;
    while (curr->next != NULL) {
        curr = curr->next;
    }
    curr->next = newNode;
}

struct Node* deleteNodes(struct Node* head, int x) {
    struct Node* prev = NULL;
    struct Node* curr = head;
    while (curr != NULL) {
        if (curr->data == x) {
            if (prev == NULL) {
                head = curr->next;
            }
            else {
                prev->next = curr->next;
            }
            free(curr);
            return head;
        }
        prev = curr;
        curr = curr->next;
    }
    return head;
}

void printList(struct Node* head) {
    while (head != NULL) {
        printf("%d ", head->data);
        head = head->next;
    }
    printf("\n");
}

int main() {
    struct Node* head = NULL;
    int n, data, x;

    printf("请输入单链表的长度: ");
    if (scanf_s("%d", &n) != 1 || n < 0) {
        printf("输入长度有误!请重新输入:\n");
        return 1;
    }

    printf("请输入单链表的元素: ");
    for (int i = 0; i < n; i++) {
        if (scanf_s("%d", &data) != 1 || data < 0) {
            printf("输入元素有误!请重新输入.\n");
            return 1;
        }
        insertNode(&head, data);
    }

    printf("请输入要删除的元素: ");
    if (scanf_s("%d", &x) != 1 || x < 0) {
        printf("删除元素有误!请重新删除.\n");
        return 1;
    }

    head = deleteNodes(head, x);

    printf("最终的单链表结果为: ");
    printList(head);

    return 0;
}

The results of executing the code are shown below:

Question 2) Delete the specified value from the leading node linked list

In the singly linked list L with the head node, delete all nodes with value x and release their space. Assume that the node with value x is not unique. Try writing an algorithm to achieve the above operation.

Realistic path follow

First: We define the node structure of a singly linked list and create a singly linked list with the head node.

Then: We implement the function of inserting new nodes into the singly linked list and deleting all nodes with the value x in the singly linked list.

Then: In the main function, we first enter the number and value of nodes in the singly linked list, insert these values ​​into the singly linked list, and enter the node to be deleted. value and call the function that deletes the node.

Finally: We print out the deleted singly linked list. This completes the four steps of the overall code.

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

// 定义单链表结点结构体
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// 创建带头节点的单链表
Node* createLinkedList() {
    Node* head = (Node*)malloc(sizeof(Node));
    head->next = NULL;
    return head;
}

// 向单链表中插入新结点
void insertNode(Node* head, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;

    Node* p = head;
    while (p->next != NULL) {
        p = p->next;
    }
    p->next = newNode;
}

// 删除单链表中所有值为 x 的结点
void deleteNodes(Node* head, int x) {
    Node* p = head->next;
    Node* prev = head;

    while (p != NULL) {
        if (p->data == x) {
            Node* temp = p;
            prev->next = p->next;
            p = p->next;
            free(temp);
        }
        else {
            prev = p;
            p = p->next;
        }
    }
}

// 打印单链表
void printLinkedList(Node* head) {
    Node* p = head->next;
    while (p != NULL) {
        printf_s("%d ", p->data);
        p = p->next;
    }
    printf_s("\n");
}

int main() {
    Node* list = createLinkedList();

    // 输入单链表数据
    int n;
    printf_s("请输入单链表的结点个数:");
    scanf_s("%d", &n);

    printf_s("请输入单链表的结点值:\n");
    for (int i = 0; i < n; i++) {
        int value;
        scanf_s("%d", &value);
        insertNode(list, value);
    }

    // 输入要删除的结点值
    int x;
    printf_s("请输入要删除的结点值:");
    scanf_s("%d", &x);

    // 删除值为 x 的结点
    deleteNodes(list, x);

    // 打印删除后的单链表
    printf_s("删除后的单链表:");
    printLinkedList(list);

    return 0;
}

The results of executing the code are shown below:

Question 3) Reverse output of linked list values

Let L be a singly linked list with the head node, and write an algorithm to output the value of each node in reverse from the end to the beginning.

Realistic path follow

First: The program defines a Link structure to represent the nodes of the linked list. Each node contains an integer data and a pointer to the next node.

Then: In the function reverseOutput, first determine whether the incoming linked list node pointer is empty, and if it is empty, return it directly. Otherwise, call the reverseOutput function recursively, passing in the node pointer to the next node. In this way, the nodes at the end of the linked list can be output in reverse order first.

Then: After the recursive call, print the data of the current node and use the printf_s function to output.

Finally: In the main function, first read the number of nodes entered by the user to create the linked list. Then define two pointer variables q and head, where q is used to point to the head pointer of the linked list, and head is used to point to the latest node. Next, create the nodes of the linked list by looping, creating a new node each time through the loop, letting head->next point to the new node, and then point head to the new node to ensure that head always points to the latest node. Finally, set the next pointer of the last node of the linked list to NULL and redirect head to q, that is, head points to the head node of the linked list. Finally, call the reverseOutput function to output the linked list elements in reverse order. Finally, return 0 to indicate that the program ends normally.

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

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

void reverseOutput(Link* p) {
	if (p == NULL) return;
	else {
		reverseOutput(p->next);
		printf_s("%d ", p->data);
	}
}

int main() {
	int n, data;
	printf_s("请输入创建链表的结点个数:");
	scanf_s("%d", &n);
	struct Link* q;
	struct Link* head = (struct Link*)malloc(sizeof(struct Link));
	head->next = NULL;
	q = head;
	for (int i = 0; i < n; i++)
	{
		struct Link* newP = (struct Link*)malloc(sizeof(struct Link));
		printf_s("请输入第 %d 个结点的值:",i+1);
		scanf_s("%d", &data);
		newP->data = data;
		newP->next = NULL;
		head->next = newP;
		head = head->next; // head要始终指向最新节点
	}
	head->next = NULL;
	head = q; // 最后head要指向头结点
	reverseOutput(head->next);
	return 0;
}

The results of executing the code are shown below: 

Question 4) Delete the minimum node from the singly linked list with the head node

Try to write an efficient algorithm to delete a minimum node in the singly linked list L with the head node (assuming that the minimum node is unique)

Realistic path follow

First of all: The code defines a linked list node structure Node, which contains the data field data and the pointer next pointing to the next node.

Then: Create a singly linked list with the head node through the createLinkedList function. Inside the function, we use the malloc function to allocate memory for the head node, and point the next pointer of the head node to NULL, indicating that the linked list is empty.

Then: Use the insertNode function to insert a new node into the linked list. This function accepts a linked list head node pointer head and the value to be inserted as parameters. Inside the function, we first create a new node newNode and allocate memory for it. Then, we traverse the linked list, find the tail node of the linked list, and insert the new node into the next pointer of the tail node.

Finally: Delete the minimum node in the linked list through the deleteMinNode function. This function accepts the head node pointer of the linked list as a parameter. Inside the function, we use two pointers prev and curr to traverse the linked list and find the minimum node and its previous node. Then, we point the `next` pointer of the previous node of the minimum value node to the node next to the minimum value node and release the memory of the minimum value node.

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

// 定义链表结点结构体
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// 创建带头结点的单链表
Node* createLinkedList() {
    Node* head = (Node*)malloc(sizeof(Node));
    head->next = NULL;
    return head;
}

// 向链表中插入新节点
void insertNode(Node* head, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;
    Node* curr = head;
    while (curr->next != NULL) {
        curr = curr->next;
    }
    curr->next = newNode;
}

// 删除最小值结点
void deleteMinNode(Node* head) {
    if (head == NULL || head->next == NULL) {
        return;
    }
    Node* prev = head;
    Node* curr = head->next;
    Node* minPrev = prev; // 最小值结点的前一个结点
    Node* minNode = curr; // 最小值结点
    while (curr != NULL) {
        if (curr->data < minNode->data) {
            minPrev = prev;
            minNode = curr;
        }
        prev = curr;
        curr = curr->next;
    }
    minPrev->next = minNode->next;
    free(minNode);
}

// 打印链表
void printLinkedList(Node* head) {
    if (head == NULL || head->next == NULL) {
        printf("链表为空\n");
        return;
    }
    Node* curr = head->next;
    while (curr != NULL) {
        printf("%d ", curr->data);
        curr = curr->next;
    }
    printf("\n");
}

int main() {
    Node* L = createLinkedList();

    int n;  // 链表长度
    printf("请输入链表的长度:");
    scanf_s("%d", &n);

    printf("请输入链表的元素:\n");
    for (int i = 0; i < n; i++) {
        int value;
        scanf_s("%d", &value);
        insertNode(L, value);
    }

    printf("原始链表:");
    printLinkedList(L);

    deleteMinNode(L);

    printf("删除最小值结点后的链表:");
    printLinkedList(L);

    return 0;
}

The results of executing the code are shown below:  

Question 5) Find the intersection of two linked lists

It is known that two linked lists A and B represent two sets respectively, and their elements are sorted in ascending order. Compile a function to find the intersection of A and B, and store it in the A linked list.

Realistic path follow

First: Create two singly linked lists A and B with head nodes through the createLinkedList function. This function allocates memory space and points the next pointer of the head node to NULL, indicating that the linked list is empty.

Then: Use the scanf_s function to obtain the integers n and m input by the user, which represent the lengths of linked lists A and B respectively.

Then: Call the findIntersection function, passing in the A linked list and the B linked list as parameters. This function is used to find the intersection of linked list A and linked list B, and store the result in linked list A.

Finally: Use the printLinkedList function to print out the original A and B linked lists and the intersection results.

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

typedef struct Node {
    int data;
    struct Node* next;
} Node;

// 创建带头结点的单链表
Node* createLinkedList() {
    Node* head = (Node*)malloc(sizeof(Node));
    head->next = NULL;
    return head;
}

// 向链表中插入新节点
void insertNode(Node* head, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;
    Node* curr = head;
    while (curr->next != NULL) {
        curr = curr->next;
    }
    curr->next = newNode;
}

// 求两个递增排序链表的交集,并将结果存放于A链表中
void findIntersection(Node* A, Node* B) {
    Node* currA = A->next;
    Node* currB = B->next;
    Node* prevA = A;
    Node* temp;

    while (currA != NULL && currB != NULL) {
        if (currA->data < currB->data) {
            prevA->next = currA->next;
            temp = currA;
            currA = currA->next;
            free(temp);
        }
        else if (currA->data > currB->data) {
            currB = currB->next;
        }
        else {
            prevA = currA;
            currA = currA->next;
            currB = currB->next;
        }
    }

    while (currA != NULL) {
        prevA->next = currA->next;
        temp = currA;
        currA = currA->next;
        free(temp);
    }
}

// 打印链表
void printLinkedList(Node* head) {
    if (head == NULL || head->next == NULL) {
        printf("链表为空\n");
        return;
    }
    Node* curr = head->next;
    while (curr != NULL) {
        printf("%d ", curr->data);
        curr = curr->next;
    }
    printf("\n");
}

int main() {
    Node* A = createLinkedList();
    Node* B = createLinkedList();

    int n, m; // A链表长度和B链表长度
    printf("请输入A链表的长度:");
    scanf_s("%d", &n);
    printf("请输入A链表的元素(递增排序):\n");
    for (int i = 0; i < n; i++) {
        int value;
        scanf_s("%d", &value);
        insertNode(A, value);
    }

    printf("请输入B链表的长度:");
    scanf_s("%d", &m);
    printf("请输入B链表的元素(递增排序):\n");
    for (int i = 0; i < m; i++) {
        int value;
        scanf_s("%d", &value);
        insertNode(B, value);
    }

    printf("原始A链表:");
    printLinkedList(A);
    printf("原始B链表:");
    printLinkedList(B);

    findIntersection(A, B);

    printf("A与B的交集结果:");
    printLinkedList(A);

    return 0;
}

The results of executing the code are shown below: 

Question 6) Sorting of singly linked list

There is a singly linked list L with a head node. Design an algorithm to make its elements in ascending order.

Realistic path follow

First of all: The linked list node structure ListNode is defined, including the data field and the pointer to the next node.

Then: Implement the function createNewNode to create a new node, which is used to allocate memory and initialize the new node.

Then: Implement the function insertInOrder to insert elements into an ordered linked list, find the appropriate position to insert a new node according to the size of the element, and maintain the order of the linked list.

Finally: In the main function, create the linked list L of the leading node through user input, and call insertInOrder to insert the input elements into the linked list in increasing order, and finally print the arrangement The contents of the linked list.

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

// 定义链表节点结构体
typedef struct ListNode {
    int data;
    struct ListNode* next;
} ListNode;

// 创建新节点
ListNode* createNewNode(int data) {
    ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// 将元素插入有序链表中
void insertInOrder(ListNode** head, int data) {
    ListNode* newNode = createNewNode(data);

    // 如果链表为空或者新节点的值小于头结点的值,则将新节点作为头结点
    if (*head == NULL || data < (*head)->data) {
        newNode->next = *head;
        *head = newNode;
    }
    else {
        ListNode* curr = *head;

        // 找到适当的位置插入新节点
        while (curr->next != NULL && data > curr->next->data) {
            curr = curr->next;
        }

        newNode->next = curr->next;
        curr->next = newNode;
    }
}

// 打印链表的内容
void printLinkedList(ListNode* head) {
    ListNode* curr = head;
    while (curr != NULL) {
        printf("%d ", curr->data);
        curr = curr->next;
    }
    printf("\n");
}

// 从键盘读取用户输入的整数
int readInt() {
    int num;
    scanf_s("%d", &num);
    return num;
}

// 主函数
int main() {
    // 创建带头结点的链表L
    ListNode* L = createNewNode(0);
    printf("请输入链表L的元素个数:");
    int n = readInt();
    for (int i = 0; i < n; i++) {
        printf("请输入第%d个元素:", i + 1);
        int data = readInt();
        insertInOrder(&L, data);
    }

    printf("排列后的链表L: ");
    printLinkedList(L->next);

    return 0;
}

The results of executing the code are shown below: 

Question 7) Delete duplicate nodes

Design an algorithm to accomplish the following function: delete duplicate nodes in a singly linked list.

Realistic path follow

First: Defines the structure of the linked list node.

Then: Implement the function of creating a new node and the function of printing the linked list.

Then: Implements the function to delete duplicate nodes. This function traverses the linked list, and for each node, traverses the linked list again to find and delete the node with the same data as the current node.

Finally: The main function is implemented. In the main function, the user is first asked to input the number of elements and specific values ​​of the linked list, and then creates a singly linked list based on the input. Then call the function to delete duplicate nodes, and then print out the linked list after deleting duplicate nodes. Finally, release the linked list memory.

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

// 定义链表节点结构体
typedef struct ListNode {
    int data;
    struct ListNode* next;
} ListNode;

// 创建新节点
ListNode* createNewNode(int data) {
    ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// 打印链表
void printList(ListNode* head) {
    ListNode* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

// 删除重复节点
void removeDuplicates(ListNode* head) {
    if (head == NULL) {
        return;
    }

    ListNode* current = head;
    while (current != NULL) {
        ListNode* runner = current;
        while (runner->next != NULL) {
            if (runner->next->data == current->data) {
                ListNode* duplicate = runner->next;
                runner->next = runner->next->next;
                free(duplicate);
            }
            else {
                runner = runner->next;
            }
        }
        current = current->next;
    }
}

// 释放链表内存
void freeList(ListNode* head) {
    ListNode* current = head;
    while (current != NULL) {
        ListNode* temp = current;
        current = current->next;
        free(temp);
    }
}

// 主函数
int main() {
    int n;
    printf("请输入链表元素个数:");
    scanf_s("%d", &n);

    ListNode* head = NULL;
    ListNode* tail = NULL;

    printf("请输入链表元素值:");
    for (int i = 0; i < n; i++) {
        int data;
        scanf_s("%d", &data);

        ListNode* newNode = createNewNode(data);

        if (head == NULL) {
            head = newNode;
            tail = newNode;
        }
        else {
            tail->next = newNode;
            tail = newNode;
        }
    }

    printf("原链表:");
    printList(head);

    removeDuplicates(head);

    printf("删除重复节点后的链表:");
    printList(head);

    // 释放链表内存
    freeList(head);

    return 0;
}

The results of executing the code are shown below: 

Question 8) Delete nodes with equal absolute values

Design an algorithm to accomplish the following function: delete elements with equal absolute values ​​in a singly linked list.

Realistic path follow

First of all: Define the structure of a linked list node, and implement the functions of creating new nodes, printing linked lists, deleting nodes with equal absolute values, and releasing linked list memory.

Then: The main function requires the user to input the number and specific values ​​of the linked list elements, and then creates a singly linked list based on the input.

Then: Call the removeAbsoluteDuplicates function to delete nodes with equal absolute values ​​in the linked list.

Finally: Print out the linked list after deleting nodes with equal absolute values, and release the memory of the linked list.

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

// 定义链表节点结构体
typedef struct ListNode {
    int data;
    struct ListNode* next;
} ListNode;

// 创建新节点
ListNode* createNewNode(int data) {
    ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// 打印链表
void printList(ListNode* head) {
    ListNode* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

// 删除绝对值相等节点
void removeAbsoluteDuplicates(ListNode* head) {
    if (head == NULL) {
        return;
    }

    ListNode* current = head;
    while (current != NULL && current->next != NULL) {
        ListNode* runner = current;

        while (runner->next != NULL) {
            if (abs(runner->next->data) == abs(current->data)) {
                ListNode* duplicate = runner->next;
                runner->next = runner->next->next;
                free(duplicate);
            }
            else {
                runner = runner->next;
            }
        }
        current = current->next;
    }
}

// 释放链表内存
void freeList(ListNode* head) {
    ListNode* current = head;
    while (current != NULL) {
        ListNode* temp = current;
        current = current->next;
        free(temp);
    }
}

// 主函数
int main() {
    int n;
    printf("请输入链表元素个数:");
    scanf_s("%d", &n);

    ListNode* head = NULL;
    ListNode* tail = NULL;

    printf("请输入链表元素值:");
    for (int i = 0; i < n; i++) {
        int data;
        scanf_s("%d", &data);

        ListNode* newNode = createNewNode(data);

        if (head == NULL) {
            head = newNode;
            tail = newNode;
        }
        else {
            tail->next = newNode;
            tail = newNode;
        }
    }

    printf("原链表:");
    printList(head);

    removeAbsoluteDuplicates(head);

    printf("删除绝对值相等节点后的链表:");
    printList(head);

    // 释放链表内存
    freeList(head);

    return 0;
}

The results of executing the code are shown below: 

Guess you like

Origin blog.csdn.net/qq_53123067/article/details/134318870