简体   繁体   English

使用数组的队列类型数据结构实现

[英]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}; (1)假设有一个数组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. (2)int前面必须指向第一个索引,而int后面必须指向必须指向最后一个索引。

(3)Now i have to add first and second element of this array. (3)现在我必须添加此数组的第一个和第二个元素。 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]. 假设我这样做了(i + 2 = 3),最后通过执行Rear + 1(data [rear + 1])来设置这3,现在当第二次执行发生时,我们不必考虑前两个元素(它们已经添加),因此在这种情况下,我们可以执行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}. 但是请注意这里,因为这个+2只会第一次执行 ,在此之后它只会是+1(因为我们只添加了一个元素,并且第一次我们添加了2个元素)。必须像所获得的“ 3”那样去索引的最后,像这样的{1,2,3,4,5,6,3}会去最后。

(4)So we have to take into account (4)因此我们必须考虑到

(4.a) Increment of Rear by one on each addition. (4.a)每次增加后方增加一。

(4.b) Increment of Front by one (accept the first addition where we add two elements). (4.b)Front增加一个(接受第一个加法,即我们添加两个元素)。

(5) My idea to do this is as follows: (5)我的想法如下:

    #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). **我的疑问是如何将Front增加两个第一次相加以及如何在第一次添加后相加一个,以便first总是指向要添加的元素(增加并不困难,我已经做到了)。 Please help me for Front increment, Algorithm and code will be very grateful. 请帮助我增加Front,算法和代码将不胜感激。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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