Queue: linear form having a certain operational constraints, only one end of the insertion, deletion at the other end.
Features: First come, first serve, first-out table
Head front, rear tail
Sequential storage
. 1 #define the MaxSize <the maximum number of data elements stored> 2 . 3 struct QNode { . 4 . 5 the ElementType the Data [the MaxSize]; . 6 . 7 int REAR; // End . 8 . 9 int Front; // head 10 . 11 }; 12 is 13 is typedef struct * Queue QNode;
14 // Front REAR = = -1;
Circular queue storage approach:
(1) use of an additional tag: size or Tag (insert 1, delete 0) domain
(2) using only spatial array of n-1
(1) into the queue (circular queue)
1 void AddQ (Queue PtrQ, ElementType item){ 2 3 if ( (PtrQ->rear+1) % MaxSize == PtrQ->front ){ 4 5 printf("队列满"); 6 7 return; 8 9 } 10 11 PtrQ->reat = (PtrQ->rear+1) % MaxSize; 12 13 PtrQ->Data[PtrQ->rear] = item; 14 15 }
(2) the queue
ElementType DeleteQ ( Queue PtrQ ){ if (PtrQ->front == PtrQ->reat) { printf("队列空"); return ERROR; } else { PtrQ->front = (PtrQ->front+1) % MaxSize; return PtrQ->Data[PtrQ->front]; } }
Chain store
struct the Node {// linked list node structure of
the ElementType the Data;
struct the Node * the Next;
};
struct {// chain QNode queue structure
struct Node * rear; // points to the tail node
struct Node * front; // head junction point points };
typedef struct QNode * Queue; Queue PTRQ;
Insert and delete operations are carried out at both ends of the list
Without head node chain operations team squad list
The deleteq the ElementType (Queue PTRQ) {
struct the Node * FrontCell;
the ElementType FrotElem;
IF (PtrQ-> Front == NULL) {
the printf ( "queue empty" );
return ERROR;
}
FrontCell = PtrQ-> Front; IF (PtrQ-> front == PtrQ-> rear) // If only one queue element PtrQ-> front = PtrQ-> rear = NULL; // delete the post queue is empty the else {PtrQ-> = Front PtrQ-> front-> the Next; FrontElem = FrontCell-}> the Data; Free (FrontCell); return FrontElem;}