迴圈隊列,迴圈隊列指標

來源:互聯網
上載者:User

迴圈隊列,迴圈隊列指標

     迴圈隊列的實現,本人語文表達能力有限,但寫的代碼簡單易懂,這就不做更多的解釋,直接看代碼。

1:標頭檔:#include"DQueue.h"
#pragma once#include<stdio.h>#include<stdlib.h>#define ElemType int#define SizeFull 20typedef struct Queue{ElemType *data;int sizefull;int front;int rear;}Queue;void Show();  //顯示菜單void InitQueue(Queue *Q);//初始化隊列void DestoryQueue(Queue *Q);//摧毀隊列void ClearQueue(Queue *Q);//清除隊列bool IsFull(Queue *Q);//判空bool IsEmpty(Queue *Q);//判滿int QueueLength(Queue *Q);//求隊列長度void EnQueue(Queue *Q,ElemType x);//入隊列void DeQueue(Queue *Q);//出隊列void ShowQueue(Queue *Q);//顯示隊列

2:功能實現DQueue.cpp:
<pre name="code" class="cpp">#include"DQueue.h"void InitQueue(Queue *Q){Q->data=(ElemType*)malloc(sizeof(ElemType)*SizeFull);if(Q->data==NULL){printf("開闢記憶體失敗\n");return;}Q->sizefull=SizeFull;Q->front=Q->rear=0;}void Show(){printf("************************************************\n");printf("*[1]: 入棧                   [2]: 出棧         *\n");printf("*[3]: 棧長度                 [4]: 摧毀棧       *\n");printf("*[5]: 清理棧                 [6]: 顯示棧       *\n");printf("*[0]: 退棧                                     *\n");printf("************************************************\n");printf("請選擇:");}void DestoryQueue(Queue *Q){free(Q->data);Q->front=Q->rear=Q->sizefull=0;}void ClearQueue(Queue *Q){Q->front=0;Q->rear=0;}bool IsFull(Queue *Q){return (Q->rear+1)%Q->sizefull==Q->front;}bool IsEmpty(Queue *Q){return Q->rear%Q->sizefull==Q->front;}int QueueLength(Queue *Q){return Q->rear%Q->sizefull;}void EnQueue(Queue *Q,ElemType x){if(IsFull(Q)){printf("空間已滿\n");return;}Q->data[Q->rear]=x;Q->rear=(Q->rear+1)%Q->sizefull;}void DeQueue(Queue *Q){if(IsEmpty(Q)){printf("隊列為空白,無法刪除\n");return;}Q->front=(Q->front+1)%Q->sizefull;}void ShowQueue(Queue *Q){for(int i=Q->front;i!=Q->rear;i=(i+1)%Q->sizefull){printf("%d ",Q->data[i]);}printf("\n");}

3:主函數main.cpp

<pre name="code" class="cpp">#include"DQueue.h"void main(){Queue Q;InitQueue(&Q);ElemType x;int Length;int pos=1;while(pos){Show();scanf("%d",&pos);switch(pos){case 1:printf("請輸入資料,以-1結束:");while(scanf("%d",&x),x!=-1){EnQueue(&Q,x);}break;case 2:DeQueue(&Q);break;case 3:Length=QueueLength(&Q);printf("Length=%d\n",Length);break;case 4: DestoryQueue(&Q);break;case 5:ClearQueue(&Q);break;case 6:ShowQueue(&Q);break;default :break;}}}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.