1. cyclic queue
Sequential storage queue implemented // <the maximum number of data elements stored> #define the MaxSize struct QNode { the ElementType the Data [the MaxSize]; int REAR; int Front; }; typedef struct QNode * Queue; // 1. enqueue void AddQ (PTRQ queue, the ElementType Item) { IF ((PtrQ-> + REAR. 1) ==% PtrQ- the MaxSize> Front) { the printf ( "queue full"); return; } PtrQ-> REAR = (PtrQ-> + REAR . 1) the MaxSize%; PtrQ-> the Data [PtrQ-> REAR] Item =; } // 2. dequeue the ElementType the deleteq (queue PTRQ) { IF (PtrQ-> == Front PtrQ-> REAR) { the printf ( "queue empty "); return REEOR; } the else { PtrQ->front = (PtrQ->front + 1) % MaxSize; return PtrQ->Data[PtrQ->front]; } }
2. Chain queue
// linked storage queue struct the Node { the ElementType the Data; struct the Next the Node *; }; struct QNode { struct the Node * REAR; struct the Node * Front; }; typedef struct QNode * Queue; Queue PTRQ; the ElementType the deleteq (Queue PTRQ) { struct the Node * FrontCell; the ElementType FrontElem; IF (PtrQ-> Front == NULL) { the printf ( "queue empty"); return ERROR; } FrontCell = PtrQ-> Front; IF (PtrQ-> == Front PtrQ- > rear) // only one queue element { PtrQ-> = Front PtrQ-> rEAR = NULL; // delete the queue is empty } the else { PtrQ-> = Front PtrQ-> front-> the Next; } FrontElem = FrontCell->Data; free(FrontCell); // 释放被删除的结点空间 return FrontElem; } void AddQ(Queue PtrQ, ElementType item) { struct Node node; node = (struct Node *)malloc(sizeof(struct Node)); node->Data = item; node->Next = PtrQ->front->Next; PtrQ->front->Next = node; }