回 帖 发 新 帖 刷新版面

主题:数据结构错误

#include<stdio.h>
typedef char ElemType;
#define MaxSize 100
typedef struct{
    ElemType queue[MaxSize];
    int front,rear;

}queuetype;

void initqueue(queuetype *Q)
{
    Q->front=Q->rear=-1;
}
void enter(queuetype *Q,ElemType x)
{
    if (Q->rear==MaxSize) printf("error");
    else 
    {
        Q->rear++;
        Q->queue[Q->rear]=x;

    }
}
void delete1(queuetype *Q)
{
    if (Q->rear=Q->front=-1)printf("队列为空");
    else
            Q->front++;
}
ElemType gethead(queuetype *Q)
{
    if (Q->rear=Q->front=-1)printf("队列为空") ;
    else 
    {
        return(Q->queue[Q->front+1]);
    }
}
int empty(queuetype *Q)
{
    if (Q->rear=Q->front=-1)return 1;
    else return 0;
}
void display(queuetype *Q)
{int i;
    if(Q->rear=Q->front=-1)printf("对列为空");
    else 
    {
        for (i=Q->front+1;i<=Q->rear;i++)
            printf("%c\n",Q->queue[i]);
    }
}
void main()
{
    queuetype *qu;
    printf("初始化队列\n");
        
initqueue(qu);
printf("队列空:%d",empty(qu));
    printf("依次入队a,b,c,d:\n");
    enter(qu,'a');
    enter(qu,'b');
    enter(qu,'c');
    enter(qu,'d');
    display(qu);
    printf("出队一次:\n");
    delete1(qu);
    printf("队首元素:",gethead(qu));
}
//程序运行的时候为什么会出现内存错误????????请高手指教

回复列表 (共1个回复)

沙发

if (Q->rear=Q->front=-1)printf("队列为空");
这里怎么写成赋值语句了?

队列一般不这样写,一般写成循环队列,像这样的队列不管MaxSize有多大,随着入队操作的增加最终会"error"。

我来回复

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