1个回答
2015-04-15
展开全部
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int QElemType;
typedef int status;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front) exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
status DestroyQueue(LinkQueue &Q)
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return OK;
}
status EnQueue(LinkQueue &Q,QElemType e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) return OVERFLOW;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
status DeQueue(LinkQueue &Q,QElemType &e)
{
QueuePtr p;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.front==Q.rear;
free(p);
return OK;
}
status QueueLength(LinkQueue Q,QElemType &i)
{
QueuePtr p;
i=0;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
while(p!=NULL)
{
i++;
p=p->next;
}
return OK;
}
status GetHead(LinkQueue Q,QElemType &e)
{
QueuePtr p;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
e=p->data;
return OK;
}
void QueueTraverse(LinkQueue Q)
{
QueuePtr p;
int e;
p=Q.front->next;
while(p!=NULL)
{
e=p->data;
cout<<e<<
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int QElemType;
typedef int status;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front) exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
status DestroyQueue(LinkQueue &Q)
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return OK;
}
status EnQueue(LinkQueue &Q,QElemType e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) return OVERFLOW;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
status DeQueue(LinkQueue &Q,QElemType &e)
{
QueuePtr p;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.front==Q.rear;
free(p);
return OK;
}
status QueueLength(LinkQueue Q,QElemType &i)
{
QueuePtr p;
i=0;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
while(p!=NULL)
{
i++;
p=p->next;
}
return OK;
}
status GetHead(LinkQueue Q,QElemType &e)
{
QueuePtr p;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
e=p->data;
return OK;
}
void QueueTraverse(LinkQueue Q)
{
QueuePtr p;
int e;
p=Q.front->next;
while(p!=NULL)
{
e=p->data;
cout<<e<<
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询