迴圈隊列基本操作

來源:互聯網
上載者:User

#include<stdio.h>//迴圈隊列的儲存結構
#include<stdlib.h>
#define OVERFLOW -2
#define maxqsize 100
typedef int QElemType;
typedef struct {
 QElemType *base;
 int front;
 int rear;
}SqQueue;
void InitQueue(SqQueue *Q)
{//構造一個空隊列Q
 Q->base=(QElemType *)malloc(maxqsize * sizeof(QElemType));
 if(!Q->base) exit(OVERFLOW);
 Q->front=Q->rear=0;
}
void ClearQueue(SqQueue *Q)
{//置空
 Q->front=Q->rear;
}
void EnQueue(SqQueue *Q,QElemType e)
{//入隊
 printf("please input tne number you want input/n");
 scanf("%d",&e);
 if((Q->rear+1)%maxqsize==Q->front) {printf("sorry The Queue is full!/nNOW The Program will be out!/n");exit(0);}
 Q->base[Q->rear]=e;
 Q->rear=(Q->rear+1)%maxqsize;
 printf("congratulation Enqueue successful/n");
}
void DeQueue(SqQueue *Q,QElemType e)
{//出隊
 if(Q->front==Q->rear) {printf("Sorry the queue is empty/nNOW The Program will be out!/n");exit(0);}
 e=Q->base[Q->front];
 Q->front=(Q->front+1)%maxqsize;
 printf("congratulation dequeue successful/n");
}
void QueueLength(SqQueue *Q)
{//求隊列長度
 printf("the length of the queue is %d/n",(Q->rear-Q->front+maxqsize)%maxqsize);
}
void PrintQueue(SqQueue *Q)
{//列印佇列
 int i=1;
 if(Q->front==Q->rear) printf("Sorry The Queue is Empty/n");
 while(Q->front!=Q->rear)//while(Q->front!=(Q->rear+1)%maxqsize)以什麼作為條件?空隊列???
 {
  printf("Queue the %dth is %d /n",i++,Q->base[Q->front]);
  Q->front++;
 }
}
void DestroyQueue(SqQueue *Q)
{//銷毀隊列
 if(!Q->base) free(Q->base);
 Q->base=NULL;
 Q->rear=Q->front;
 printf("DestroyQueue is OK!/nNOW The Program will be out!/n");
 exit(0);
}
int main()
{
 SqQueue Q;
 QElemType e;
 int i;
 InitQueue(&Q);
 printf("please input what you want to do/nfor 0:over/n1:clearQueue/n2:EnQueue/n3:DeQueue/n4:find Queue length/n5:print Queue/n");
 scanf("%d",&i);
 while(i)
 {
  switch(i)
  {
  case 1:ClearQueue(&Q);break;
  case 2:EnQueue(&Q,e);break;
  case 3:DeQueue(&Q,e);break;
  case 4:QueueLength(&Q);break;
  case 5:PrintQueue(&Q);break;
  }
  printf("you can input aagain :/nfor 0:over/n1:clearQueue/n2:EnQueue/n3:DeQueue/n4:find Queue length/n5:print Queue/n");
  scanf("%d",&i);
 }
 return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.