程序可以运行,界面如下;反复检查代码似乎都没有问题,为什么出不了结果呢。。恳求大神指教!!
//链队列
#include "stdAfx.h"
#include <iostream>
using namespace std;
typedef int ElemType;
extern void Error( char * s );
enum Status{
ERROR,
OVERFLOW1,
OK
};
//链队列的类型定义
typedef struct QNode /* 结点结构 */
{
ElemType data;
struct QNode *next;
} QNode,*QueuePtr;
typedef struct /* 队列的链表结构 */
{
QueuePtr front,rear; /* 队头、队尾指针 */
} LinkQueue;
////////////////////////////////算法实现//////////////////////////////////
//1.初始化队列
Status InitQueue_L( LinkQueue &Q )
{
if( Q.front != NULL)
return OVERFLOW1;
Q.front = Q.rear ;
Q.front->next = NULL;
return OK;
}
//2. 销毁队列
Status DestroyQueue_L( LinkQueue &Q )
{
if(Q.front==Q.rear)
return ERROR;
delete Q.front ;
Q.front =Q.rear =NULL;
return OK;
}
//3. 入队:把x插入队尾
Status EnQueue_L( LinkQueue &Q , ElemType x )
{
QNode*p = new QNode;
if(p!=NULL)
return OVERFLOW1;
p->data = x;
p->next =NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
//4. 从出队: 如果队空,返回false;否则返回队头元素到x
Status DeQueue_L( LinkQueue &Q , ElemType &x )
{
QNode*p = new QNode;
if(Q.front==Q.rear)
return ERROR;
p=Q.front->next;
Q.front->next=p->next;
if(Q.front->next==NULL)
{
Q.rear=Q.front;
}
x = p->data;
delete p;
return OK;
}
int main(int argc, char* argv[])
{
//测试链队列
LinkQueue queue;
cout<<"测试链队列"<<endl;
InitQueue_L( queue );//初始化队列
for ( int i = 0 ; i < 100 ; i ++ )
{
EnQueue_L( queue , i );//入入队的顺序为0,1,2,3,...,99(自行添加出错处理)
}
cout<<"队列的测试"<<endl;
while ( DeQueue_L( queue , i ) == OK )//出队,输出出队的元素
{
cout<< i <<" ";
}
cout<<endl;
DestroyQueue_L( queue );//销毁队列
cout<<"退出main函数"<<endl;
system("pause");
return 0;
}