能不能帮我分析一个题目c++的题目
题目是这样的药c++足一道队列的题目,我知道我编写的有问题,但是我不晓得怎样改,希望你能帮我分析下,怎样该法源代码:
#include"iostream.h"
typedef char Elemtype;
typedef struct qnode
{
Elemtype data;
struct qnode *next;
}QNode;
typedef struct
{
QNode *front;
QNode *rear;
}LiQueue;
class Queue
{
public:
void InitQueue();//初始化单链表;
void clearQueue();//销毁单链表;
void enQueue(int n);//向队列添加值为n的元素(注意添加的位置为队尾);
void deQueue();//删除队列中的一个元素(注意删除元素的位置为队头);
void print();//将队列中的元素从队头开始全部输出;
void getcount(int n);//查询队列中某个位置的元素的值;
int QueueEmpty();//判断队列是否为空;
private:
QNode *front;
QNode *rear;
};
void Queue::InitQueue()
{
q=(QNode *)malloc(sizeof(QNode));
front=rear=NULL;
}
void Queue::clearQueue()
{
QNode *p=front,*r;
if(p!=NULL)
{
r=p->next;
while(r!=NULL)
{
delete [p]
p=r;
r=p->next;
}
delete [q]
}
int Queue::QueueEmpty()
{
if(rear==NULL)
return 1;
else
return 0;
}
void Queue::enQueue( int n)
{
QNode *s;
s=(QNode *)malloc(sizeof(QNode));
s->data=n;
s=s->next;
s->next=NULL;
if(front==NULL)
front=rear=s;
else
{
rear->next=s;
rear=s;
}
}
void Queue::deQueue()
{
QNode *t;
if(rear==NULL)
cout<<"队列没有元素 !"<<endl;
t=front;
if(front==rear)
front=rear=NULL;
else
front=front->next;
e=t->data;
delete [t];
cout<<"删除成功 !"<<endl;
}
void Queue::getcount(int i)
{
int j=0;
Queue *p=q;
if(rear==NULL)
cout<<"没有元素可查 !"<<endl;
while(j<i-1&&q!=NULL)
{
j++;
p=p->next;
}
if(rear==NULL)
return 0;
else
{
e=p->data;
return e;
}
}
void Queue::print()
{
cout<<"输出存储的元素: "<<endl;
if(rear==NULL)
cout<<"队列没有元素 !"<<endl;
else{
while(rear->next!=NULL)
{
cout<<front<<" "<<endl;
front=front->next;
}
}
void menu()
{
system("cls")
cout<<"\t 1、输入元素 "<<endl;
cout<<"\t 2、输出元素 "<<endl;
cout<<"\t 3、删除元素 "<<endl;
cout<<"\t 4、查询元素 "<<endl;
cout<<"\t 5、判断队列是否为空 "<<endl;
cout<<"\t 6、销毁队列 "<<endl;
}
void main()
{
Queue q;
cout<<"初始化队列"<<endl;
menu();
int x;
cin>>x;
switch(x)
{
case 1:{
cout<<"向队列输入元素:"<<endl;
int n;
cin>>n;
q.enQueue(int n);
};break;
case 2:{q.print()};break;
case 3:{q.deQueue()};break;
case 4:{
cout<<"查询第几个元素: "<<endl;
int i;
cin>>i;
cout<<q.getcount(int i)
};break;
case 5:{(q.QueueEmpty()==0)?"是":"否"};break;
case 6:{q.clearQueue()};break;
default:break;
}
}
有很多错误!但我不晓得怎样修改,希望帮忙哈,谢谢!
补充:--------------------Configuration: z zcz - Win32 Debug--------------------
Compiling...
z zcz.cpp
e:\homework\程序练习\z zcz.cpp(30) : error C2065: 'q' : undeclared identifier
e:\homework\程序练习\z zcz.cpp(30) : error C2065: 'malloc' : undeclared identifier
e:\homework\程序练习\z zcz.cpp(30) : error C2440: '=' : cannot convert from 'struct qnode *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
e:\homework\程序练习\z zcz.cpp(43) : error C2120: 'void' illegal with all types
e:\homework\程序练习\z zcz.cpp(43) : error C2106: '=' : left operand must be l-value
e:\homework\程序练习\z zcz.cpp(47) : error C2059: syntax error : '}'
e:\homework\程序练习\z zcz.cpp(50) : error C2601: 'QueueEmpty' : local function definitions are illegal
e:\homework\程序练习\z zcz.cpp(58) : error C2601: 'enQueue' : local function definitions are illegal
e:\homework\程序练习\z zcz.cpp(75) : error C2601: 'deQueue' : local function definitions are illegal
e:\homework\程序练习\z zcz.cpp(90) : error C2601: 'getcount' : local function definitions are illegal
e:\homework\程序练习\z zcz.cpp(109) : error C2601: 'print' : local function definitions are illegal
e:\homework\程序练习\z zcz.cpp(166) : fatal error C1075: end of file found before the left brace '{' at 'e:\homework\程序练习\z zcz.cpp(109)' was matched
Error executing cl.exe.z zcz.obj - 12 error(s), 0 warning(s)
追问:什么 这个程序就是我写的 我记在学c++case 2:{q.print()};break;
检举
这有什么问题