1 #include <stdio.h> //队列的链表实现方式 2 #include <stdlib.h> 3 #include <stdbool.h> 4 5 6 //队列的声明 7 struct QueueRecord; 8 typedef struct QueueRecord *QueuePtr; 9 typedef struct QueueNode *Queue; 10 typedef int ElementType; 11 12 bool IsEmpty(Queue Q); 13 Queue CreateQueue(); 14 void DisposeQueue(Queue Q); 15 void Enqueue(ElementType X, Queue Q); 16 ElementType Dequeue(Queue Q); 17 void printQueue(Queue Q); 18 19 struct QueueRecord { 20 ElementType Element; 21 QueuePtr Next; 22 }; 23 24 struct QueueNode { 25 QueuePtr Front; 26 QueuePtr Rear; 27 }; 28 29 30 int main() 31 { 32 int number, flag; 33 ElementType X; 34 Queue Q; 35 36 printf("|**************队列的基本操作**************|\n"); 37 printf("|******************************************|\n"); 38 printf("| 1. 创建队列 |\n"); 39 printf("| 2. 销毁队列 |\n"); 40 printf("| 3. 入队列 |\n"); 41 printf("| 4. 出队列 |\n"); 42 printf("| 5. 输出队列里的元素 |\n"); 43 printf("|******************************************|\n"); 44 45 flag = 1; 46 while (flag) { 47 printf("请选择 : "); 48 scanf("%d", &number); 49 50 switch (number) { 51 case 1 : 52 Q = CreateQueue(); 53 break; 54 55 case 2 : 56 if (Q) { 57 DisposeQueue(Q); 58 } else { 59 printf("队列不存在!!!\n"); 60 exit(-1); 61 } 62 break; 63 64 case 3 : 65 if (Q) { 66 printf("请输入要入队列的元素:"); 67 scanf("%d", &X); 68 Enqueue(X, Q); 69 } else { 70 printf("队列不存在!!!\n"); 71 exit(-1); 72 } 73 break; 74 75 case 4 : 76 if (Q) { 77 Dequeue(Q); 78 } else { 79 printf("队列不存在!!!\n"); 80 exit(-1); 81 } 82 break; 83 84 case 5 : 85 if (Q) { 86 printQueue(Q); 87 } else { 88 printf("队列不存在!!!\n"); 89 exit(-1); 90 } 91 break; 92 93 default : 94 printf("操作异常,请按任意键结束!!!"); 95 flag = 0; 96 } 97 } 98 99 return 0; 100 } 101 102 bool IsEmpty(Queue Q) 103 { 104 return Q->Front == Q->Rear; 105 } 106 107 Queue CreateQueue() 108 { 109 Queue Q; 110 111 if (!(Q = malloc(sizeof(struct QueueNode)))) 112 exit(-1); 113 if (!(Q->Front = Q->Rear = malloc(sizeof(struct QueueRecord)))) 114 exit(-1); 115 Q->Front->Next = Q->Rear->Next = NULL; 116 117 return Q; 118 } 119 120 void Enqueue(ElementType X, Queue Q) 121 { 122 QueuePtr P; 123 124 if (!(P = malloc(sizeof(struct QueueRecord)))) 125 exit(-1); 126 P->Element = X; 127 P-> Next = NULL; 128 Q->Rear->Next = P; 129 Q->Rear = P; 130 } 131 132 ElementType Dequeue(Queue Q) 133 { 134 QueuePtr P; 135 ElementType X; 136 137 if (IsEmpty(Q)) { 138 printf("队列为空!!!\n"); 139 exit(-1); 140 } else { 141 P = Q->Front->Next; 142 X = P->Element; 143 Q->Front->Next = P->Next; 144 if (Q->Rear == P) //队列中只有一个元素 145 Q->Rear = Q->Front; 146 free(P); 147 148 return X; 149 } 150 } 151 152 void DisposeQueue(Queue Q) 153 { 154 QueuePtr P, TmpCell; 155 P = Q->Front->Next; 156 Q->Front = Q->Rear = NULL; 157 while (P) { 158 TmpCell = P->Next; 159 free(P); 160 P = TmpCell; 161 } 162 163 free(Q); 164 } 165 166 void printQueue(Queue Q) 167 { 168 ElementType X; 169 170 while (!IsEmpty(Q)) { 171 X = Dequeue(Q); 172 printf("%d ", X); 173 } 174 printf("\n"); 175 }
1 //队列的数组实现方式 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <stdbool.h> 6 7 #define MinQueueSize (5) //最小队列大小 8 9 //队列的声明 10 struct QueueRecord; 11 typedef struct QueueRecord *Queue; 12 typedef int ElementType; 13 14 bool IsEmpty(Queue Q); 15 bool IsFull(Queue Q); 16 Queue CreateQueue(int MaxElements); 17 void DisposeQueue(Queue Q); 18 void MakeEmpty(Queue Q); 19 void Enqueue(ElementType X, Queue Q); 20 ElementType Dequeue(Queue Q); 21 void printQueue(Queue Q); 22 23 struct QueueRecord { 24 int Capacity; //容量 25 int Front; 26 int Rear; 27 int Size; 28 ElementType *Array; 29 }; 30 31 int main() 32 { 33 int number, flag; 34 ElementType X, MaxQueueSize; 35 Queue Q; 36 37 printf("|**************队列的基本操作**************|\n"); 38 printf("|******************************************|\n"); 39 printf("| 1. 创建队列 |\n"); 40 printf("| 2. 销毁队列 |\n"); 41 printf("| 3. 入队列 |\n"); 42 printf("| 4. 出队列 |\n"); 43 printf("| 5. 输出队列里的元素 |\n"); 44 printf("|******************************************|\n"); 45 46 printf("请输入队列容量大小 :"); 47 scanf("%d", &MaxQueueSize); 48 49 flag = 1; 50 while (flag) { 51 printf("请选择 : "); 52 scanf("%d", &number); 53 54 switch (number) { 55 case 1 : 56 Q = CreateQueue(MaxQueueSize); 57 break; 58 59 case 2 : 60 if (Q) { 61 DisposeQueue(Q); 62 } else { 63 printf("队列不存在!!!\n"); 64 exit(-1); 65 } 66 break; 67 68 case 3 : 69 if (Q) { 70 printf("请输入要入队列的元素:"); 71 scanf("%d", &X); 72 Enqueue(X, Q); 73 } else { 74 printf("队列不存在!!!\n"); 75 exit(-1); 76 } 77 break; 78 79 case 4 : 80 if (Q) { 81 Dequeue(Q); 82 } else { 83 printf("队列不存在!!!\n"); 84 exit(-1); 85 } 86 break; 87 88 case 5 : 89 if (Q) { 90 printQueue(Q); 91 } else { 92 printf("队列不存在!!!\n"); 93 exit(-1); 94 } 95 break; 96 97 default : 98 printf("操作异常,请按任意键结束!!!"); 99 flag = 0; 100 } 101 } 102 103 return 0; 104 } 105 106 bool IsFull(Queue Q) 107 { 108 return Q->Size == Q->Capacity; 109 } 110 111 bool IsEmpty(Queue Q) 112 { 113 return Q->Size == 0; 114 } 115 116 Queue CreateQueue(int MaxElements) 117 { 118 Queue Q; 119 120 if (MaxElements < MinQueueSize) { 121 printf("队列容量太小!!!"); 122 exit(-1); 123 } 124 if (!(Q = malloc(sizeof(struct QueueRecord)))) 125 exit(-1); 126 if (!(Q->Array = malloc(sizeof(ElementType) * MaxElements))) 127 exit(-1); 128 Q->Capacity = MaxElements; 129 MakeEmpty(Q); 130 131 return Q; 132 } 133 134 void Enqueue(ElementType X, Queue Q) 135 { 136 if (!IsFull(Q)) { 137 Q->Size++; 138 Q->Array[Q->Rear] = X; 139 Q->Rear = ((Q->Rear) + 1) % Q->Capacity; 140 } 141 } 142 143 void MakeEmpty(Queue Q) 144 { 145 Q->Size = 0; 146 Q->Front = 0; 147 Q->Rear = 0; 148 } 149 ElementType Dequeue(Queue Q) 150 { 151 if (!IsEmpty(Q)) { 152 ElementType X; 153 Q->Size--; 154 X = Q->Array[Q->Front]; 155 Q->Front = ((Q->Front) + 1) % Q->Capacity; 156 157 return X; 158 } 159 } 160 161 void DisposeQueue(Queue Q) 162 { 163 MakeEmpty(Q); 164 free(Q->Array); 165 free(Q); 166 } 167 168 void printQueue(Queue Q) 169 { 170 ElementType X; 171 printf("(打印原理:出队列输出。即打印完成后,队列里再无元素!)\n"); 172 while (!IsEmpty(Q)) { 173 X = Dequeue(Q); 174 printf("%d ", X); 175 } 176 printf("\n"); 177 }
时间: 2024-10-09 04:37:27