求错——循环队列

作者在 2011-04-11 11:22:31 发布以下内容
//循环队列
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20

typedef struct
{
    int *base;
    int rear;
    int front;
}SqQueue;

void Instructions(void)
{
    printf("Enter your choice:\n"
        "1、Insert an element to the Queue\n"
        "2、Delete an element from the Queue\n"
        "3、End\n");
}

void InitQueue(SqQueue Q)
{
    Q.base=(int *)malloc(MAXSIZE*sizeof(int));
    Q.front=Q.rear=0;
}

int QueueEmpty(SqQueue Q)
{
    return Q.rear==(Q.front+1)%MAXSIZE;
}

int QueueFull(SqQueue Q)
{
    return Q.front==(Q.rear+1)%MAXSIZE;
}

void InsertQueue(SqQueue Q,int elem)
{
    if(QueueFull(Q)) {printf("Overflow\n"); exit(0);}
    Q.base[Q.rear]=elem;
    Q.rear=(Q.rear+1)%MAXSIZE;
}

void DeleteQueue(SqQueue Q,int elem)
{
    if(QueueEmpty(Q)) {printf("Null\n"); exit(0);}
    elem=Q.base[Q.front];
    Q.front=(Q.front+1)%MAXSIZE;
}

void main()
{
    int choice,elem;
    SqQueue Q;
    InitQueue(Q);
    Instructions();
    scanf("%d",&choice);
    while(choice!=3)
    {
        switch(choice)
        {
        case 1:printf("Please enter the elem:\t");scanf("%d",&elem);InsertQueue(Q,elem);break;
        case 2:DeleteQueue(Q,elem);break;
        default:printf("Choice Error!\n");break;
        }
        scanf("%d",&choice);
    }
    printf("T");
}
数据结构 | 阅读 664 次
文章评论,共0条
游客请输入验证码
文章归档
最新评论