Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode)) 请帮我每一个字都仔细翻译一下好么 十分感谢

如题所述

这是C吗
这段代码 Q是个结构体 指的是队列
front是队列前端 rear是队列末端
Queueptr 是 queue pointer 队列指针
malloc:memory allocate 内存分配
sizeof:尺寸
QNode:队列节点
这段代码是在初始化队列 但是不应该这么写
让队列Q的前段和末端指向同一个节点
并给这个节点申请一个单位节点的内存
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-21
#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<<
我可以帮助你,你先设置我最佳答案后,我百度Hii教你。你的串号我已经记下,采纳后我会帮你制作本回答被提问者和网友采纳
第2个回答  2010-11-28
QueuePtr Q.front=new QNode;//问题所在

改为

Q.front=new QNode;
相似回答