0
Follow
1
View

List bubble sort data loss problem

dujian0602 注册会员
2023-02-25 14:19

Refer to GPT and my own idea, this error is usually caused by the operation of the freed memory, it is recommended to check whether the code has access to the freed memory.

Depending on your description, you may need to check whether there is a problem with the adjustment process of the linked list structure that could cause the linked list structure to be broken. You can also check to see if memory is allocated and released correctly, if there are any memory leaks, etc.

Here are some problems and solutions that may cause access rights conflicts:

Accessing the memory that has been freed
If you try to access the memory region after the memory has been freed, you will result in an access permission conflict. To prevent this from happening, you can either set the pointer to NULL after freeing memory, or check to see if the pointer is NULL before freeing memory.

Memory out of bounds
Access outside the valid range of an array or pointer to a memory region results in an access permission conflict. To avoid this, check the valid range of an array or pointer before accessing it, or use a more secure data structure such as std::vector, etc.

Multiple threads accessing the same memory region
In multithreaded programming, if multiple threads access the same memory region at the same time, access permission conflicts may occur. To avoid this, you can use thread synchronization mechanisms such as locking.

Incorrect memory allocation and release
Incorrect operations during memory allocation and release may result in access permission conflicts. To prevent this from happening, you can carefully review the code that allocates and frees memory to ensure that it is correct. You can also use tools, such as valgrind, to check for memory allocation and release problems.
Based on the information you provided, the code needs to be modified as follows:

Dynamically allocates and frees memory for linked list nodes rather than creating nodes on the stack. This avoids data loss due to the small stack space.

Before freeing node memory, we need to set the next member of the node pointer to NULL. This avoids data loss due to hanging Pointers.

During the sorting process, it is necessary to determine if the list header pointer needs to be updated. If the list header pointer changes, the header pointer needs to be updated after the sorting is complete.

Before printing the result, you need to determine whether the linked list is empty. If the list is empty, a corresponding prompt should be printed.

Finally, you should avoid using Chinese characters and non-ASCII characters in if statements and printf statements to avoid coding problems.

Here is the modified code:

void Analysis() {
    system("cls");
    good *tail, *q, *p;
    tail = q = p = NULL;

    int count = 0, i, j;
    p = head;
    while (p != NULL) {
        count++;
        p = p->next;
    }

    good *a = (good*)malloc(sizeof(good));
    a->next = NULL;
    tail = a;
    p = head;
    for (i = 0; i < count - 1; i++) {
        j = count - i - 1;
        tail = a;
        tail->next = head;
        q = tail->next;
        p = q->next;
        while (j--) {
            if (q->profit < p->profit) {
                q->next = p->next;
                p->next = q;
                tail->next = p;
            }
            tail = tail->next;
            q = tail->next;
            if (q->next == NULL)
                break;
            else
                p = q->next;
        }
    }

    head = a->next;
    free(a);
    if (head == NULL) {
        printf("No product information entered!\n");
    } else {
        printf("The most profitable product is: %s\n", head->name);
        printf("The profit is: %.2f\n", head->profit);
        printf("It is recommended to increase sales of this product!\n");
    }
    system("pause");
}

Note that this is just a modification to the code you provided, and there may still be other problems. It is recommended that you carefully observe the output of the program running during testing so that problems can be detected and resolved promptly.

cs9_11 注册会员
2023-02-25 14:19
< div class = "md_content_show e397 data - v - 3967" = "" >

the reference answer ChatGPT

The problem with this code is that the list structure is not properly maintained when the list is bubbling, resulting in data loss or program exceptions.

Specifically, in bubbling list sort, the program uses the three Pointers tail, q, and p to traverse the list and swap elements. Where the tail pointer always points to the previous element of the current element, the q pointer points to the current element, and the p pointer points to the next element of the current element. If you need to switch the positions of elements pointing to q and p, the program does the following:

q->next = p->next;
p->next = q;
tail->next = p;

The purpose of these operations is to move the element pointed to by q to the end of the list. However, these operations do not properly maintain the structure of the list, sometimes resulting in missing list elements or program exceptions.

To properly maintain the structure of a linked list, you can use a temporary pointer variable t to hold the next element of the current element. When swapping elements, you simply point q to the element that t points to, then p to q, and finally tail to p. The specific operations are as follows:


t = p->next;
q->next = t;
p->next = q;
tail->next = p;

In this way, you can maintain the list structure properly and avoid data loss or program exceptions.

In addition, for memory release, only need to call free() function after the end of the linked list operation to release the opened memory can be, do not need to release in the middle of the function.

cy6715066 注册会员
2023-02-25 14:19
< div class = "md_content_show e397 data - v - 3967" = "" >

no, or there will be a loss of data.

dayin781m 注册会员
2023-02-25 14:19

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > according to provide you the code, may be due to at the end of the bubble sort, to release the list(a)(free) operation, leading to loss of data in the list. Because free(a) frees the header pointer a and all the nodes in the list, making the data in the list inaccessible.

To solve this problem, try putting the release operation last, making sure to release the list only after all operations have been executed. You can try to include a release operation in the last else branch, for example:

else {
    printf("利酒最大的商品是:Keln,%s\n", head->nane);
    printf("利用内:%d\n", head->profat);
    printf("建议管加该商品的销售!\n");
    system("pause");
    free(a); // 释放链表
}

Also, to ensure that the data in the linked list is not lost, you can print out the data in the linked list for easier debugging and observation before the release operation, for example:

good* temp = head;
while (temp != NULL) {
    printf("商品名称:%s,利润:%d\n", temp->name, temp->profit);
    temp = temp->next;
}
free(a); // 释放链表

This allows you to print out all the product information in the linked list before releasing it to ensure that the data is not lost.

Hopefully, these methods will help you solve the problem. If the problem persists, more information and details can be provided so that the problem can be better understood and resolved.

q455769050 注册会员
2023-02-25 14:19
< div class = " md_content_show“数据- v - 3967 e397 = " " >

void Analysis()
{
    system("cls");
    good* tail, * q, * p, * a;
    a = tail = (good*)malloc(LEN);
    p = head;
    int count = 0, i, j;
    while (p != NULL)
    {
        count++;
        p = p->next;
    }
    for (i = 0; i < count - 1; i++)
    {
        j = count - i - 1;
        tail = a;
        tail->next = head;
        q = tail->next;
        p = q->next;
        while (j--)
        {
            if (q->profit < p->profit)
            {
                q->next = p->next;
                p->next = q;
                tail->next = p;
            }
            tail = tail->next;
            q = tail->next;
            if (q->next == NULL)
                break;
            else
                p = q->next;
        }
    }
    if (head == NULL)
    {
        printf("尚未输入商品信息!\n");
        getchar();
    }
    else
    {
        printf("利润最大的商品是:%s\n", head->name);
        printf("利润为:%.2f\n", head->profit);
        printf("建议增加该商品的销售!\n");
    }
    system("pause");
    free(a);
}

csddjj1199 注册会员
2023-02-25 14:19

Please send the code

About the Author

Question Info

Publish Time
2023-02-25 14:19
Update Time
2023-02-25 14:19