具有多个元素并找到最大元素的链表

问题描述 投票:-1回答:2

[当我创建链接列表以复制费用经理时,我一直坚持寻找费用最大的一天。我以某种方式设法通过遍历找到了total的最大值,但无法打印与之关联的day。请帮助。

我的结构的代码:

struct node{
int day;
int movies;
int groceries;
int travel;
int total;
struct node* left;
struct node* right;
};
void find_max()
{
    struct node *new1 = start;
    int max, c;
    if(start == NULL) {
        printf("List is empty\n");
        return;
    }
    else {
        max = start->total;
        while(new1 != NULL) {
            if(new1->total > max)
            {
                max = new1->total;
            }
            new1 = new1->right;
        }
    }
printf("The maximum spending was: %d",max);
}

这里,当我尝试打印new1->day时(我不确定这叫什么。这是分支吗?),它向我显示垃圾值或停止运行。

如何正确显示?

编辑(代码):


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

struct node{
int day;
int movies;
int groceries;
int travel;
int total;
struct node* left;
struct node* right;
};

void maximumNode();

//Main goes here, where I choose the option using switch case. Say the example is case 3

case 3:
        {
            maximumNode();
            break;
        }

//End of main

void maximumNode() {
    struct node *new1 = start;
    struct node *max;

    if(start == NULL) {
        printf("List is empty\n");
        return;
    }

    else {
        max->total = start->total;
        while(new1 != NULL) {
            if(new1->total > max->total)
            {
                max->total = new1->total;
            }
            new1 = new1->right;
        }
    }
printf("The maximum spending was: %d and the day was: %d\n\n",max->total, max->day);
}

这里,在将情况3输入到列表中后,我立即键入该程序,甚至无法运行。 (当我将max设为int值时运行)。

编辑2:我只是重新运行了代码,显然即使在插入时也犯了错误。很抱歉浪费大家的时间,感谢大家的支持。

我的插入代码,以防万一:

void Insert(int a, int b, int c, int d)
{
    struct node *temp,*t;
    int total1=b+c+d;
    temp=(struct node*)malloc(sizeof(struct node));
    if(start==NULL)
    {
        start=temp;printf("%d", total1);
        start->day=a;
        start->movies=b;
        start->groceries=c;
        start->travel=d;
        start->total=total1;
        start->left=NULL;
        start->right=NULL;
    }
    else
    {
       temp=start;
        while(temp->right!=NULL)
        {
            temp=temp->right;
        }
        t=(struct node*)malloc(sizeof(struct node));
        start->day=a;
        start->movies=b;
        start->groceries=c;
        start->travel=d;
        start->total=total1;
        t->right=NULL;
        t->left=temp;
        temp->right=t;
    }
    printf("\n\nYour expense has been saved successfully!\n\n");
}
c linked-list doubly-linked-list
2个回答
0
投票

在您发布的代码的第一个版本中,您只是将最大值保留为整数。如果您想查找该值,那很好,但是会丢失该节点的信息。

在评论中,我建议您将max设为节点。您做到了,但是犯了几个错误:

  • max未初始化,这意味着会发生不好的事情。 (C语言表示“未定义的行为”。)初始化max = start。 (在未初始化的情况下查看指针定义会引发一个红色标记。如果您不知道要初始化的内容,请至少将其设为NULL,以便以后可以对NULL进行检查。只写struct node *max;意味着max有一个不确定的值,您甚至无法检查!
  • 然后,当您找到更好的节点时,请勿设置max->total。这意味着您仅将第一个节点用作最大存储空间,从而更改了不需要的列表数据。设置新节点:

    if (new1->total > max->total) max = new1;
    

    ((如果您仔细阅读我的评论,那是我的建议。)>

  • 让我们实现它,并修复代码中的一些语义问题,请参见下面的注释:

const struct *node maximumNode()
{
    const struct node *node = start;
    const struct node *max = start;

    while (node != NULL) {
        if(node->total > max->total) {
            max = node;
        }

        node = node->right;
    }

    return max;
}

注意事项:

  • 函数现在返回对具有最大total的节点的引用。然后,调用代码可以打印信息或按需要以其他方式使用该节点,例如:

    const struct node *max = maximumNode();
    
    if (node) {
        printf("Max. total of %d was on day %d.\n",
            node->totel, node->day;
    }
    

    这比在适当的功能下进行打印更干净。这也将允许您在需要最大值的其他上下文中使用相同的功能。节点。

  • 我已经在函数中创建了节点指针const struct node *。这意味着您不能修改结构的内容。仅找到最大值就意味着您只检查列表,但不要更改它。使用此声明,编译器将抱怨尝试设置max->total,并且您会看到错误。
  • 您不需要NULL的第一个测试。当start == NULL, then alsonode == NULLandmax == NULL. That doesn't change, because the loop isn't entered and we terurnNULL`时,在这种情况下,我们可以做到最好。
  • 我将节点的名称从new1更改为node。这是一个表面上的更改,但是对我来说new建议创建一个节点,但是正如我们仅检查的那样,该名称可能会误导您。小事很重要。 (也是,我是一名拾荒者。)

经过长时间的工作和与@MOehm的长时间讨论之后,我得出的结论是:这段代码完全是@MOehm's,我做了一些小的更改。 (对他表示敬意)


void maximumNode()
{
    const struct node *node = h;
    const struct node *max = h;

    if (h==NULL)
    {
        printf("\n\nThe expense list is empty!\n\n");
    }
    else
    {
          while (node->next != NULL) {
        if(node->total > max->total) {
            max = node;
        }
        node = node->next;
    }
    if (node->next==NULL)
        {
            if (node->total > max->total)
            {
                max = node;
            }
        }
    printf("Max. total of %d was on day %d.\n",max->total, max->day);
    return;
    }

}

我什至对插入代码进行了一些更改。 I have used and modified the insertion part of the code taken from this website.

void create()
{
    int data;
    int d,m,g,t;
    int total1=0;
    temp =(struct node *)malloc(1*sizeof(struct node));
    temp->prev = NULL;
    temp->next = NULL;
    printf("\n\nDay: ");
    scanf("%d",&d);
    printf("\n\nEnter the expenses:\n1. Movies: ");
    scanf("%d",&m);
    printf("2. Groceries: ");
    scanf("%d",&g);
    printf("3. Travel: ");
    scanf("%d",&t);
    total1 = m+g+t;
    temp->day=d;
    temp->movies=m;
    temp->groceries=g;
    temp->travel=t;
    temp->total=total1;
}
void insert2()
{
    if (h == NULL)
    {
        create();
        h = temp;
        temp1 = h;
    }
    else
    {
        create();
        temp1->next = temp;
        temp->prev = temp1;
        temp1 = temp;
    }
}

我在其余的代码中没有做任何更改。

编辑1:在代码中编辑列表是否为空,即h==NULL


0
投票

经过长时间的工作和与@MOehm的长时间讨论之后,我得出的结论是:这段代码完全是@MOehm's,我做了一些小的更改。 (对他表示敬意)

© www.soinside.com 2019 - 2024. All rights reserved.