[Data structure] Determine the node with the largest value in a singly linked list of length n by traversing one trip (implemented in C language)

This question is page 51 of the textbook, question 6 of the algorithm question

Table of contents

1. Define the structure

2. Create a singly linked list with tail insertion

3. Traverse the singly linked list, find out the position of the node with the largest value, and output the data saved by the node

4. Design and realize this traversal and output of the largest node

Five, complete code

Six, run the test


1. Define the structure

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

2. Create a singly linked list with tail insertion

LNode* CreateAtTail(int a[], int n)
{
    int i;
    LNode* first, * s, * r;
    first = (LNode*)malloc(sizeof(LNode));
    first->next = NULL;
    r = first;
    for (i = 0; i < 5; i++)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = a[i];
        r->next = s;
        r = s;
    }
    r->next = NULL;
    return first;
}

3. Traverse the singly linked list, find out the position of the node with the largest value, and output the data saved by the node

void COMList(LNode* first)
{
    LNode *p;
    int max = 0;
    int count = 1;
    int i = 0;
    int biaoji = 1;
    p = first->next;
    max = p->data;
    while (p != NULL)
    {
        i++;
        if (p->data > max)
        {
            max = p->data;
            count = count + i - biaoji;
            biaoji = i;
        }  
        p = p->next;
    }
    printf("单链表中值最大的节点为第 %d 个值为 %d ", count, max);
}

 There is also why p=p->next is placed under the if conditional sentence. If it is placed above, p becomes NULL at the end of the comparison. At this time, when comparing p->data with max, there is no This node, how to compare.

And my code actually compares the first element. In fact, this step is unnecessary, because it will definitely not be larger than the initial max, that is, the first element itself. (But I can't figure out how to change it now)

yes!

I got it

The code is as follows, which is to change the loop condition (if you don’t change it here, if you only change if, p->next will have the above empty pointer again), the condition of the if conditional sentence is changed, and the value of max is changed. . Everyone can understand.

If you don’t know, leave a message and ask me~

void COMList(LNode* first)
{
    LNode *p;
    int max = 0;
    int count = 1;
    int i = 1;
    int biaoji = 1;
    p = first->next;
    max = p->data;
    while (p->next != NULL)
    {
        i++;
        if (p->next->data > max)
        {
            max = p->next->data;
            count = count + i - biaoji;
            biaoji = i;
        }
        
        p = p->next;
    }
    printf("单链表中值最大的节点为第 %d 个值为 %d ", count, max);
}

 

One of the more difficult things to understand here is the count operation:

A common mistake is to directly count++. The result of such a calculation is definitely wrong. For example, input five numbers (here I am testing that the length of the linked list is 5, all of which are integers), 1 3 2 5 4, here, The if conditional sentence will separate 2, so that the count will be reduced by 1, and so on. In a similar situation, look at the table length and data, and add 2, 3... and so on.

The correct solution is to use i to record the number compared to, and then use the variable biaoji to record the position of the biaoji when p->data was greater than max last time, and then add i-biaoji to the count, for example , In the last example, 1 3 2 5 4, the position of 3 is 2, and the position of 5 is 4. In this way, 4-2=2, count+2 is enough, and one will not be added, and the position will be miscalculated.

To give another example, for example, 1 2 2 2 5, when entering for the first time, the position of the first 2 is 2, the second 2, and the third 2 cannot enter, and when it reaches 5, the position is 5, with a difference of 5 -2=3, so count+3 is enough, and there will be no less addition.

If you don’t understand, let’s give another example, 1 2 3 4 5, 1 can’t enter, the first one to enter is 2, the position is 2, 3 enters, the position is 3, count+3-2, just count+1 up.

4. Design and realize this traversal and output of the largest node

int main()
{
    LNode* first;
    int a[5];
    int i = 0;
    int count = 0;
    printf("请输入5个数,进行初始化链表:\n");
    for (i = 0; i < 5; i++)
    {
        scanf_s("%d", &a[i]);
    }
    first = CreateAtTail(a, 5);
    COMList(first);
    return 0;
}

Five, complete code

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

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


void COMList(LNode* first)
{
    LNode *p;
    int max = 0;
    int count = 1;
    int i = 0;
    int biaoji = 1;
    p = first->next;
    max = p->data;
    while (p != NULL)
    {
        i++;
        if (p->data > max)
        {
            max = p->data;
            count = count + i - biaoji;
            biaoji = i;
        }
        
        p = p->next;
    }
    printf("单链表中值最大的节点为第 %d 个值为 %d ", count, max);
}
LNode* CreateAtTail(int a[], int n)
{
    int i;
    LNode* first, * s, * r;
    first = (LNode*)malloc(sizeof(LNode));
    first->next = NULL;
    r = first;
    for (i = 0; i < 5; i++)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = a[i];
        r->next = s;
        r = s;
    }
    r->next = NULL;
    return first;
}
int main()
{
    LNode* first;
    int a[5];
    int i = 0;
    int count = 0;
    printf("请输入5个数,进行初始化链表:\n");
    for (i = 0; i < 5; i++)
    {
        scanf_s("%d", &a[i]);
    }
    first = CreateAtTail(a, 5);
    COMList(first);
    return 0;
}

Six, run the test

 

 

 Please correct me if there is any mistake~

If you don’t understand, leave a comment and ask me~

Everyone go and try this code!

Guess you like

Origin blog.csdn.net/m0_57549888/article/details/124434196