Linux data structure - queue (sequential table implementation)

First of all, we need to understand that the rule followed by the queue is "first in, first out". We can see examples of queuing in life (such as: queuing for tickets, queuing for meals) is actually a queue.

Like the stack, in order to ensure the security of the queue, we have only three operations on the queue: entering the queue, exiting the queue, and taking the first element of the queue.

This article mainly uses an array to implement a queue. In order to make full use of the space of the queue, a linear queue is used to simulate a circular queue.

#define SeqQueueMaxSize 1000
typedef char SeqQueueType;

typedef struct SeqQueue
{
    SeqQueueType data[SeqQueueMaxSize];
    size_t head;
    size_t tail;
    size_t size;
} seqQueue;

The macro SeqQueueMaxSize sets the number of items that can be stored in the entire array, head is the table below where the first element of the queue is in the array, tail is the table below where the element at the end of the queue is in the array, and size is the number of elements we store in the real queue The total number of.

1. Initialization of the queue

void  SeqQueueInit(SeqQueue* q)
{
    if (q == NULL)
        return;
    q->size = 0;
    q->head = 0;
    q->tail = 0;
    return;
}

First of all, we must judge the illegality of the pointer.

Then set the number inside to 0. I start with 0 for head and tail.

2. Destruction of the queue

void  SeqQueueDestory(SeqQueue* q)
{
    if (q == NULL)
        return;
    q->size = 0;
    q->head = 0;
    q->tail = 0;
    return;
}

Destruction is the same as initialization. Because we don't use malloc to open up space, we don't need free to release it. Set the number of elements stored in the queue to 0, and set the head and tail to 0, starting from 0.

3. Enter the queue

void SeqQueuePush(SeqQueue* q, SeqQueueType value)
{
    if (q == NULL)
        return;
    if (q->size >= SeqQueueMaxSize)
        return;
       //q->data[q->size++] = value;//wrong
    q->data[q->tail++] = value;//xian yong hou jia
    ++q->size;
    if (q->tail >= SeqQueueMaxSize)
    {
        q->tail = 0;
    }
    return;
}

Ideas:

1. Passing parameters: which queue do you want to enter, and what element do you want to enter;

2. The first thing is to judge the legitimacy of the pointer;

3. Judging whether it is full, can you still enter?

4. Insert a new element at the tail; if the newly inserted element has been queued to the last position of the queue, you need to set the tail to 0 and start from the beginning.

5. Then size++ the elements in the entire queue.

4. Dequeue

void SeqQueuePop(SeqQueue* q)
{
    if (q == NULL)
        return;
    if (q->size == 0)
        return;
    ++q->head;
    if (q->head >= SeqQueueMaxSize)
        q->head = 0;
    --q->size;
    return;
}

Ideas:

1. Passing parameters: which queue element do you want to output

2. Judgment of the legitimacy of the pointer

3. Determine whether the queue is an empty queue

4. Out of the queue, throw away the first element of the queue. If the head at this time has reached the last position of the entire queue, set the head to 0 and continue from the beginning.

5. The number of elements in the entire queue size--.

5. Take the first element of the team

int SeqQueueTop(SeqQueue* q, SeqQueueType* value)
{
    if (q == NULL || value == NULL)
        return 1;
    if (q->size == 0)
        return 1;
    *value = q->data[q->head];
    return 0;
}

Ideas:

1. Passing parameters: which queue to take from, and the element taken out is value. Here value is an output parameter.

2. Judgment of the legitimacy of the pointer.

3. Empty queue judgment. If it is an empty queue, the first element of the queue cannot be taken.

4. Assign the element of the team leader to value.

Returns 0 if successful, 1 if failed.

Test function:

void Test()
{
    SeqQueue queue;
    SeqQueueInit(&queue);
    SeqQueuePush(&queue,'a');
    SeqQueuePush(&queue,'b');
    SeqQueuePush(&queue,'c');
    SeqQueuePop(&queue);
    SeqQueueType value;
    int temp = SeqQueueTop(&queue, &value);
    if (temp == 0)
        printf("%c\n", value);
    return;
}
intmain()
{
    Test();
    return 0;
}

Did a simple test without taking all special cases into account.

Readers can also add test statements as needed.

Test Results:


First initialize a queue, then enter the queue with three elements 'a', 'b', 'c', and then output an element. Then there are two elements 'b' and 'c' left in the queue, and the first element of the queue is taken, and the output result is 'b'.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325171859&siteId=291194637