# 两个元素相加的数据结构

``````      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
``````

``````    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)
``````

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

0条评论