队列结构实现 发表于 2018-03-06 | 分类于 c++ | 1234567891011121314151617181920212223242526272829303132#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>#include<stdbool.h>typedef struct Queue{ int * pBase; int front; int rear;}QUEUE;/*队列的实现*///初始化void init_queue(QUEUE *);void init_queue(QUEUE *phead){ phead->pBase = (int*)malloc(sizeof(int) * 6); phead->front = 0; phead->rear = 0;}//入队bool en_queue(QUEUE *,int);bool full_queue(QUEUE *);bool is_timy(QUEUE *);bool de_queue(QUEUE *, int*); 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970bool en_queue(QUEUE *phead,int val){ if (full_queue(phead)) return false; phead->pBase[phead->rear] = val; phead->rear = (phead->rear + 1) % 6; return true;}//出队bool de_queue(QUEUE *phead, int *val){ if (is_timy(phead)) return false; *val = phead->pBase[phead->front]; phead->front = (phead->front + 1) % 6; return true;}//是否为空bool is_timy(QUEUE *phead){ if (phead->front == phead->rear) return true; return false;}//是否饱和bool full_queue(QUEUE * phead){ if (phead->front==(phead->rear+1)%6) return true; return false;}//遍历void taverse_queue(QUEUE *);void taverse_queue(QUEUE * phead){ int f = phead->front; int r = phead->rear; if (is_timy(phead)) return; while (f!=r) { printf("%d\n", phead->pBase[f]); f = (f + 1) % 6; }}void main(){ QUEUE queue; int val; init_queue(&queue); //初始化 if (is_timy(&queue)) //是否为空 printf("空队列"); en_queue(&queue, 1); //入队 en_queue(&queue, 2); en_queue(&queue, 3); en_queue(&queue, 4); en_queue(&queue, 5); taverse_queue(&queue); //遍历 if (full_queue(&queue)) //是否饱和 printf("饱和"); en_queue(&queue, 6); if (full_queue(&queue)) //是否饱和 printf("饱和"); de_queue(&queue, &val); de_queue(&queue, &val); de_queue(&queue, &val); de_queue(&queue, &val); de_queue(&queue, &val); de_queue(&queue, &val); taverse_queue(&queue); de_queue(&queue, &val); //出队 printf("%d", val);} 版权声明:本文为博主原创,如若转载请标明出处https://dword.top/队列实现.html -------------end-------------