队列入队和出队程序演示
生活随笔
收集整理的這篇文章主要介紹了
队列入队和出队程序演示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <stdio.h>
#include <string.h>
#include <malloc.h>typedef struct Queue {int * pBase;int front;int rear;} QUEUE;void init(QUEUE *);
bool en_queue(QUEUE *pQ, int val); //入隊
void traverse_queue(QUEUE * pQ);
bool full_queue(QUEUE * pQ);bool out_queue(QUEUE *pQ, int *pVal); //出隊 *pVal用來保存出隊的元素
bool empty_queue(QUEUE *pQ); //判空int main(void) {QUEUE Q;int val; //保存出隊元素init(&Q);en_queue(&Q, 1);en_queue(&Q, 2);en_queue(&Q, 3);en_queue(&Q, 4);en_queue(&Q, 5);en_queue(&Q, 6);en_queue(&Q, 7);en_queue(&Q, 8);traverse_queue(&Q); //遍歷輸出if (out_queue(&Q, &val)) { //出隊printf("出隊成功 出隊元素 %d\n",val);//成功}else {printf("出隊失敗 出隊元素 %d\n", val);//失敗}traverse_queue(&Q); //遍歷輸出while (true){}return 0;
}void init(QUEUE *pQ) {pQ->pBase = (int *)malloc(sizeof(int) * 6);pQ->front = 0;pQ->rear = 0;
}//入隊
bool en_queue(QUEUE *pQ, int val) {if (full_queue(pQ)) {return false;}else {pQ->pBase[pQ->rear] = val;pQ->rear=(pQ->rear + 1) % 6;return true;}}//判斷隊列是不是滿了
bool full_queue(QUEUE * pQ) {if ( (pQ->rear + 1)%6==pQ->front) {return true;}else {return false;}}//遍歷輸出
void traverse_queue(QUEUE *pQ) {int i=pQ->front;while (i != pQ->rear) {printf("%d ", pQ->pBase[i]);i = (i + 1) % 6;}printf("\n");return;
}//出隊
bool out_queue(QUEUE *pQ, int *pVal) {if (empty_queue(pQ)) {return false; }else {*pVal = pQ->pBase[pQ->front]; //隊列從頭部出隊,保存頭部出隊的元素pQ->front = (pQ->front + 1) % 6;}
}//隊列判空
bool empty_queue(QUEUE *pQ) {if (pQ->front == pQ->rear) {return true;}else {return false;}}
總結
以上是生活随笔為你收集整理的队列入队和出队程序演示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql5.6 二进制免编译安装
- 下一篇: linux搜狗输入法配置,liunx--