数据结构中添加了两个元素

时间:2014-01-31 16:44:18

标签: c arrays algorithm data-structures struct

我在c中遇到问题,我希望使用下面编写的代码来实现这种功能。输入和输出应该是这样的:

      Input Freq                                  Output Freq
1     0,1,4,2,5,3 add two first (0+1)             0,1,4,2,5,3,1
2     4,2,5,3,1   add min and last (2+1)          0,1,4,2,5,3,1,3
3     4,5,3,3     add min and last (3+3)          0,1,4,2,5,3,1,3,6
4     4,5,6      **here we add(4+5)**(minimum two)0,1,4,2,5,3,1,3,6,9
5     9,6         minimum two                     0,1,4,2,5,3,1,3,6,9,15
6     15

但是条件是必须没有元素交换没有排序,**但是我们可以处理元素的索引以进行比较,如果我们发现了在我们添加它们的任何索引处的正确元素并放在数组的最后。

我正在尝试一些基本的想法它正在为第一个if条件正常工作但在其他两个如果我想知道的情况下写什么。请帮助我。 假设data [i] .freq = {0,1,2,3,4,5}和data [i] .next指向下一个元素,如上例中第一步0指向1,此1现在指向对于由这两个得到的元素(所以这1指向1,而最后一个索引将指向下一个“1”(我们另外使用),所以下一个“1”是4,所以最后一个元素“1”指向4并且我们保持索引指向的方式相同。如果你不明白我的意思,请不要犹豫。 代码我猜应该非常接近这个:

    data[data_size].freq=data[0].freq+data[1].freq; // here i add the first 2 elements "0" and "1" in the example i given below.
    data[data_size].flag=0;  //I am using flag variable to show which elements are added or which are not added even once. If flag ="1" then that element is added if it "0" then not added even once.
    data[0].flag=1;
    data[1].flag=1;  //these two have been added.
    int count=5;
    do
    {
        for(i=0;data[i].next!=-1;i=data[i].next)
        { 
            if(data[data[i].next].freq>data[data_size].freq && data[data[i].next].flag==0)//Here i am setting flag=0 for those elements who not have been added yet. Because we don't have to take in account for addition those elements who are already added once.(step1 and step2 are coming in this loop)
            {
            data[data_size+1].freq= data[data_size].freq+ data[data[i].next].freq; 
            data[data_size].flag=1;//those elements which we are adding we set their flag to 1
            data[data[i].next].flag=1;
            data[data_size+1].flag=0;//this is the element onbtained on result will be sent to last index.With 0 flag because it is not added yet.
            data[data_size].next=data[i].next;
            data[i].next=data_size;
            data_size++; 
            } 
            if(data[data[i].next].freq<data[data_size].freq && data[data[i].next].flag==0)
            {
             //some code for step4 where 6>5 (in this case we added 5+4)
             data_size++; 
            }
            if(data[data[i].next].freq==data[data_size].freq && data[data[i].next].flag==0)
            {
               //Some code for step3 when element are equal
               data_size++; 
            }
        } 
        count--;
    } while(count>0)

会有不同的条件,例如(最后一个元素=右边找到的元素,例如步骤2中的3 + 3 = 6),找到的元素是saller,然后是最后一个元素,如5 + 4 = 9(参见步骤4)

如果有条件的话,还有其他两个想法吗?我的数组输入必须是{0,1,4,2,5,3}(我的意思是data [i] .freq),输出数组必须是{0,1,4,2,5,3,1,3,6,9,15}(数据) [data_size] .freq),没有任何排序,也没有任何交换,只使用索引移动,只使用数组。 如果有条件,请帮我写另外两个。

1 个答案:

答案 0 :(得分:0)

最后我能够解决我的问题。我使用队列来做到这一点,使用Front和Rear变量指向data []数组的起始和结束索引。以下是任何未来用户的等效帮助示例,如果遇到同样的问题(请注意,这里的每一件事都是静态的,但实现的逻辑是相同的:

#include <stdio.h>

void main() {
    int max = 50;
    int index = 0, Front = 0, Rear, min, min2, location, i, location2, flag[30], check = 0, j;

    int data[50] = {
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    };
    Rear = 9;
    for (i = 0; i <= Rear; i++) {
        flag[i] = 0;
    }
    int count = Rear;
    do {
        if (Front == 0) {
            printf("check1 \n ");

            Rear++;
            data[Rear] = data[Front] + data[1];
            flag[Front] = 1;
            flag[Rear] = 0;
            flag[1] = 1;
            printf("*****************dataRear: %d\n ", data[Rear]);
            Front = Front + 2;
        }

        if (data[Front] == data[Rear] && flag[Rear] == 0 && flag[Front] == 0) {
            printf("check3 \n ");
            data[Rear + 1] = data[Front] + data[Rear];
            printf("************dataRear[Rear+1]: %d\n ", data[Rear + 1]);
            flag[Front] = 1;
            flag[Rear] = 1;
            flag[Rear + 1] = 0;
            for (j = Front + 1; j <= Rear; j++) {
                if (flag[j] == 0) {
                    Front = j;
                    break;
                }
            }
            Rear++;
        }

        if (data[Front] < data[Rear] && flag[Rear] == 0 && flag[Front] == 0) {
            int start = Front + 2;
            min = data[Front];
            for (j = Front + 1; j <= Rear; j++) {
                if (flag[j] == 0) {
                    min2 = data[j];
                    location2 = j;
                    break;
                }
            }
            location = Front;

            for (i = start; i <= Rear; i++) {
                if (data[i] < min && flag[i] == 0) {
                    min = data[i];
                    location = i;
                    min2 = min;
                }
                if (data[i] < min2 && flag[i] == 0) {
                    min2 = data[i];
                    location2 = i;
                }
            }
            data[Rear + 1] = min2 + min;
            flag[location2] = 1;
            flag[location] = 1;
            flag[Rear + 1] = 0;

            for (j = location + 1; j <= Rear; j++) {
                if (flag[j] == 0) {
                    Front = j;
                    break;
                }
            }
            printf("*****************dataRear: %d\n ", data[Rear]);
            Rear = Rear + 1;
        }
        count--;
    } while (Front != Rear && count > 0);



    for (i = 0; i < 21; i++) {
        printf(" %d  ", data[i]);
    }
    printf("\n");
}