#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.
为什么会这样啊?怎么改?急求解……答得好加分!~
额……我也不知道是内存溢出还是什么……我是初学者……我急于知道哪里错了,怎么能改对……希望各位大侠帮帮忙~