数据结构循环队列的c语言实现,程序通过了编译连接但是运行时崩溃了……

#include<string.h> // 字符串函数头文件
#include<ctype.h> // 字符函数头文件
#include<malloc.h> // malloc()等
#include<limits.h> // INT_MAX等
#include<stdio.h> // 标准输入输出头文件
#include<stdlib.h> // atoi(),exit()
#include<io.h> // eof()
#include<math.h> // 数学函数头文件
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码
#define MAX_QSIZE 100 // 最大队列长度+1
#define N 10
typedef int QElemType;
typedef struct
{
char name[20];
char id[20];
int x;
int y;
int z;
}point;
typedef struct
{ point* base; // 初始化的动态分配存储空间
int front; // 头指针,若队列不空,指向队列头元素
int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;

void InitQueue(SqQueue &Q)
{ // 构造一个空队列Q
Q.base=(point *)malloc(MAX_QSIZE *sizeof(point));
if(!Q.base) // 存储分配失败
exit(OVERFLOW);
Q.front=Q.rear=0;
}

Status QueueEmpty(SqQueue Q)
{ // 若队列Q为空队列,则返回TRUE;否则返回FALSE
if(Q.front==Q.rear) // 队列空的标志
return TRUE;
else
return FALSE;
}

Status EnQueue(SqQueue &Q,point e)
{ // 插入元素e为队列Q的新的队尾元素
if((Q.rear+1)%MAX_QSIZE==Q.front) // 队列满
return ERROR;
Q.base[Q.rear]=e; // 将e插在队尾
Q.rear=(Q.rear+1)%MAX_QSIZE; // 队尾指针+1后对MAX_QSIZE取余
return OK;
}

int QueueLength(SqQueue Q)
{ // 返回队列Q的元素个数,即队列的长度
return(Q.rear-Q.front+MAX_QSIZE)%MAX_QSIZE;
}

DeQueue(SqQueue &Q,point &e)
{ // 若队列Q不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
if(Q.front==Q.rear) // 队列空
return ERROR;
e=Q.base[Q.front]; // 将队头元素的值赋给e
Q.front=(Q.front+1)%MAX_QSIZE; // 移动队头指针
return OK;
}

void QueueTraverse(SqQueue Q,point e)
{ // 从队头到队尾依次对队列Q中每个元素输出
int i=Q.front; // i最初指向队头元素
while(i!=Q.rear) // i指向队列Q中的元素
e=Q.base[i];//把i指向的元素赋给e
{
printf("name:%s id=%s x=%d y=%d z=%d",e.name,e.id,e.x,e.y,e.z); // 输出i所指元素
i=(i+1)%MAX_QSIZE; // i指向下一个元素
}
printf("\n");
}

void main()
{
int i;
SqQueue Q;
InitQueue(Q);
point p[N];
point e;
for(i=1;i<=N;i++)
{
printf("Input the %dth point's name id x y z ",i);
scanf("%s %s %d %d %d",p[i].name,p[i].id,p[i].x,p[i].y,p[i].z);
EnQueue(Q,p[1]);
}
QueueTraverse(Q,e);

}
运行时如入数据后弹出对话框---应用程序错误,0xccccccc内存不能为written.
为什么会这样啊?怎么改?急求解……答得好加分!~
额……我也不知道是内存溢出还是什么……我是初学者……我急于知道哪里错了,怎么能改对……希望各位大侠帮帮忙~

scanf("%s %s %d %d %d",p[i].name,p[i].id,&p[i].x,&p[i].y,&p[i].z);
输入改为上句(你没对int类型变量取地址)
改过之后就不会 出内存错误了 (不崩溃了)
但是程序会陷入死循环
简单分析一下: 你的 point e 没有初始化就作为函数QueueTraverse的参数使用了
是不是这里的问题 我不太了解你写的程序的目的 所以不太清楚具体细节
一下是完整程序:

// testing2.cpp : 定义控制台应用程序的入口点。
//

#include<string.h> // 字符串函数头文件
#include<ctype.h> // 字符函数头文件
#include<malloc.h> // malloc()等
#include<limits.h> // INT_MAX等
#include<stdio.h> // 标准输入输出头文件
#include<stdlib.h> // atoi(),exit()
#include<io.h> // eof()
#include<math.h> // 数学函数头文件
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码
#define MAX_QSIZE 100 // 最大队列长度+1
#define N 2
typedef int QElemType;
typedef struct
{
char name[20];
char id[20];
int x;
int y;
int z;
}point;
typedef struct
{ point* base; // 初始化的动态分配存储空间
int front; // 头指针,若队列不空,指向队列头元素
int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;

void InitQueue(SqQueue &Q)
{ // 构造一个空队列Q
Q.base=(point *)malloc(MAX_QSIZE *sizeof(point));
if(!Q.base) // 存储分配失败
exit(OVERFLOW);
Q.front=Q.rear=0;
}

Status QueueEmpty(SqQueue Q)
{ // 若队列Q为空队列,则返回TRUE;否则返回FALSE
if(Q.front==Q.rear) // 队列空的标志
return TRUE;
else
return FALSE;
}

Status EnQueue(SqQueue &Q,point e)
{ // 插入元素e为队列Q的新的队尾元素
if((Q.rear+1)%MAX_QSIZE==Q.front) // 队列满
return ERROR;
Q.base[Q.rear]=e; // 将e插在队尾
Q.rear=(Q.rear+1)%MAX_QSIZE; // 队尾指针+1后对MAX_QSIZE取余
return OK;
}

int QueueLength(SqQueue Q)
{ // 返回队列Q的元素个数,即队列的长度
return(Q.rear-Q.front+MAX_QSIZE)%MAX_QSIZE;
}

int DeQueue(SqQueue &Q,point &e)
{ // 若队列Q不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
if(Q.front==Q.rear) // 队列空
return ERROR;
e=Q.base[Q.front]; // 将队头元素的值赋给e
Q.front=(Q.front+1)%MAX_QSIZE; // 移动队头指针
return OK;
}

void QueueTraverse(SqQueue Q)
{ // 从队头到队尾依次对队列Q中每个元素输出
int i=Q.front; // i最初指向队头元素
point e=Q.base[i];//把i指向的元素赋给e
while(i!=Q.rear) // i指向队列Q中的元素
{
printf("name:%s id=%s x=%d y=%d z=%d\n",e.name,e.id,e.x,e.y,e.z); // 输出i所指元素
i=(i+1)%MAX_QSIZE; // i指向下一个元素
}
printf("\n");
}
int main()
{
int i;
SqQueue Q;
InitQueue(Q);
point p[N];
for(i=1;i<=N;i++)
{
printf("Input the %dth point's name id x y z ",i);
scanf("%s %s %d %d %d",p[i].name,p[i].id,&p[i].x,&p[i].y,&p[i].z);
EnQueue(Q,p[1]);
}
QueueTraverse(Q);
system("pause");
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-11-14
改了三个地方:用//!!!!!!标注的地方:

#include<string.h> // 字符串函数头文件
#include<ctype.h> // 字符函数头文件
#include<malloc.h> // malloc()等
#include<limits.h> // INT_MAX等
#include<stdio.h> // 标准输入输出头文件
#include<stdlib.h> // atoi(),exit()
#include<io.h> // eof()
#include<math.h> // 数学函数头文件
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码
#define MAX_QSIZE 100 // 最大队列长度+1
#define N 10
typedef int QElemType;

typedef struct
{
char name[20];
char id[20];
int x;
int y;
int z;
}point;

typedef struct
{
point* base; // 初始化的动态分配存储空间
int front; // 头指针,若队列不空,指向队列头元素
int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;

void InitQueue(SqQueue &Q)
{
// 构造一个空队列Q
Q.base=(point *)malloc(MAX_QSIZE *sizeof(point));
if (!Q.base) // 存储分配失败
exit(OVERFLOW);
Q.front=Q.rear=0;
}

Status QueueEmpty(SqQueue Q)
{
// 若队列Q为空队列,则返回TRUE;否则返回FALSE
if (Q.front==Q.rear) // 队列空的标志
return TRUE;
else
return FALSE;
}

Status EnQueue(SqQueue &Q,point e)
{
// 插入元素e为队列Q的新的队尾元素
if ((Q.rear+1)%MAX_QSIZE==Q.front) // 队列满
return ERROR;
Q.base[Q.rear]=e; // 将e插在队尾
Q.rear=(Q.rear+1)%MAX_QSIZE; // 队尾指针+1后对MAX_QSIZE取余
return OK;
}

int QueueLength(SqQueue Q)
{
// 返回队列Q的元素个数,即队列的长度
return(Q.rear-Q.front+MAX_QSIZE)%MAX_QSIZE;
}

Status DeQueue(SqQueue &Q,point &e)
{
// 若队列Q不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
if (Q.front==Q.rear) // 队列空
return ERROR;
e=Q.base[Q.front]; // 将队头元素的值赋给e
Q.front=(Q.front+1)%MAX_QSIZE; // 移动队头指针
return OK;
}

void QueueTraverse(SqQueue Q,point &e) //!!!!!!!!这里将"point e"改为引用:"point &e"
{
// 从队头到队尾依次对队列Q中每个元素输出
int i=Q.front; // i最初指向队头元素
while (i!=Q.rear) // i指向队列Q中的元素
{
e=Q.base[i];//把i指向的元素赋给e //!!!!!!!!这句移到循环里面来
printf("name:%s id=%s x=%d y=%d z=%d\n", e.name, e.id, e.x, e.y, e.z); // 输出i所指元素
i=(i+1)%MAX_QSIZE; // i指向下一个元素
}
printf("\n");
}

int main()
{
int i;
SqQueue Q;
InitQueue(Q);
point p[N];
point e;
for (i=0;i<N;i++)
{
printf("Input the %dth point's name id x y z ",i);
scanf("%s %s %d %d %d", p[i].name, p[i].id, &p[i].x, &p[i].y, &p[i].z); //!!!!!!!这里读入整型变量必须加取地址符
EnQueue(Q,p[i]);
}
QueueTraverse(Q,e);

return 0;
}
第2个回答  2010-11-14
这个知道的可能都不会看这里了
第3个回答  2010-11-14
是内存溢出吗?
相似回答