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

标签 c linked-list doubly-linked-list

当我创建一个链接列表来复制费用经理时,我陷入了寻找费用最高的日子的困境。我以某种方式设法通过遍历找到了总计的最大值,但无法打印与之关联的日期。请帮忙。

我的结构代码:

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

最佳答案

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

在评论中,我建议您将 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时,那么node == NULL并且max == NULL。这不会改变,因为没有进入循环并且我们 terurnNULL`,这是在这种情况下我们能做的最好的事情。
  • 我已将节点名称从 new1 更改为 node。这是一个表面上的改变,但对我来说 new 表明正在创建一个节点,但由于我们只是检查,该名称可能会产生误导。小事情很重要。 (而且,我是个吹毛求疵的人。)

关于c - 具有多个元素的链表并找到最大的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58463251/

相关文章:

c - 在链表结构中获取 SIGSEGV 段错误

java - 排序链表

c - PathFindExtensionW 始终返回 "."而不是完整扩展名

c - 如何在 C 中将一个结构的内容复制到另一个结构中

c++ - 如何在 C++ 中修复 "Segmentation fault(core dump)"?

c++ - 我怎样才能访问双向链表中的嵌套结构?

java - DoublyLinkedList 的 toString()

c - 解析一个巨大的文本文件给我一个段错误

c - 使用 ptrace 抓取所有 printfs

c++ - "Guessing"从双向链表的哪一侧开始