从任意位置删除链表中的最大数(C语言)

chhqkbe1  于 2022-12-17  发布在  其他
关注(0)|答案(1)|浏览(113)

我有这个代码:

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

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

void deallocate(Node **root)
{
    Node *curr = *root;
    while (curr != NULL)
    {
        Node *aux = curr;
        curr = curr->next;
        free(aux);
    }
    *root = NULL;
}

void insert_end(Node **root, int value)
{
    Node *new_node = malloc(sizeof(Node));
    if (new_node == NULL)
    {
        exit(1);
    }
    new_node->next = NULL;
    new_node->x = value;

    if (*root == NULL)
    {
        *root = new_node;
        return;
    }

    Node *curr = *root;
    while (curr->next != NULL)
    {
        curr = curr->next;
    }
    curr->next = new_node;
}

void deserialize(Node **root)
{
    FILE *file = fopen("duom.txt", "r");
    if (file == NULL)
    {
        exit(2);
    }

    int val;
    while (fscanf(file, "%d, ", &val) > 0)
    {
        insert_end(root, val);
    }
    fclose(file);
}
int largestElement(struct Node *root)
{
    int max = INT_MIN;
    while (root != NULL)
    {
        if (max < root->x)
            max = root->x;
        root = root->next;
    }
    return max;
}

void deleteN(Node **head, int position)
{
    Node *temp;
    Node *prev;
    temp = *head;
    prev = *head;
    for (int i = 0; i < position; i++)
    {
        if (i == 0 && position == 1)
        {
            *head = (*head)->next;
            free(temp);
        }
        else
        {
            if (i == position - 1 && temp)
            {
                prev->next = temp->next;
                free(temp);
            }
            else
            {
                prev = temp;

                // Position was greater than
                // number of nodes in the list
                if (prev == NULL)
                    break;
                temp = temp->next;
            }
        }
    }
}

int main()
{
    Node *root = NULL;

    printf("MENU:\n");
    printf("if you press 0 the list will be created \n");
    printf("if you press 1 the list will be printed on a screen\n");
    printf("if you press 2 it deletes the biggest element\n");
    printf("if you press 3 the program ends\n\n");

    int meniu;
    printf("press:\n");
    scanf("%d", &meniu);
    while (meniu != 3)
    {
        if (meniu == 0)
        {
            deserialize(&root);
            printf("sarasas sukurtas.\n");
        }

        if (meniu == 1)
        {
            for (Node *curr = root; curr != NULL; curr = curr->next)
                printf("%d\n", curr->x);
        }
        if (meniu == 2)
        {
            int max_element = largestElement(root);
            printf("%d max\n", max_element);
            deleteN(&root, max_element);

        }
        printf("press:\n");
        scanf("%d", &meniu);
    }

    deallocate(&root);
    return 0;
}

当我编译并运行delete函数时,它第一次只删除最大的数字,如果我第二次调用它,它会删除列表中的最后一个数字。有人能帮我解决这个问题吗?
我编辑了它,这样所有的代码都可以看到,因为很难像以前那样理解它

watbbzwu

watbbzwu1#

您的largestElement返回列表中最大的数据值。

int max_element = largestElement(root);
    printf("%d max\n",max_element);
    deleteN(&root, max_element);

使用返回值就好像它是具有最大元素的节点的位置。
您需要一个函数来返回具有最大数据值的元素的位置,而不是largestElement
它看起来像这样:

int positionOfLargestElement(struct Node* root)
{
  int pos = 0;
  int maxpos = 0;
  int max = INT_MIN;
  while (root != NULL) {
     ++pos;
     if (max < root->x)
     {
         max = root->x;
         maxpos = pos;
     }
     root = root->next;
  }
  return maxpos;
}

注意:确切的代码取决于第一个节点被认为是位置0还是位置1,但是原理是相同的。

相关问题