Queue efficiency, which is the best implementation - array or linked list

Which way gives faster enqueueing and dequeuing when I need to insert very few elements, Is an array better than a linked list?

I need to insert multiple items, and I need to remove and read the deleted item from the queue. If it is an array, I may need to change the indexes every time I delete an item. Insertion and deletion can occur simultaneously.

Which one is better from the following case?

typedef struct{
    mylist list;
    struct mylistQ *next;
}mylistQ;

Array Code

 static mylist myListQ[QUEUESIZE+1];
int qLast = 0;

void enqueue_element(mylist qItem)
{
        myListQ[qLast] = qItem;
    qLast++;
}

mylist dequeue_element()
{
 retryq:
   if(qLast >0) {
    mylist qReturn = myListQ[0];  
    int i;
    for (i = 0; i < qLast - 1; i++){
        myListQ[i] = myListQ[i + 1];
    }
    qLast--; 
    return qReturn;
     }
   else {
    goto retryq;
    }
}

Linked list

 int qLast = 0;

mylistQ *headElement = NULL;   
mylistQ *tailElement = NULL;     

void enqueue_element(mylist *List)
{
    mylistQ *newnode;      
    newnode=(mylistQ*)av_malloc(sizeof(mylistQ));
    newnode->next=NULL;
    newnode->list=*List;
    qLast++;
    if(headElement==NULL && tailElement==NULL)
    {
        headElement=newnode;
        tailElement=newnode;
    }
    else
    {
        tailElement->next=newnode;
        tailElement=newnode;
    }
 }

mylist dequeue_element()
{
    mylistQ *delnode;      /* Node to be deleted */
    mylist Dellist;
    if(headElement==NULL && tailElement==NULL){
        LOg( "Queue is empty to delete any element");
        }
    else
    {
       Log("In dequeue_picture queue is not empty");
        delnode=headElement;
        headElement=headElement->next;
    if (!headElement){
        tailElement=NULL;
    }
        Dellist = delnode->list;
        av_free(delnode);
    qLast--;
    }
        return Dellist;
}
+3
source share
3 answers

It depends on how many operations you will perform and how exactly the version of the array is implemented.

, 1000 /, , . , .

, 30 , , , .

, , .

EDIT: @Hans Passant, , . , , , , L2. , , . . , , . , , , , , .

, , , , , , , ? , , - 800 . ? , , . , . , , , , . , , ( gcc) NDEBUG -O3.

: , , , . , , , , , int "" .

:

int ciruclarArray[SIZE];
int front = 0;
int back = 0;

void enqueue(int elem)
{
    circularArray[back] = elem;
    if(back < (circularArray.length - 1))
        back++;
    else
        back = 0;
    return elem;
}

int dequeue()
{
    int toReturn = circularArray[front];
    //do same check for incrementing as enqueue
    return toReturn;
}

.

+4

, , . .

+3

Even if you have many elements, the implementation of the array is probably the fastest. For inspiration, I looked at the GCC C ++ dequeue. It stores the queue as an array of arrays. I'm not sure if iterators are wrapped like in a circular buffer. The array implementation also has quick random access if you need it later.

+1
source

Source: https://habr.com/ru/post/1794574/


All Articles