-1

当我创建一个链接列表来复制一个费用管理器时,我被困在寻找费用最大的那一天。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");
}
4

2 回答 2

0

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

在评论中,我建议你做max一个节点。你这样做了,但犯了几个错误:

  • max是未初始化的,这意味着坏事会发生。(C-speak 是“未定义的行为”。)初始化max = start。(看到没有初始化的指针定义应该引发一个危险信号。如果你不知道要初始化什么,至少 make it 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 == NULL andmax == NULL . That doesn't change, because the loop isn't entered and we terurnNULL`时,这是我们在这种情况下可以做的最好的事情。
  • 我已将节点的名称从更改new1为 just node。这是一个表面上的变化,但new对我来说,这表明正在创建一个节点,但由于我们只是在检查,这个名称可能会产生误导。小事很重要。(另外,我是个吹毛求疵的人。)
于 2019-10-19T19:25:10.337 回答
0

在与@MOehm 进行长时间的讨论后,我得出了以下结论:这段代码完全是@MOehm 的,我做了一些小的改动。(给他点赞)


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;
    }

}

我什至对插入代码进行了一些更改。我已经使用并修改了从本网站获取的代码的插入部分。

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;
    }
}

I haven't made any changes in the rest of the code.

Edit 1: Edited in the code for if the list is empty i.e. h==NULL

于 2019-10-19T22:00:39.810 回答