データこうぞう


1つのチームを作って、1、3、5、7、9が入って、また列を出ます.
チェーンキューのメソッドとループキューの2つのメソッド.
まず、チェーンキュー:
 #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("
"); 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)
"; 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("
"); 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)
"; 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; }