資料結構(隊列)

來源:互聯網
上載者:User

建立一個隊,1,3,5,7,9入隊,再出隊列。

鏈隊列的方法和迴圈隊列兩種方法。

首先是鏈隊列:

 #include<string> #include<iostream> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2  using namespace std; typedef int Status; /* Status是函數的類型,其值是函數結果狀態碼,如OK等 */ typedef int QElemType; typedef struct QNode {   QElemType data;   struct QNode *next; }QNode,*QueuePtr; typedef struct {   QueuePtr front,rear; /* 隊頭、隊尾指標 */ }LinkQueue;   Status InitQueue(LinkQueue &Q) { // 構造一個空隊列Q   if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))     exit(OVERFLOW);   Q.front->next=NULL;   return OK; } Status QueueEmpty(LinkQueue Q) { // 若Q為空白隊列,則返回TRUE,否則返回FALSE   if(Q.front==Q.rear)     return TRUE;   else     return FALSE; } Status GetHead(LinkQueue Q,QElemType &e) { // 若隊列不空,則用e返回Q的隊頭元素,並返回OK,否則返回ERROR   QueuePtr p;   if(Q.front==Q.rear)     return ERROR;   p=Q.front->next;   e=p->data;   return OK; } Status EnQueue(LinkQueue &Q,QElemType e) { // 插入元素e為Q的新的隊尾元素   QueuePtr p;   if(!(p=(QueuePtr)malloc(sizeof(QNode)))) // 儲存分配失敗     exit(OVERFLOW);   p->data=e;   p->next=NULL;   Q.rear->next=p;   Q.rear=p;   return OK; } Status DeQueue(LinkQueue &Q,QElemType &e) { // 若隊列不空,刪除Q的隊頭元素,用e返回其值,並返回OK,否則返回ERROR   QueuePtr p;   if(Q.front==Q.rear)     return ERROR;   p=Q.front->next;   e=p->data;   Q.front->next=p->next;   if(Q.rear==p)     Q.rear=Q.front;   free(p);   return OK; } Status QueueTraverse(LinkQueue Q,void(*vi)(QElemType)) { // 從隊頭到隊尾依次對隊列Q中每個元素調用函數vi()。一旦vi失敗,則操作失敗   QueuePtr p;   p=Q.front->next;   while(p)   {     vi(p->data);     p=p->next;   }   printf("\n");   return OK; }  void visit(QElemType i) {  cout<<i<<" "; } int main() {   int i;   QElemType d;   LinkQueue q;   i=InitQueue(q);   EnQueue(q,1);   EnQueue(q,3);   EnQueue(q,5);   EnQueue(q,7);   EnQueue(q,9);   cout<<"已經插入5個元素(1,3,5,7,9)\n";   cout<<"隊列的元素依次為:";   QueueTraverse(q,visit);   cout<<endl;   i=GetHead(q,d);   if(i==OK)     cout<<"隊頭元素是: "<<d<<endl;   DeQueue(q,d);   cout<<"刪除了隊頭元素,"<<d;   i=GetHead(q,d);   if(i==OK)     cout<<"\t新的隊頭元素是: "<<d<<endl;   DeQueue(q,d);   cout<<"刪除了隊頭元素,"<<d;   i=GetHead(q,d);   if(i==OK)     cout<<"\t新的隊頭元素是: "<<d<<endl;    DeQueue(q,d);   cout<<"刪除了隊頭元素,"<<d;   i=GetHead(q,d);   if(i==OK)     cout<<"\t新的隊頭元素是: "<<d<<endl;   DeQueue(q,d);   cout<<"刪除了隊頭元素,"<<d;   i=GetHead(q,d);   if(i==OK)     cout<<"\t新的隊頭元素是: "<<d<<endl;    DeQueue(q,d);   cout<<"刪除了最後一個元素"<<d<<endl;   if(QueueEmpty(q))   cout<<"現在隊列是一個空隊列";   else cout<<"隊列非空";   cout<<endl;      }

迴圈鏈表的程式:

 #include<iostream> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2  using namespace std; typedef int Status; /* Status是函數的類型,其值是函數結果狀態碼,如OK等 */ typedef int QElemType;  #define MAXQSIZE 5 // 最大隊列長度(對於迴圈隊列,最大隊列長度要減1) typedef struct {   QElemType *base; // 初始化的動態分配儲存空間   int front; // 頭指標,若隊列不空,指向隊列頭元素   int rear; // 尾指標,若隊列不空,指向隊列尾元素的下一個位置 }SqQueue; /* bo3-3.c 迴圈隊列(儲存結構由c3-3.h定義)的基本操作(9個) */ Status InitQueue(SqQueue &Q) { /* 構造一個空隊列Q */   (Q).base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));   if(!(Q).base) /* 儲存分配失敗 */     exit(OVERFLOW);   (Q).front=(Q).rear=0;   return OK; } Status QueueEmpty(SqQueue Q) { /* 若隊列Q為空白隊列,則返回TRUE,否則返回FALSE */   if(Q.front==Q.rear) /* 隊列空的標誌 */     return TRUE;   else     return FALSE; } Status GetHead(SqQueue Q,QElemType &e) { /* 若隊列不空,則用e返回Q的隊頭元素,並返回OK,否則返回ERROR */   if(Q.front==Q.rear) /* 隊列空 */     return ERROR;   e=*(Q.base+Q.front);   return OK; } Status EnQueue(SqQueue &Q,QElemType e) { /* 插入元素e為Q的新的隊尾元素 */   if(((Q).rear+1)%MAXQSIZE==(Q).front) /* 隊列滿 */     return ERROR;   (Q).base[(Q).rear]=e;   (Q).rear=((Q).rear+1)%MAXQSIZE;   return OK; } Status DeQueue(SqQueue &Q,QElemType &e) { /* 若隊列不空,則刪除Q的隊頭元素,用e返回其值,並返回OK;否則返回ERROR */   if((Q).front==(Q).rear) /* 隊列空 */     return ERROR;   e=(Q).base[(Q).front];   (Q).front=((Q).front+1)%MAXQSIZE;   return OK; } Status QueueTraverse(SqQueue Q,void(*vi)(QElemType)) { /* 從隊頭到隊尾依次對隊列Q中每個元素調用函數vi().一旦vi失敗,則操作失敗 */   int i;   i=Q.front;   while(i!=Q.rear)   {     vi(*(Q.base+i));     i=(i+1)%MAXQSIZE;   }   printf("\n");   return OK; }void visit(QElemType i) {  cout<<i<<" "; }int main() {   int i;   QElemType d;   SqQueue q;   i=InitQueue(q);   EnQueue(q,1);   EnQueue(q,3);   EnQueue(q,5);   EnQueue(q,7);   EnQueue(q,9);   cout<<"已經插入5個元素(1,3,5,7,9)\n";   cout<<"隊列的元素依次為:";   QueueTraverse(q,visit);   cout<<endl;   i=GetHead(q,d);   if(i==OK)     cout<<"隊頭元素是: "<<d<<endl;   DeQueue(q,d);   cout<<"刪除了隊頭元素,"<<d;   i=GetHead(q,d);   if(i==OK)     cout<<"\t新的隊頭元素是: "<<d<<endl;   DeQueue(q,d);   cout<<"刪除了隊頭元素,"<<d;   i=GetHead(q,d);   if(i==OK)     cout<<"\t新的隊頭元素是: "<<d<<endl;    DeQueue(q,d);   cout<<"刪除了隊頭元素,"<<d;   i=GetHead(q,d);   if(i==OK)     cout<<"\t新的隊頭元素是: "<<d<<endl;   DeQueue(q,d);   cout<<"刪除了隊頭元素,"<<d;   i=GetHead(q,d);   if(i==OK)     cout<<"\t新的隊頭元素是: "<<d<<endl;    DeQueue(q,d);   cout<<"\t刪除了最後一個元素"<<d<<endl;   if(QueueEmpty(q))   cout<<"現在隊列是一個空隊列";   else cout<<"隊列非空";   cout<<endl;        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.