#include
#include
#define MAXSIZE 1024
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int QElemType;
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
void InitQueue(LinkQueue&Q)//构造一个队列
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
return;
}
void EnQueue(LinkQueue&Q,QElemType e)//插入元素e为Q的新队尾
{
QNode *p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->data=e;
p->next=NILL;
Q.rear->next=p;
Q.rear=p;
return;
}
void DeQueue(LinkQueue&Q,QElemType e)//删除队头元素
{
if(Q.front==Q.rear)return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)Q.rear=Q.front;
free(p);
return;
}
void DestroyQueue(LinkQueue&Q)//销毁队列
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return;
}
int main()
{
int e;
LinkQueue Q;
printf("请输入您的元素:\n");
scanf("%d",&e);
EnQueue(Q,e);
DestroyQueue(Q);
return 0;
}
C:\Users\86182\Desktop\队列.cpp(37) : error C2065: 'NILL' : undeclared identifier
C:\Users\86182\Desktop\队列.cpp(37) : error C2440: '=' : cannot convert from 'int' to 'struct QNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Users\86182\Desktop\队列.cpp(45) : error C2562: 'DeQueue' : 'void' function returning a value
C:\Users\86182\Desktop\队列.cpp(43) : see declaration of 'DeQueue'
C:\Users\86182\Desktop\队列.cpp(46) : error C2065: 'p' : undeclared identifier
C:\Users\86182\Desktop\队列.cpp(46) : error C2440: '=' : cannot convert from 'struct QNode *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Users\86182\Desktop\队列.cpp(47) : error C2227: left of '->data' must point to class/struct/union
C:\Users\86182\Desktop\队列.cpp(48) : error C2227: left of '->next' must point to class/struct/union
C:\Users\86182\Desktop\队列.cpp(49) : error C2446: '==' : no conversion from 'int' to 'struct QNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Users\86182\Desktop\队列.cpp(49) : error C2040: '==' : 'struct QNode *' differs in levels of indirection from 'int'
执行 cl.exe 时出错.
队列.obj - 1 error(s), 0 warning(s)
修改如下,供参考:
#include<stdio.h>
#include<stdlib.h>
#include <crtdbg.h>
#define MAXSIZE 1024
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode* next;
}QNode, * QueuePtr;
typedef struct
{
QueuePtr front;//队头指针
QueuePtr rear; //队尾指针
}LinkQueue;
void InitQueue(LinkQueue& Q)//构造一个队列
{
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q.front) exit(OVERFLOW);
Q.front->next = Q.rear->next = NULL; //修改
return;
}
void EnQueue(LinkQueue& Q, QElemType e)//插入元素e为Q的新队尾
{
QNode* p;
p = (QueuePtr)malloc(sizeof(QNode));
if (!p) exit(OVERFLOW);
p->data = e;
p->next = NULL; //NILL;
if (!Q.front->next) //修改
Q.front->next = p; //修改
else
Q.rear->next = p; //修改
Q.rear = p; //修改
return;
}
void DeQueue(LinkQueue& Q, QElemType &e)//删除队头元素 修改 QElemType e
{
if (!Q.front->next) { //Q.front->next == Q.rear 修改
e = -999;
return; // ERROR; 修改
}
QueuePtr p = Q.front->next; //修改
e = p->data;
Q.front->next = p->next;
//if (Q.rear == p) Q.rear = Q.front; //修改
free(p);
return;
}
void DestroyQueue(LinkQueue& Q)//销毁队列
{
while (Q.front)
{
Q.rear = Q.front->next;
free(Q.front);
Q.front = Q.rear;
}
return;
}
int main()
{
int e;
LinkQueue Q;
InitQueue(Q); //修改
//printf("请输入您的元素:\n"); //修改
//scanf("%d", &e); //修改
EnQueue(Q, 8);
EnQueue(Q, 10);
DeQueue(Q, e);
printf("%d\n", e);
DeQueue(Q, e);
printf("%d\n", e);
DeQueue(Q, e);
printf("%d\n", e);
DestroyQueue(Q);
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: