25. 데이터 구조 - 선형 표 - 순서 대기 열

2916 단어
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /*           */

typedef int Status;
typedef int QElemType; /* QElemType          ,     int */

/*             */
typedef struct
{
    QElemType data[MAXSIZE];
    int front;        /*     */
    int rear;        /*    ,     ,              */
}SqQueue;

Status visit(QElemType c)
{
    printf("%d ",c);
    return OK;
}

/*         Q */
Status InitQueue(SqQueue *Q)
{
    Q->front=0;
    Q->rear=0;
    return  OK;
}

/*  Q      */
Status ClearQueue(SqQueue *Q)
{
    Q->front=Q->rear=0;
    return OK;
}

/*    Q    ,   TRUE,    FALSE */
Status QueueEmpty(SqQueue Q)
{
    if(Q.front==Q.rear) /*        */
        return TRUE;
    else
        return FALSE;
}

/*   Q     ,           */
int QueueLength(SqQueue Q)
{
    return  (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}

/*      ,  e  Q     ,   OK,    ERROR */
Status GetHead(SqQueue Q,QElemType *e)
{
    if(Q.front==Q.rear) /*     */
        return ERROR;
    *e=Q.data[Q.front];
    return OK;
}

/*      ,     e Q       */
Status EnQueue(SqQueue *Q,QElemType e)
{
    if ((Q->rear+1)%MAXSIZE == Q->front)    /*        */
        return ERROR;
    Q->data[Q->rear]=e;            /*    e      */
    Q->rear=(Q->rear+1)%MAXSIZE;/* rear        , */
    /*             */
    return  OK;
}

/*      ,   Q     , e     */
Status DeQueue(SqQueue *Q,QElemType *e)
{
    if (Q->front == Q->rear)            /*        */
        return ERROR;
    *e=Q->data[Q->front];                /*         e */
    Q->front=(Q->front+1)%MAXSIZE;    /* front        , */
    /*             */
    return  OK;
}

/*            Q        */
Status QueueTraverse(SqQueue Q)
{
    int i;
    i=Q.front;
    while((i+Q.front)!=Q.rear)
    {
        visit(Q.data[i]);
        i=(i+1)%MAXSIZE;
    }
    printf("
"); return OK; } int main() { Status j; int i=0,l; QElemType d; SqQueue Q; InitQueue(&Q); printf(" , ?%u(1: 0: )
",QueueEmpty(Q)); printf(" ( %d ),-1 : ",MAXSIZE-1); do { /* scanf("%d",&d); */ d=i+100; if(d==-1) break; i++; EnQueue(&Q,d); }while(i0) printf(" %d :
",l-2); while(QueueLength(Q)>2) { DeQueue(&Q,&d); printf(" %d
",d); } j=GetHead(Q,&d); if(j) printf(" : %d
",d); ClearQueue(&Q); printf(" , ?%u(1: 0: )
",QueueEmpty(Q)); return 0; }

좋은 웹페이지 즐겨찾기