回 帖 发 新 帖 刷新版面

主题:这是个队列的源代码,错在那里??

#include<stdio.h>
#include<stdlib.h>
#define MAXQSIZE 100;
typedef char QElemType;
typedef int Statue;
typedef struct SqQueue{
    QElemType *base;
    int front;
    int rear;
}SqQueue;

Status InitQueue(SqQueue *Q){
    Q->base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
    if(!Q->base)
        exit(-1);
    Q->front=Q->rear=0;
    return 1;
}
int Queuelength(SqQueue *Q){
    return (Q->rear-Q->front+MAXQSIZE)%MAXQSIZE;
}
Statue EnQueue(SqQueue *Q,QElenType *e)
{
    if((Q->tear+1)%MAXQSIZE==Q->front){
        printf("man");
        exit(-1);
    }
    Q->base[Q->rear]=*e;
    Q->rear=(Q->rear+1)%MAXQSIZE;
    return 1;
}
void Dequeue(SqQueue *Q,QElemType *e){
    if(Q->rear==Q->front){
        printf("kongzhan");
        exit(-1);
    }
    *e=Q->base[Q->front];
    Q->front=(Q->front+1)%MAXQSIZE;
}
Statue main()
{
    SqQueue S;
    QElemType a;
    InitQueue(&S);
    scanf("%c",&a);
    while(a!='\n'){
        printf("shuru yuansu");
        scanf("%c",&a);
        EnQueue(&S,&a);
    }
    DeQueue(&S,&a);
    printf("shanchude yuansushi %c",a);
    return 1;

}

回复列表 (共3个回复)

沙发

改好了


#include <stdio.h>
#include <stdlib.h>

#define MAXQSIZE 100
#define OK 1
#define NULL 0
#define ERROR -1

typedef char QElemType;
typedef int Statue;
typedef struct SqQueue{
    QElemType *base;
    int front;
    int rear;
}SqQueue;

Statue InitQueue(SqQueue *Q)
{
    Q->base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
    if(!Q->base)
        exit(ERROR);
    Q->front = Q->rear = NULL;
    return OK;
}

Statue Queuelength(SqQueue *Q)
{
    return (Q->rear - Q->front + MAXQSIZE) % MAXQSIZE;
}

Statue EnQueue(SqQueue *Q,QElemType *e)
{
    if((Q->rear + 1) % MAXQSIZE == Q->front){
        printf("FULL\n");
        exit(ERROR);
    }
    Q->base[Q->rear]=*e;
    Q->rear=(Q->rear+1)%MAXQSIZE;
    return OK;
}

QElemType DeQueue(SqQueue *Q)
{
    QElemType e;
    if(Q->rear + 1== Q->front){
      printf("empty stack\n");
      exit(ERROR);
    }
     e = Q->base[Q->front];
     Q->front=(Q->front+1)%MAXQSIZE;
    return e;
}

void Prn_Queue(SqQueue *Q)
{
    int i, j;

    i = Q->rear;
    j = Q->front;

    while(Q->rear != Q->front){
    printf("%c ", Q->base[Q->front]);
    Q->front = (Q->front + 1) % MAXQSIZE;
    }
    putchar('\n');
    Q->rear = i;
    Q->front = j;
}

Statue main()
{
    SqQueue S;
    int i;
    QElemType a, b;

    InitQueue(&S);
    printf("enter the elements('#' means over):\n");
    while( (a=getchar()) != '#'){
        EnQueue(&S,&a);
    }
    printf("print the queue: ");
    Prn_Queue(&S);
    printf("the first element of the queue:");
    b = DeQueue(&S);
    printf(" %c\n", b);
    getch();
    return OK;
}

板凳


QElemType DeQueue(SqQueue *Q)
{
    QElemType e;
    if(Q->rear[color=800000] [color=00FF00]+ 1[/color][/color]== Q->front){  //应该去了吧
      printf("empty stack\n");
      exit(ERROR);
    }
     e = Q->base[Q->front];
     Q->front=(Q->front+1)%MAXQSIZE;
    return e;
}

3 楼


去掉吧
我的疏忽

我来回复

您尚未登录,请登录后再回复。点此登录或注册