Data entry structure - queue

A way to achieve " FIFO " storage structure

classification:

  1. Chain queue: linked list implemented
  2. Static queue: use the array to achieve, must be static queue is typically circular queue

Circular queue explain:

  1. Why is static circular queue queue

    Reduce waste of memory

  2. Circular queue requires several parameters to determine

    Two parameters, Frant, REAR but two parameters have different meanings on different occasions, recommended for beginners to remember

  3. The meaning of the various parameters cyclic queue

    Queue initialization: front and rear values ​​are zero

    The next front element represents the first element of the queue, rear agent queue of the last valid element: a non-empty queue

    Queue is empty: front and rear of equal value, but not necessarily zero

  4. Cycling team included Team pseudo algorithm

    The value stored in the rear position represented

    Error wording: r = r + 1

    Right should be r = (r + 1)% array length

  5. Team cycling team lists pseudo algorithm

    front = (front + 1)% array length

  6. How to determine whether the circular queue is empty

    If the values ​​are equal the front and rear, the queue is empty

  7. How to determine the circular queue is full

    A multi-parameter table identifies the increase

    With at least one element , such are usually: (+ REAR. 1) length of the array == front%

Specific implementation

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


typedef struct Queue
{
    int * pBase;
    int front;
    int rear;
}QUEUE;

void init(QUEUE *);
bool full_queue(QUEUE *);
bool empty(QUEUE *);
bool en_queue(QUEUE * , int val); // 入队
void traverse(QUEUE *);
bool out(QUEUE *, int * pVal);


int main(void)
{
    QUEUE Q;
    int val;

    init(&Q);
    en_queue(&Q , 1);
    en_queue(&Q , 2);
    en_queue(&Q , 3);
    en_queue(&Q , 4);
    en_queue(&Q , 5);
    en_queue(&Q , 6);
    en_queue(&Q , 7);

    traverse(&Q);
    if (out(&Q , &val))
    {
        printf("出队成功,出队的元素:%d\n", val);
        en_queue(&Q , 9);
        traverse(&Q);
    }
    else
    {
        printf("出队失败\n");
    }


    return 0;
}


void init(QUEUE * pQ)
{
    pQ->pBase = (int *)malloc(sizeof(int) * 6); // 初始化默认是长度是6
    if (NULL == pQ->pBase)
    {
        printf("初始化失败\n");
        exit(-1);
    }

    pQ->front = 0;
    pQ->rear = 0;
}

bool full_queue(QUEUE *pQ)
{
    if ((pQ->rear+1)%6 == pQ->front)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool en_queue(QUEUE *pQ , int val)
{
    if (full_queue(pQ))
    {
        return false;
    }
    else
    {
        pQ->pBase[pQ->rear] = val;
        pQ->rear = (pQ->rear+1)%6;
        return true;
    }
}

void traverse(QUEUE * pQ)
{
    int i = pQ->front;

    while(i != pQ->rear)
    {
        printf("%d\n",pQ->pBase[i] );
        i = (i+1) % 6;
    }

    return;
}


bool empty(QUEUE * pQ)
{
    if (pQ->front == pQ->rear)
    {
        return true;
    }
    else
    {
        return false;
    }
}


bool out(QUEUE * pQ, int * pVal)
{

    if (empty(pQ))
    {
        return false;
    }
    else
    {
        *pVal = pQ->pBase[pQ->front];
        pQ->front = (pQ->front+1) % 6;
        return true;
    }

}

Queue application

  • And all time-related operations have shadow queue

Guess you like

Origin www.cnblogs.com/mengd/p/12045809.html