顺序存储队列头文件:
1 #pragma once 2 #include<stdlib.h> 3 #include<stdio.h> 4 #define MAX_SIZE 1024 5 6 //顺序队列结构体 7 typedef struct SEQQUEUE 8 { 9 void *data[MAX_SIZE]; 10 int size; 11 }SeqQueue; 12 13 //初始化队列 14 SeqQueue* Init_SeqQueue(); 15 //入队 16 void Push_SeqQueue(SeqQueue* queue, void* value); 17 //出队 18 void Pop_SeqQueue(SeqQueue* queue); 19 //返回队首元素 20 void* Front_SeqQueue(SeqQueue* queue); 21 //返回队尾元素 22 void* Back_SeqQueue(SeqQueue* queue); 23 //返回队列长度 24 int Size_SeqQueue(SeqQueue* queue); 25 //清空队列 26 void Clear_SeqQueue(SeqQueue* queue); 27 //销毁队列 28 void Free_SeqQueue(SeqQueue* queue);
顺序存储队列实现文件:
1 #include "队列顺序存储.h" 2 //初始化队列 3 SeqQueue* Init_SeqQueue() 4 { 5 SeqQueue* queue = (SeqQueue*)malloc(sizeof(SeqQueue)); 6 if (queue == NULL) 7 { 8 exit(1); 9 } 10 11 queue->size = 0; 12 for (int i = 0; i < MAX_SIZE; ++i) 13 { 14 queue->data[i] = NULL; 15 } 16 17 return queue; 18 } 19 //入队数组左边做队头 20 void Push_SeqQueue(SeqQueue* queue, void* value) 21 { 22 if (queue == NULL) 23 { 24 exit(1); 25 } 26 27 if (value == NULL) 28 { 29 exit(1); 30 } 31 32 if (queue->size == MAX_SIZE) 33 { 34 exit(1); 35 } 36 37 queue->data[queue->size] = value; 38 queue->size++; 39 } 40 //出队 41 void Pop_SeqQueue(SeqQueue* queue) 42 { 43 if (queue == NULL) 44 { 45 exit(1); 46 } 47 48 if (queue->size == 0) 49 { 50 exit(1); 51 } 52 53 for (int i = 0; i < queue->size-1; ++i) 54 { 55 queue->data[i] = queue->data[i + 1]; 56 } 57 queue->size--; 58 } 59 //返回队首元素 60 void* Front_SeqQueue(SeqQueue* queue) 61 { 62 if (queue == NULL) 63 { 64 exit(1); 65 } 66 67 if (queue->size == 0) 68 { 69 exit(1); 70 } 71 72 return queue->data[0]; 73 } 74 //返回队尾元素 75 void* Back_SeqQueue(SeqQueue* queue); 76 //返回队列长度 77 int Size_SeqQueue(SeqQueue* queue) 78 { 79 if (queue == NULL) 80 { 81 exit(1); 82 } 83 84 return queue->size; 85 } 86 //清空队列 87 void Clear_SeqQueue(SeqQueue* queue) 88 { 89 if (queue == NULL) 90 { 91 exit(1); 92 } 93 94 queue->size = 0; 95 } 96 //销毁队列 97 void Free_SeqQueue(SeqQueue* queue) 98 { 99 if (queue == NULL) 100 { 101 exit(1); 102 } 103 104 for (int i = 0; i < MAX_SIZE; ++i) 105 { 106 queue->data[i] = NULL; 107 } 108 queue->size = 0; 109 free(queue); 110 111 }
顺序存储队列测试文件:
1 #include"队列顺序存储.h" 2 #include<string.h> 3 #include<stdlib.h> 4 #include<stdio.h> 5 6 #define M 8 7 #define N 3 8 //数据元素结构体 9 typedef struct PERSON 10 { 11 char name[64]; 12 int age; 13 }Person; 14 15 int main() 16 { 17 //创建循环链表 18 SeqQueue* queue = Init_SeqQueue(); 19 20 //创建数据 21 Person p1 = { "aaa",10 }; 22 Person p2 = { "bbb",20 }; 23 Person p3 = { "ccc",30 }; 24 Person p4 = { "ddd",40 }; 25 Person p5 = { "eee",50 }; 26 27 //入队 28 Push_SeqQueue(queue, (Person*)&p1); 29 Push_SeqQueue(queue, (Person*)&p2); 30 Push_SeqQueue(queue, (Person*)&p3); 31 Push_SeqQueue(queue, (Person*)&p4); 32 Push_SeqQueue(queue, (Person*)&p5); 33 34 //返回队首元素 35 Person* temp = (Person*)Front_SeqQueue(queue); 36 printf("value: %s age: %d ", temp->name, temp->age); 37 38 //出队 39 Pop_SeqQueue(queue); 40 41 //返回队首元素 42 temp = (Person*)Front_SeqQueue(queue); 43 printf("value: %s age: %d ", temp->name, temp->age); 44 45 int size = Size_SeqQueue(queue); 46 printf("长度%d ", size); 47 48 //清空队列 49 Clear_SeqQueue(queue); 50 51 size = Size_SeqQueue(queue); 52 printf("长度%d ", size); 53 //销毁队列 54 Free_SeqQueue(queue); 55 return 0; 56 }
测试结果: