队列queue
原则:先进先出 后进后出
1 #include "queue.h" 2 /* 3 typedef int T; 4 5 typedef struct Queue{ 6 T *m_vect; //存储数据的内存空间 7 size_t cap; //总容量 8 size_t index; //队列头的下标位置 9 size_t size; //目前元素的个数 10 }Queue; 11 */ 12 //初始化队列 申请动态内存 13 void init(Queue *que,size_t cap){ 14 que->m_vect = calloc(cap,sizeof(T)); 15 que->cap = cap; 16 que->index = 0; 17 que->size = 0; 18 } 19 //队列是否为空 20 bool isEmpty(Queue *que){ 21 return que->size == 0; 22 } 23 //队列是否已满 24 bool isFull(Queue *que){ 25 return que->size == que->cap; 26 } 27 //容量大小 28 size_t getCap(Queue *que){ 29 return que->cap; 30 } 31 //已经存储的元素个数 32 size_t getSize(Queue *que){ 33 return que->size; 34 } 35 //压入一个元素 //需要判断队列有没有满 36 void push(Queue *que,T data){ 37 que->m_vect[(que->index+que->size)%que->cap] = data; 38 ++que->size; 39 } 40 //弹出一个元素 //需要判断队列还有没有元素 41 T pop(Queue *que){ 42 T data = que->m_vect[que->index]; 43 que->index = (que->index+1)%que->cap; 44 --que->size; 45 return data; 46 } 47 //指向头元素 48 T peekFront(Queue *que){ 49 return que->m_vect[que->index]; 50 } 51 //指向尾元素 52 T peekTail(Queue *que){ 53 return que->m_vect[(que->index+que->size-1)%que->cap]; 54 } 55 //遍历元素 56 void travel(Queue *que){ 57 for(int i=0;i<que->size;i++){ 58 printf("%d ",que->m_vect[(i+que->index)%que->cap]); 59 } 60 printf(" "); 61 } 62 //清空元素 63 void clear(Queue *que){ 64 while(!isEmpty(que)){ 65 pop(que); 66 } 67 //que->index = 0; 68 //que->size = 0; 69 } 70 //销毁队列 71 void destroy(Queue *que){ 72 if(que->m_vect != NULL){ 73 free(que->m_vect); 74 que->m_vect = NULL; 75 } 76 }
双端队列deque
原则:队列两端 都可以进出
1 #include "deque.h" 2 /* 3 typedef int T; 4 5 typedef struct Deque{ 6 T *m_vect; //存储元素的内存空间 7 size_t cap; //总容量 8 size_t size; //当前元素的个数 9 size_t index; //队首的位置 10 11 }Deque; 12 */ 13 //初始化队列 申请动态内存 14 void init(Deque *deq,size_t cap){ 15 deq->m_vect = calloc(cap,sizeof(T)); 16 deq->cap = cap; 17 deq->size = 0; 18 deq->index = 0; 19 } 20 //销毁队列 21 void destroy(Deque *deq){ 22 if(deq->m_vect != NULL){ 23 free(deq->m_vect); 24 deq->m_vect = NULL; 25 } 26 } 27 //判断是否为空 28 bool isEmpty(Deque *deq){ 29 return deq->size == 0; 30 } 31 //判断是否已满 32 bool isFull(Deque *deq){ 33 return deq->cap == deq->size; 34 } 35 //容量 36 size_t getCap(Deque *deq){ 37 return deq->cap; 38 } 39 //当前元素的个数 40 size_t getSize(Deque *deq){ 41 return deq->size; 42 } 43 //从头部压入元素 44 void pushHead(Deque *deq,T data){ 45 /* 46 if(deq->index==0){ 47 deq->index = deq->cap-1; 48 }else{ 49 --deq->index; 50 }*/ 51 deq->index = deq->index==0?deq->cap-1:deq->index-1; 52 deq->m_vect[deq->index] = data; 53 ++deq->size; 54 } 55 //从尾部压入元素 56 void pushTail(Deque *deq,T data){ 57 deq->m_vect[(deq->size+deq->index)%deq->cap] = data; 58 ++deq->size; 59 } 60 //从头部弹出元素 61 T popHead(Deque *deq){ 62 T data = deq->m_vect[deq->index]; 63 deq->index = (deq->index+1)%deq->cap; 64 --deq->size; 65 return data; 66 } 67 //从尾部弹出元素 68 T popTail(Deque *deq){ 69 T data = deq->m_vect[(deq->size+deq->index-1)%deq->cap]; 70 --deq->size; 71 return data;//return deq->m_vect[(--deq->size+deq->index)%deq->cap]; 72 } 73 //指向队列头部 74 T peekHead(Deque *deq){ 75 return deq->m_vect[deq->index]; 76 } 77 //指向队列尾部 78 T peekTail(Deque *deq){ 79 return deq->m_vect[(deq->index+deq->size-1)%deq->cap]; 80 } 81 //遍历元素 82 void travel(Deque *deq){ 83 for(int i=0;i<deq->size;i++){ 84 printf("%d ",deq->m_vect[(deq->index+i)%deq->cap]); 85 } 86 printf(" "); 87 } 88 //清空队列 89 void clear(Deque *deq){ 90 while(!isEmpty(deq)){ 91 popTail(deq); 92 } 93 //deq->size = 0; 94 //deq->index = 0; 95 }