队列数组实现的简单操作:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
/***********************************************************/
// 程序名称:StackOfLink.cpp // 程序目的:设计一个数组实现的队列的程序 // 程序来源:数据结构与算法分析(C语言描述) P-60 // 日期:2013-8-23 10:32:58 /***********************************************************/ #include <stdio.h> #include <stdlib.h> #define Error( str ) FatalError( str ) #define FatalError( str ) fprintf( stderr, "%s\n", str ), exit( 1 ) typedef int ElementType; #define MINQUEUESIZE 5 struct QueueRecord { int capacity; // 容量 int front; // 对头 int rear; // 队尾 int size; ElementType* arrayQueue; }; typedef struct QueueRecord* Queue; int IsEmpty(Queue q); int IsFull(Queue q); Queue CreateQueue( int maxElements); void DisposeQueue(Queue q); void MakeEmpty(Queue q); void Enqueue(ElementType x, Queue q); ElementType Front(Queue q); void Dequeue(Queue q); ElementType FrontAndDequeue(Queue q); /************************************************************************/ // 主程序 /************************************************************************/ int main( void) { Queue sqQueue; int maxElements = 10; sqQueue = CreateQueue(maxElements); if (IsEmpty(sqQueue)) printf( "创建了空队列!"); int value = 0; printf( "队列中的数据为(front->rear):\n"); while (!IsFull(sqQueue)) { Enqueue(value*value, sqQueue); // 入队 printf( "%d ", value*value); value++; } printf( "队列已满\n"); ElementType frontQueue; frontQueue = Front(sqQueue); printf( "对头元素为:%d\n", frontQueue); Dequeue(sqQueue); frontQueue = Front(sqQueue); printf( "执行出队操作Dequeue之后,对头元素是:%d\n", frontQueue); DisposeQueue(sqQueue); return 0; } /************************************************************************/ // 队列是否为空 /************************************************************************/ int IsEmpty(Queue q) { return q->size == 0; } /************************************************************************/ // 队列是否已满 /************************************************************************/ int IsFull(Queue q) { return q->size == q->capacity; } /************************************************************************/ // 创建队列 /************************************************************************/ Queue CreateQueue( int maxElements) { Queue q; if (maxElements < MINQUEUESIZE) Error( "要创建的队列太小!"); q = (Queue)malloc( sizeof( struct QueueRecord)); if ( NULL == q) FatalError( "空间不足,队列内存分配失败!"); q->arrayQueue = (ElementType*)malloc( sizeof(ElementType) * maxElements); if ( NULL == q->arrayQueue) FatalError( "空间不足,队列内数组创建失败!"); q->capacity = maxElements; MakeEmpty(q); return q; } /************************************************************************/ // 释放队列 /************************************************************************/ void DisposeQueue(Queue q) { if ( NULL != q) { free(q->arrayQueue); free(q); } return; } /************************************************************************/ // 使队列为空 /************************************************************************/ void MakeEmpty(Queue q) { q->size = 0; q->front = 1; q->rear = 0; } /************************************************************************/ // 检测对头/队尾位置是否已到尾端 /************************************************************************/ static int Succ( int value, Queue q ) { if( ++value == q->capacity ) value = 0; return value; } /************************************************************************/ // 入队列 /************************************************************************/ void Enqueue(ElementType x, Queue q) { if (IsFull(q)) Error( "队列已满"); else { q->size++; q->rear = Succ(q->rear, q); q->arrayQueue[q->rear] = x; } return; } /************************************************************************/ // 出队列 /************************************************************************/ void Dequeue(Queue q) { if (IsEmpty(q)) Error( "队列为空!"); else { q->size--; q->front = Succ(q->front, q); } return; } /************************************************************************/ // 返回对列头元素 /************************************************************************/ ElementType Front(Queue q) { if (!IsEmpty(q)) return q->arrayQueue[q->front]; Error( "队列为空\n"); return 0; } /************************************************************************/ // 返回对列头元素并使对头元素出对 /************************************************************************/ ElementType FrontAndDequeue(Queue q) { ElementType x = 0; if( IsEmpty(q) ) Error( "队列为空!" ); else { q->size--; x = q->arrayQueue[q->front]; q->front = Succ( q->front, q ); } return x; } |