简体   繁体   中英

queue type data structure implementation using arrays

I have undergone a situation where i have to implement a queue type data structure.It is like this:

(1) Suppose there is an array data[50]= {1,2,3,4,5,6};

(2) int Front must point to first index and int rear must point must point to last index.

(3)Now i have to add first and second element of this array. Suppose i do that (i+2=3) and this 3 will be set at last by doing rear+1 (data[rear+1]), Now when the second execution occurs, We don't have to take into account the first two element (they already added), So in this case we can do data[front+2]. But pay attention here please, because this +2 is going to be done first time only , after this it will be just +1 (because we added only one element, and first time we add 2 element).And the element obtained on addition must go to last of the index like "3" obtained will go at last like this {1,2,3,4,5,6,3}.

(4)So we have to take into account

(4.a) Increment of Rear by one on each addition.

(4.b) Increment of Front by one (accept the first addition where we add two elements).

(5) My idea to do this is as follows:

    #include <stdio.h>
    #define MAX 50
    int data[MAX];
    int rear = - 1;
    int front = - 1;

    void main()
    {
     int rear=6, front=0;
     data[size]={1,2,3,4,5,6};
     int count=size;
     //First do addition of the first two elements
     data[rear]= data[0]+data[1];
     for(i=front; i<size*2-1 ; i++) //we are doing data*2-1 because we know the final obtained on doing all the addition until there is 1 element will have the size (size*2-1).
      {
       do
        { 
         //here we do addition data[rear+1]=data[front]+data[rear];
         // rear++;     
         count--;
        }while (count>1);
       }
      for(i=0; i<size*2-1 ; i++)
      {
       printf("%d ", data[i]); //Now this must print "1,2,3,4,5,6,3,6,9,12,21" (addition of element at front and rear)
      }         

    }

**My doubt is how to increase the Front by two first time addition and by increasing by one after first addition so that first will always point to the element to be added(increasing is not difficult , i have done that). Please help me for Front increment, Algorithm and code will be very grateful.

int max = 50;
int index = 0;
int endIndex = 5; //You can compute the length. I will just hard code
int list[max] = {1,2,3,4,5,6};
int front = list[index];
int rear = list[endIndex];

while (index!= endIndex) 
{
if (index==0)
{
        index++;
        list[endIndex+index] = front+list[index];
        rear = list[endIndex+index];
        index++;
        front = list[index];
}
else 
{
        list[rear+1] = front + rear;
        index++;
        front = list[index];
        rear = list[endIndex+index];
}
}

This should do what are you are looking for. I didn't test it but I believe it is what you described.

Finally i managed to solve the question, I am leaving here my code for others (if they use it in future):

#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};
 Rear=3;
//Rear=size-1
for(i=0; i<=Rear;i++)
{
flag[i]=0;
 //printf("");
 }
int count=Rear;
do
{
//printf("check5 \n ");
if(Front==0)
 {
printf("check1 \n ");

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

   if(data[Front]==data[Rear] && flag[Rear] ==0 && flag[Front]==0 ) 
 { 
    printf("check3 \n ");
     printf("Front : %d\n ", Front);
    // Rear++;
printf("Rear : %d \n", Rear);
printf("dataFront1: %d\n ", data[Front]);
printf("dataRear1: %d\n ", data[Rear]);
    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;
    printf("check Front front : %d\n", Front);
                for(j=Front+1;j<=Rear;j++)
                {
                if(flag[j]==0)
                {
                Front=j;    
                 break;             
                }
                }           
//  Front++; 
    Rear++;
}

  if(data[Front]<data[Rear] && flag[Rear] ==0 && flag[Front]==0) 
   {         
         printf("check4 \n ");   
                 printf("Front : %d\n ", Front);
printf("Rear : %d \n", Rear);
printf("dataFront1: %d\n ", data[Front]);
printf("dataRear1: %d\n ", data[Rear]);
 printf("Flagcheck data[6].flag : %d \n",flag[6]);
                int start= Front+2;
                min= data[Front];
                for(j=Front+1;j<=Rear;j++)
                {
                if(flag[j]==0)
                {
                min2=data[j];
                location2=j;
                printf("j = %d \n",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; 
                printf("min= %d\n", min);
                printf("min2= %d\n", min2);
                printf("location= %d\n", location);
                printf("location2= %d\n", location2);
                printf(" ***********data[Rear+1] = %d\n", data[Rear+1]);
                flag[location2]=1;
                flag[location]=1;
                flag[Rear+1]=0;

                for(j=location+1;j<=Rear;j++)
                {
                if(flag[j]==0)
                {
                Front=j;    
                 break;             
                }
                }           
                Rear=Rear+1;                
 printf("Front : %d\n ", Front);
printf("Rear : %d \n", Rear);
 }  
//printf("\n");
//printf("Front : %d\n ", data[Front]);
//printf("Rear : %d \n", data[Rear]);
count--;
} while(Front!=Rear && count>0);
for (i=0;i<15;i++)
{
 printf(" %d  ", data[i]);
}
//char path[100]={'\0'};
//traverse_tree(data, 10,path);
printf("\n");
}
////

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM