c - data strucure of addition of two elements -


i have problem in c, want achieve kind of thing using code written below. input , output should this:

      input freq                                  output freq 1     0,1,4,2,5,3 add 2 first (0+1)             0,1,4,2,5,3,1 2     4,2,5,3,1   add min , last (2+1)          0,1,4,2,5,3,1,3 3     4,5,3,3     add min , last (3+3)          0,1,4,2,5,3,1,3,6 4     4,5,6      **here add(4+5)**(minimum two)0,1,4,2,5,3,1,3,6,9 5     9,6         minimum 2                     0,1,4,2,5,3,1,3,6,9,15 6     15 

but condition there must no swaping of elements, no sorting, **but can deal index of element comparison , once if found correct element @ index add them , put @ last of array.

i trying basic idea working correctly first if condition write in other 2 if conditions want know.please me there. suppose data[i].freq={0,1,2,3,4,5} , data[i].next points next element in example above in first step 0 points 1 , 1 points element obtained these two(so 1 points 1 @ alst , 1 @ last index point next of "1" (which used in addition)so next of "1" 4 , last element "1" points 4 , same way maintain index pointing). please not hesitate me ask if have not understood mean say. code guess should close :

    data[data_size].freq=data[0].freq+data[1].freq; // here add first 2 elements "0" , "1" in example given below.     data[data_size].flag=0;  //i using flag variable show elements added or not added once. if flag ="1" element added if "0" not added once.     data[0].flag=1;     data[1].flag=1;  //these 2 have been added.     int count=5;         {         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 setting flag=0 elements not have been added yet. because don't have take in account addition elements added once.(step1 , step2 coming in loop)             {             data[data_size+1].freq= data[data_size].freq+ data[data[i].next].freq;              data[data_size].flag=1;//those elements adding set flag 1             data[data[i].next].flag=1;             data[data_size+1].flag=0;//this element onbtained on result sent last index.with 0 flag because 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 step4 6>5 (in case added 5+4)              data_size++;              }             if(data[data[i].next].freq==data[data_size].freq && data[data[i].next].flag==0)             {                //some code step3 when element equal                data_size++;              }         }          count--;     } while(count>0) 

there different conditions (last elements= element in right found eg. 3+3=6 in step2) , elements found saller last element 5+4=9 (see step 4)

any idea right in other 2 if conditions? array input must {0,1,4,2,5,3}(i mean data[i].freq) , output array must {0,1,4,2,5,3,1,3,6,9,15} (data[data_size].freq), out sorting , without swapping, using index movement , using arrays. please me in writting 2 if conditions.

finally able make solution of question. used queue that, front , rear variable points starting , end index of data[] array. here equivalent example future user if undergoes same kind of problem(please note here every thing static logic achieve same :

#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;     (i = 0; <= rear; i++) {         flag[i] = 0;     }     int count = rear;     {         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;             (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];             (j = front + 1; j <= rear; j++) {                 if (flag[j] == 0) {                     min2 = data[j];                     location2 = j;                     break;                 }             }             location = front;              (i = start; <= 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;              (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);        (i = 0; < 21; i++) {         printf(" %d  ", data[i]);     }     printf("\n"); } 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -