队列(queue),是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。是一种先进先出(FIFO)的数据结构。
一、队列的顺序存储->循环队列
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define OK 1 5 #define ERROR 0 6 7 #define MaxSize 20 /* 存储空间初始分配量 */ 8 9 typedef int Status; 10 typedef int QElemType; /* QElemType类型根据实际情况而定,这里假设为int */ 11 12 /* 循环队列的顺序存储结构 */ 13 typedef struct 14 { 15 QElemType data[MaxSize]; 16 int front; /* 头指针 */ 17 int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */ 18 }SqQueue; 19 20 Status InitQueue(SqQueue &Q); 21 Status ClearQueue(SqQueue &Q); 22 bool QueueEmpty(SqQueue Q); 23 bool QueueFull(SqQueue Q); 24 int QueueLength(SqQueue Q); 25 Status GetHead(SqQueue Q,QElemType &e); 26 Status EnQueue(SqQueue &Q,QElemType e); 27 Status DeQueue(SqQueue &Q,QElemType &e); 28 Status QueueTraverse(SqQueue Q); 29 30 Status visit(QElemType c) 31 { 32 printf("%d ",c); 33 return OK; 34 } 35 36 /* 初始化一个空队列Q */ 37 Status InitQueue(SqQueue &Q) 38 { 39 Q.front = 0; 40 Q.rear = 0; 41 return OK; 42 } 43 44 /* 将Q清为空队列 */ 45 Status ClearQueue(SqQueue &Q) 46 { 47 Q.front = Q.rear = 0; 48 return OK; 49 } 50 51 /* 若队列Q为空队列,则返回TRUE,否则返回FALSE */ 52 bool QueueEmpty(SqQueue Q) 53 { 54 if(Q.front == Q.rear) /* 队列空的标志 */ 55 return true; 56 else 57 return false; 58 } 59 /* 若队列Q满,则返回TRUE,否则返回FALSE */ 60 bool QueueFull(SqQueue Q) 61 { 62 if( (Q.rear + 1) % MaxSize == Q.front) 63 return true; 64 else 65 return false; 66 } 67 /* 返回Q的元素个数,也就是队列的当前长度 */ 68 int QueueLength(SqQueue Q) 69 { 70 return (Q.rear - Q.front + MaxSize) % MaxSize; 71 } 72 73 /* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */ 74 Status GetHead(SqQueue Q,QElemType &e) 75 { 76 if(Q.front == Q.rear) /* 队列空 */ 77 return ERROR; 78 e = Q.data[Q.front]; 79 return OK; 80 } 81 82 /* 若队列未满,则插入元素e为Q新的队尾元素 */ 83 Status EnQueue(SqQueue &Q,QElemType e) 84 { 85 if ((Q.rear+1) % MaxSize == Q.front) /* 队列满的判断 */ 86 return ERROR; 87 Q.data[Q.rear] = e; /* 将元素e赋值给队尾 */ 88 Q.rear = (Q.rear+1) % MaxSize;/* rear指针向后移一位置, */ 89 /* 若到最后则转到数组头部 */ 90 return OK; 91 } 92 93 /* 若队列不空,则删除Q中队头元素,用e返回其值 */ 94 Status DeQueue(SqQueue &Q,QElemType &e) 95 { 96 if (Q.front == Q.rear) /* 队列空的判断 */ 97 return ERROR; 98 e = Q.data[Q.front]; /* 将队头元素赋值给e */ 99 Q.front = (Q.front+1) % MaxSize; /* front指针向后移一位置*/ 100 /* 若到最后则转到数组头部 */ 101 return OK; 102 } 103 104 /* 从队头到队尾依次对队列Q中每个元素输出 */ 105 Status QueueTraverse(SqQueue Q) 106 { 107 int i = Q.front; 108 while( i != Q.rear) 109 { 110 visit(Q.data[i]); 111 i = (i + 1) % MaxSize; 112 } 113 printf("\n"); 114 return OK; 115 } 116 117 int main() 118 { 119 QElemType d; 120 SqQueue Q; 121 int *e; 122 InitQueue(Q); 123 if( QueueEmpty(Q) ) 124 printf("空\n"); 125 else 126 printf("不空\n"); 127 for(int i = 0; i < 5; i++) { 128 EnQueue(Q,i); 129 } 130 QueueTraverse(Q); 131 printf("QueueLength(Q) = %d\n",QueueLength(Q)); 132 if( QueueEmpty(Q) ) 133 printf("空\n"); 134 else 135 printf("不空\n"); 136 137 DeQueue(Q,*e); 138 printf("出队%d\n",*e); 139 QueueTraverse(Q); 140 printf("QueueLength(Q) = %d\n",QueueLength(Q)); 141 142 GetHead(Q,*e); 143 printf("头%d\n",*e); 144 ClearQueue(Q); 145 if( QueueEmpty(Q) ) 146 printf("空\n"); 147 else 148 printf("不空\n"); 149 150 if(QueueFull(Q)) 151 printf("队列满\n"); 152 153 return 0; 154 }