【问题标题】:Finding two minimum elements in a linked list在链表中查找两个最小元素
【发布时间】:2014-04-09 14:13:09
【问题描述】:

我正在尝试使用链表来增强我对指针的概念。

我已经成功创建了链表,它也给出了最少两个元素,但我尝试了元素的数量来测试它。

突然我发现它不适用于以下示例:

enter the size of node
4
start entering the number of elements until your size
6
1
7
59
Printing linked list
6-> 1-> 7-> 59-> 

 The two minimumnumbers are  min1 :1   and min2 : 1

我这样做的完整代码是:可以直接切换到函数find_two_min()

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

struct node 
{
    int freq;
    struct node * next;
};
typedef struct node node;
node * tree;

//Problem creating area is below (code for finding minimum two elements)
void find_two_min(node**List,node** lmin1,node** lmin2)
{
    node*temp=*List;    
    node*min1,*min2;
    node*var1=*List;
    node* second=(*List)->next;
    if(var1>second)
    {
      min2=var1;
      min1=second;
    }
    else
    {
      min1=var1;
      min2 =second;
    }
    while(temp!=NULL)
    {
        if(temp->freq<min2->freq)
        {
            min1=min2;
            min2=temp;    
        }
        else if(temp->freq<min1->freq)
        {
            min1=temp;
        }    
        temp=temp->next;
    }
    *lmin1=min1; 
    *lmin2=min2;
}

void main() 
{
    int size, data;
    node* min1, *min2;
    int count = 0; //this count flag is to check is it's first node or not inside the do-while loop.
    tree = NULL;
    printf("enter the size of node\n");
    scanf("%d", & size);
    printf("start entering the number of elements until your size\n");
    node * prev;
    do
    {
        scanf("%d", & data);
        if (count == 0)
        {
            node * temp;
            temp = (node * ) malloc(sizeof(node));
            temp-> freq = data;
            temp-> next = NULL;
            prev = temp;
            tree=prev;
        } 
        else
        {
            node * temp ;
            temp = (node * ) malloc(sizeof(node));
            temp-> freq = data;
            temp-> next = NULL;
            prev-> next = temp;
            prev=prev->next;
        }
        size--;
        ++count;
    }
    while (size > 0);

    printf("Printing linked list\n");
    node * temp1;
    temp1 = tree;
    while (temp1!= NULL) 
    {
        printf("%d-> ", temp1-> freq);
        temp1 = temp1-> next;
    }
    node*temp5=tree;
    find_two_min(&temp5,&min1,&min2);
   printf("\n The two minimumnumbers are  min1 :%d   and min2 : %d\n",min1->freq,min2->freq);

}

谁能告诉我为什么它不能在这个特定的例子上工作,而它在其他样本上工作?你能帮我改正吗?

【问题讨论】:

    标签: c algorithm data-structures linked-list singly-linked-list


    【解决方案1】:

    问题是你在开头任意设置min1 作为第一个元素,min2 作为第二个元素,然后 - 你从第一个元素开始遍历 (temp)。

    这使您读取前 2 个元素两次,如果其中一个是最小值 - 它也被插入两次,算法“认为”您有两个值为 1 的元素。

    解决这个问题的方法可能是以下两种方法之一:

    1. min1min2 设置为第一个元素,然后检查哪个确实更小,然后仅从第三个元素开始遍历 - 避免重新读取您已经阅读过的元素。
    2. (更好的解决方案)将min1min2 设置为大于元素范围的值。一个不错的选择是使用INT_MAX - 它可以确保从算法的第一步调用您的逻辑。

    【讨论】:

    • 是的,你是对的,我已更改代码以在 min1 和 min2 中查找 min 和 max ,但我不知道如何从第三个元素开始,因为它是一个链表。你能给我任何算法或代码作为参考吗?将是一个很大的帮助谢谢
    • @user234839 我建议使用第二个建议,而不是从第三个元素开始,它会产生更好的代码
    • 对不起,我无法理解你的第二个答案,实际上我不知道提前的元素范围是多少(我有义务不使用内置函数,如果是这样的话,因为目标是发展概念)。你还能帮我吗?谢谢
    • 您能在您给出的第一个答案中帮助我吗?谢谢
    • @user234839,错了。 temp 是第一个节点,temp-&gt;next 是第二个节点,temp-&gt;next-&gt;next 是第三个节点。我也提倡使用第二个建议,一定要注意@amit 的警告。
    【解决方案2】:

    您在第一个节点上启动temp,即使您在初始化时已经考虑了它和第二个节点。尝试在第三个节点启动它。

    【讨论】:

    • 是的,你是对的,我已更改代码以在 min1 和 min2 中查找 min 和 max ,但我不知道如何从第三个元素开始,因为它是一个链表。你能给我任何算法或代码作为参考吗?将是一个很大的帮助谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 2021-01-13
    • 2021-01-25
    相关资源
    最近更新 更多