キューテンプレート


#include <iostream>
#include <cstdio>
#include <malloc.h>
using namespace std;
#define OVERFLOW 0
#define TRUE 1
#define FLASE 0
#define OK 1
#define ERROR 0
typedef int Status;
typedef int QElemType;
typedef struct QNode
{
   QElemType data;
   QNode *next;
}*QueueRtr;
struct LinkQueue
{
    QueueRtr ifront,irear;
};
//    
void InitQueue(LinkQueue &Q);
void DestroyQueue(LinkQueue &Q);
void ClearQueue(LinkQueue &Q);
Status QueueEmpty(LinkQueue Q);
int QueueLength(LinkQueue Q);
void EnQueue(LinkQueue &Q,QElemType e);//    e          
Status DeQueue(LinkQueue &Q,QElemType &e);//   Q  ,  Q     , e    
void QueueTraverse(LinkQueue &Q);
Status GetHead(LinkQueue Q,QElemType  e); //      
void InitQueue(LinkQueue &Q)
{
    Q.ifront=Q.irear=(QueueRtr)malloc(sizeof(QNode));//     
    if(!Q.ifront)
        exit(OVERFLOW);
    Q.ifront->next=NULL;//    next   
}
void DestroyQueue(LinkQueue &Q)
{
    while(Q.ifront) //Q.ifront   
    {
        Q.irear=Q.ifront->next;//Q.rear  Q.front      
        free(Q.ifront);//  Q.front     
        Q.ifront=Q.irear; //Q.front  Q,front      
    }
}
Status GetHead(LinkQueue Q,QElemType  e)
{
    QueueRtr p;
    if(Q.ifront==Q.irear)
        return ERROR;  //   
    p=Q.ifront->next;//p      
    e=p->data;    //         e
    return e;
}
void ClearQueue(LinkQueue &Q)
{
    DestroyQueue(Q); //    Q
    InitQueue(Q); //       
}

Status QueueEmpty(LinkQueue Q)
{
    if(Q.ifront->next==NULL)
        return TRUE;
    else
        return FLASE;
}
int QueueLength(LinkQueue Q)
{
    int i=0;
    QueueRtr p=Q.ifront;//p     
    while(Q.irear!=p)  //p        
    {
        i++;
        p=p->next; //p       
    }
    return i;
}
void EnQueue(LinkQueue &Q,QElemType e)
{//    e          
    QueueRtr p;
    p=(QueueRtr)malloc(sizeof(QNode));
    if(!p)
        exit(OVERFLOW);
    p->data=e;    // e      
    p->next=NULL;   //         
    Q.irear->next=p;   //             
    Q.irear=p;  //         
}
Status DeQueue(LinkQueue &Q,QElemType  &e)
{//   Q  ,  Q     , e    
    QueueRtr p;
    if(Q.ifront==Q.irear)
        return ERROR;
    p=Q.ifront->next;//p    
    e=p->data;   //  
    Q.ifront->next=p->next;//          
    if(Q.irear==p)  //s        
        Q.irear=Q.ifront;
    free(p);
    return OK;
}
void QueueTraverse(LinkQueue &Q)
{
    QueueRtr p=Q.ifront->next;
    while(p)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
    printf("
"); } int main() { QElemType d; LinkQueue q; InitQueue(q); printf("
"); printf(" ?(YES:1 NO:0)%d
",QueueEmpty(q)); printf(" :%d
",QueueLength(q)); EnQueue(q,1);// 3 EnQueue(q,-1); EnQueue(q,0); printf(" ?(YES:1 NO:0)%d
",QueueEmpty(q)); printf(" :%d
",QueueLength(q)); printf(" :"); QueueTraverse(q); printf(" :%d
",GetHead(q,d)); DeQueue(q,d); printf(" :%d
",d); int k,i=GetHead(q,k); printf(" :%d
",i); ClearQueue(q); printf(" q.ifront=%d,q.irear=%d q.ifront->next=%d
",q.ifront,q.irear,q.ifront->next); //DestroyQueue(q); printf(" ?(YES:1 NO:0)%d
",QueueEmpty(q)); //printf(" q.ifront=%d,q.irear=%d q.ifront->next=%d
",q.ifront,q.irear,q.ifront->next); return 0; }