68 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /*
 | |
| *@文件:queue.h
 | |
| *@作者:‘你遇了我’
 | |
| *@time:2022/11/13
 | |
| *@联系:QQ:321640253
 | |
| *@版本:V1.3
 | |
| *@描述:顺序泛型队列
 | |
| 使用案例
 | |
| int main(){
 | |
| 
 | |
|     SqQuecu a_quecu;                //初始化队列
 | |
|     InitQuecu(&a_quecu,10,sizeof(int));
 | |
|     //出队进队测试
 | |
|     int *tmep;//临时存储进对的返回值
 | |
|     for (size_t i = 0; i < 50; i++)
 | |
|     {
 | |
|         tmep = (int*)enQueue(&a_quecu);
 | |
|         if(tmep!=NULL){                         //返回值为空指针则队满
 | |
|             *tmep=i;
 | |
|         }
 | |
|         else{
 | |
|             printf("出队元素:%d\n",*(int*)deQueue(&a_quecu));
 | |
|             *(int*)enQueue(&a_quecu)=i;                             //出队后重新进队
 | |
|         }
 | |
|         //*******遍历队列**********
 | |
|         for(int i=0;i<10;i++)
 | |
|         printf("%d\n",*(int*)(a_quecu.container+i*a_quecu.NodeSize));
 | |
|         //*******遍历队列**********
 | |
|         printf("-------------------------\n");
 | |
| 
 | |
|     }
 | |
| 
 | |
|     //连续退队测试
 | |
|     for(int i=0;i<5;i++){
 | |
|         printf("出队元素:%d\n",*(int*)deQueue(&a_quecu));
 | |
|         printf("当前队列长度:%d\n",a_quecu.size);
 | |
|     }
 | |
|     DestroyQueue(&a_quecu);
 | |
|     printf("队列已销毁\n");
 | |
|     return 0;
 | |
| }
 | |
| */
 | |
| 
 | |
| #ifndef LIST_QUEUE_H
 | |
| #define LIST_QUEUE_H
 | |
| 
 | |
| #include "stdint.h"
 | |
| #include "stdbool.h"
 | |
| #include "stdlib.h"
 | |
| 
 | |
| 
 | |
| typedef struct{
 | |
|     uint16_t Maxsize;           //数组最大长度
 | |
|     uint16_t NodeSize;          //数组元素的空间大小
 | |
|     void *container;            //容器的指针
 | |
|     int front,rear;             //尾和头
 | |
|     uint16_t size;              //队列长度
 | |
| } SqQuecu;
 | |
| 
 | |
| void InitQuecu(SqQuecu *Q,uint16_t Maxsize,uint16_t NodeSize);  //初始化队列
 | |
| void *enQueue(SqQuecu *Q);                                      //进队
 | |
| void *deQueue(SqQuecu *Q);                                      //出队
 | |
| bool QueueEmpty(SqQuecu *Q);                                    //判断队空
 | |
| bool QueueFull(SqQuecu *Q);                                     //判断队满
 | |
| void DestroyQueue(SqQuecu *Q);                                  //销毁队列(只销毁队列容器,队列指针本身还在)
 | |
| 
 | |
| #endif //LIST_QUEUE_H
 |