C++隊列(數組)

來源:互聯網
上載者:User

標籤:blog   http   os   io   資料   for   2014   ar   

#include "iostream.h"#define MAXSIZE  10typedef  struct  queue{  int q[MAXSIZE] ;int front;int rear;int n;//用於引用計數};void  initQueue(queue*  qq);void  initQueue(queue* qq)  //此處必須是引用或者是指標才有效{    qq->front=0;qq->rear=0;    qq->n=0;for(int i=0;i<MAXSIZE;i++){qq->q[i]=0;}}bool  isQueueEmpty(queue* qq); //判斷隊列是否為空白bool  isQueueEmpty(queue* qq){   if(qq->front==qq->rear)   {      return true;   }   return false;}bool isQueueFull(queue* qq);//判斷是否隊列已滿bool isQueueFull(queue* qq){  if(qq->rear==MAXSIZE)  {      return true;  }  return false;}bool addElement(queue* qq , int Element);bool addElement(queue* qq , int Element){  //從隊列的隊尾入隊if(isQueueFull(qq)==false){   qq->q[qq->rear]=Element;       qq->rear++;       qq->n++; //隊列資料加一       return true;}return false;}void display(queue* qq);//顯示隊列中的所有元素void display(queue* qq){   for(int i=qq->front;i<qq->rear;i++)   {      cout<<qq->q[i]<<endl;     }}int removeElement(queue* qq);int removeElement(queue* qq){   int value = qq->q[qq->front];   qq->front++;      qq->n--;   return value;}void main(){   int number;   //queue*  qq;沒有開闢記憶體空間導致記憶體流失   queue*  qq =new queue();   if(!qq)   {      return ;   }   initQueue(qq);   //迴圈添加n個元素   for(int i=0;i<6;i++)   {      cin>>number;  addElement(qq,number);   }      cout<<"顯示隊列中的所有元素"<<endl;   display(qq);      cout<<"移除隊首元素"<<endl;   removeElement(qq);   cout<<"再次顯示全隊列元素"<<endl;   display(qq);   cout<<"顯示計數(隊列資料的個數)"<<endl;   cout<<qq->n<<endl;}<img src="http://img.blog.csdn.net/20140811105839646" alt="" />

相關文章

聯繫我們

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