队列操作的链表实现:
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 209 |
/***********************************************************/
// 程序名称:StackOfLink.cpp // 程序目的:设计一个链表实现的队列的程序 // 程序来源:数据结构与算法分析(C语言描述) P-60 // 日期:2013-8-24 17:20:20 /***********************************************************/ #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 MAXQUEUE 10 typedef struct node { ElementType data; struct node* nextNode; } Node; typedef struct queue { Node* front; // 对首指针 Node* rear; // 队尾指针 int items; // 队列中项目个数 } *ptrQueue; typedef ptrQueue Queue; int IsEmpty(Queue q); int IsFull(Queue q); Queue CreateQueue( void); 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(); 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->items == 0; } /************************************************************************/ // 是否已满 /************************************************************************/ int IsFull(Queue q) { return q->items == MAXQUEUE; } /************************************************************************/ // 创建队列 /************************************************************************/ Queue CreateQueue( void) { Queue q; q = (Queue)malloc( sizeof( struct queue)); if ( NULL == q) Error( "空间不足,队列内存分配失败!"); q->front = (Node*)malloc( sizeof(Node)); if ( NULL == q->front) Error( "空间不足,队列首节点内存分配失败!"); q->rear = (Node*)malloc( sizeof(Node)); if ( NULL == q->rear) Error( "空间不足,队列尾节点内存分配失败!"); q->items = 0; return q; } /************************************************************************/ // 释放队列 /************************************************************************/ void DisposeQueue(Queue q) { MakeEmpty(q); free(q); } /************************************************************************/ // 使队列为空 /************************************************************************/ void MakeEmpty(Queue q) { if (q == NULL) Error( "必须先使用CreateQueue创建队列!"); while (IsEmpty(q)) Dequeue(q); } /************************************************************************/ // 入对 /************************************************************************/ void Enqueue(ElementType x, Queue q) { if (IsFull(q)) Error( "队列已满!"); Node* pnode; pnode = (Node*)malloc( sizeof(Node)); if ( NULL == pnode) Error( "新节点分配内存失败!"); pnode->data= x; pnode->nextNode = NULL; if (IsEmpty(q)) q->front = pnode; // 项目位于首端 else q->rear->nextNode = pnode; // 链接到队列尾端 q->rear = pnode; // 记录队列尾端的位置 q->items++; // 项目数加1 return; } /************************************************************************/ // 出对 /************************************************************************/ void Dequeue(Queue q) { if (IsEmpty(q)) Error( "队列本身为空!"); Node* pnode; pnode = q->front; q->front = q->front->nextNode; free(pnode); q->items--; if (q->items == 0) q->rear = NULL; return; } /************************************************************************/ // 返回对列头元素 /************************************************************************/ ElementType Front(Queue q) { if (!IsEmpty(q)) return q->front->data; Error( "队列为空\n"); return 0; } /************************************************************************/ // 返回对列头元素并使对头元素出对 /************************************************************************/ ElementType FrontAndDequeue(Queue q) { ElementType x = 0; if( IsEmpty(q) ) Error( "队列为空!" ); else { q->items--; x = q->front->data; q->front = q->front->nextNode; } return x; } |