7週目負数正数をキューから追い出す

2771 ワード

<img src="http://img.blog.csdn.net/20151020192516530" alt="" />/*
Copyright (c++)2015,              
All rights reserved.
    :               
      :   
    :2015 10 20 
     :v1.0


    :           a1,a2,…an,     : ai>0 ,ai  , ai<0 ,       , ai=0 ,   
             。            ,                ,          (main  ),   
                   main     。       (   ) ,       。
    :  
    :    ,      
*/
#define MaxSize 5
typedef int ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int front,rear;     /*       */
} SqQueue;


void InitQueue(SqQueue *&q);  //         
void DestroyQueue(SqQueue *&q); //        
bool QueueEmpty(SqQueue *q);  //            
int QueueLength(SqQueue *q);   //         ,      
bool enQueue(SqQueue *&q,ElemType e);   //  
bool deQueue(SqQueue *&q,ElemType &e);  //  
#include <stdio.h>
#include <malloc.h>
#include "sqqueue.h"

void InitQueue(SqQueue *&q)  //         
{
    q=(SqQueue *)malloc (sizeof(SqQueue));
    q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q) //        
{
    free(q);
}
bool QueueEmpty(SqQueue *q)  //            
{
    return(q->front==q->rear);
}


int QueueLength(SqQueue *q)   //         ,      
{
    return (q->rear-q->front+MaxSize)%MaxSize;
}

bool enQueue(SqQueue *&q,ElemType e)   //  
{
    if ((q->rear+1)%MaxSize==q->front)  //     
        return false;
    q->rear=(q->rear+1)%MaxSize;
    q->data[q->rear]=e;
    return true;
}
bool deQueue(SqQueue *&q,ElemType &e)  //  
{
    if (q->front==q->rear)      //     
        return false;
    q->front=(q->front+1)%MaxSize;
    e=q->data[q->front];
    return true;
}
#include <stdio.h>
#include <malloc.h>
#include "sqqueue.h"

int main()
{
    ElemType a,x;
    SqQueue *qu;    //    
    InitQueue(qu);  //     
    while (1)
    {
        printf("  a (      ,    ,0  ):");
        scanf("%d", &a);
        if (a>0)
        {
            if (!enQueue(qu,a))
                printf("     ,    
"); } else if (a<0) { if (!deQueue(qu, x)) printf(" ,
"); } else break; } return 0; }
    :
<img src="http://img.blog.csdn.net/20151020192516530" alt="" />
            ,       ,         ,                ,                  ~         。