Q未被初始化的问题
我在做循环队列的问题,先来一个我自己写的,可以运行的看看
SequenceQueue1.h
typedef struct
{
ElemType queue[MaxQueueSize];
int front;
int count;
}*SequenceQueue;
void QueueInitiate(SequenceQueue Q)
{
Q->front = 0;
Q->count = 0;
}
int QueueNotEmpty(SequenceQueue Q)
{
if (Q->front == 0)
return 0;
else
return 1;
}
int QueueAppend(SequenceQueue Q, ElemType x)
{
if (Q->count > 0 && (Q->count + Q->front) == Q->front)
{
printf("队列已满无法插入!\n");
return 0;
}
else
{
Q->queue[(Q->front + Q->count) % MaxQueueSize] = x;
Q->count++;
return 1;
}
}
int QueueDelete(SequenceQueue Q, ElemType *d)
{
if (Q->count == 0)
{
printf("循环队列已空无数据元素出队列\n");
return 0;
}
else
{
*d = Q->queue[Q->front];
Q->front = (Q->front + 1) % MaxQueueSize;
Q->count--;
return 1;
}
}
void QueueGet(SequenceQueue Q, ElemType *d)
{
if (Q->count == 0)
{
printf("循环队列已空无数据元素可取!\n");
}
else
{
*d = Q->queue[Q->front];
}
}
cpp文件
#include<stdio.h>
#include<stdlib.h>
#define MaxQueueSize 10
typedef int ElemType;
#include"SequenceQueue1.h"
int main()
{
SequenceQueue Q;
QueueInitiate(&Q);
int i, d;
for (i = 0; i < 10; i++)
QueueAppend(&Q, i);
for (i = 0; i < 5; i++)
{
QueueGet(&Q, &d);
printf(" %d", d);
QueueDelete(&Q, &d);
}
printf("\n");
for (i = 0; i < 5; i++)
{
QueueAppend(&Q, i);
}
for (i = 0; i < 10; i++)
{
QueueGet(&Q, &d);
printf(" %d", d);
QueueDelete(&Q, &d);
}
printf("\n");
system("pause");
}
这个是可以正常运行的,但下面的这个就不可以了,我只是把结构体变量名弄为指针的,就不行了,没有报错,但是运行就不行,下面错误代码
头文件
typedef struct
{
ElemType queue[MaxQueueSize];
int front;
int count;
}*SequenceQueue;
void QueueInitiate(SequenceQueue Q)
{
Q->front = 0;
Q->count = 0;
}
int QueueNotEmpty(SequenceQueue Q)
{
if (Q->front == 0)
return 0;
else
return 1;
}
int QueueAppend(SequenceQueue Q, ElemType x)
{
if (Q->count > 0 && (Q->count + Q->front) == Q->front)
{
printf("队列已满无法插入!\n");
return 0;
}
else
{
Q->queue[(Q->front + Q->count) % MaxQueueSize] = x;
Q->count++;
return 1;
}
}
int QueueDelete(SequenceQueue Q, ElemType *d)
{
if (Q->count == 0)
{
printf("循环队列已空无数据元素出队列\n");
return 0;
}
else
{
*d = Q->queue[Q->front];
Q->front = (Q->front + 1) % MaxQueueSize;
Q->count--;
return 1;
}
}
void QueueGet(SequenceQueue Q, ElemType *d)
{
if (Q->count == 0)
{
printf("循环队列已空无数据元素可取!\n");
}
else
{
*d = Q->queue[Q->front];
}
}
cpp文件
#include<stdio.h>
#include<stdlib.h>
#define MaxQueueSize 10
typedef int ElemType;
#include"SequenceQueue1.h"
int main()
{
SequenceQueue Q;
QueueInitiate(Q);
int i, d;
for (i = 0; i < 10; i++)
QueueAppend(Q, i);
for (i = 0; i < 5; i++)
{
QueueGet(Q, &d);
printf(" %d", d);
QueueDelete(Q, &d);
}
printf("\n");
for (i = 0; i < 5; i++)
{
QueueAppend(Q, i);
}
for (i = 0; i < 10; i++)
{
QueueGet(Q, &d);
printf(" %d", d);
QueueDelete(Q, &d);
}
printf("\n");
system("pause");
}
为什么第二种会提示使用了未被初始化的Q,我的Q不是已经初始化了吗,求指教