不加销毁函数能出结果,加上结果就不对!求高手指点
#include<iostream>using namespace std;
#include<cstdlib>
#define OK 1
#define ERROW 0
#define OVERFLOW 0
typedef struct QNode
{
int data;
struct QNode*next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue &Q)//构造一个空对
{
Q.front=(QNode*)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.rear=Q.front;
Q.front->next=NULL;
return OK;
}
int QueueEmpty(LinkQueue &Q)
{
if(Q.front==Q.rear)
return ERROW;
else
return OK;
}
int EnQueue(LinkQueue &Q,int e)//入对
{
QueuePtr p;
p=(QNode*)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->next=NULL;
p->data=e;
Q.rear->next=p;
Q.rear=p;
return OK;
}
int DeQueue(LinkQueue &Q,int &e)//出队
{
QueuePtr p; //为什么把它改为 LNode *p就对了啊 想了很长时间想不明白 或者把 DestroyQueue函数去掉也对!
p=Q.front->next;
e=p->data;
Q.front->next=p->next; //求指点!!!谢谢了!!!
if(Q.rear==p)
Q.front=Q.rear;
free(p);
return ok;
}
int DestroyQueue(LinkQueue &Q)
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
cout<<"队列已被销毁."<<endl;
return OK;
}
int main()
{
LinkQueue Q;
InitQueue(Q);
int n;
cout<<"请输入要处理数的个数:";
cin>>n;
int i=0,e;
while(i<n)
{
cout<<"请输入第"<<i+1<<"个数:";
cin>>e;
EnQueue(Q,e);
i++;
}
i=0;
while(i<n)
{
DeQueue(Q,e);
cout<<e<<" ";
i++;
}
cout<<endl;
DestroyQueue(Q);//加上它就不对了!
return 0;
}