Cyclic queue data structure []

Queues are FIFO (first in first out) linear form, referred to as FIFO.

 

Suppose circular queues:

Team head: front

The tail: rear

Maximum size: queue size

Queue full: (rear + 1)% queue size == front

Common queue length is calculated: (rear - front + queue size)% queue size

Sample code storage structure

typedef int QElemType; / * type of data within the queue * /

/ * Sequential storage structure circular queue * /

typedef struct

{

  QElemType date[MAXSIZE];

  int front; / * head pointer * /

  int rear; / * tail pointer, if the queue is not empty, a point to enqueue position * /

} SqQueue;

 

/ * Initialize an empty queue * /

int InitQueue(SqQueue *Q)

{

  Q->front = 0;

  Q->rear = 0;

  return OK;

}

 

/ * Find a circular queue length * /

int QueueLenght(SqQueue *Q)

{

  return (Q.rear - Q.front + MAXSIZE)% MAXSIZE;

}

/ * Enqueue operation circular queue * /

int EnQueue(SqQueue *Q, QElemType e)

{

  if ((Q-> rear + 1)% MAXSIZE == Q-> front) / * * determined queue full /

    return ERROR;

  Q-> data [Q-> rear] = e; / * * a new element to the tail /

  Q-> rear = (Q-> rear + 1)% MAXSIZE; / * pointer is moved one after the rear * /

                      / * If you go to the last head array * /

  return OK;

}

/ * Dequeue circular queue * /

int EnQueue(SqQueue *Q, QElemType *e)

{

  if (Q-> rear == Q-> front) / * Analyzing queue is empty * /

    return ERROR;

  * E = Q-> data [Q-> front]; / * the head elements to e * /

  Q-> front = (Q-> front + 1)% MAXSIZE; / * pointer moves back front a * /

                      / * If you go to the last head array * /

  return OK;

}

Guess you like

Origin www.cnblogs.com/guoqingpeng/p/12554254.html