主题:[讨论]求一C++入门问题
class Queue{
struct Node{
int elem;
Node * next;
};
public:
void Init();
void EnQueue(int);
int DelQueue();
int GetLength(){return length;}
void Print();
private:
Node * elemHead,*elemTail;
int length;
Node * creatNode (int );
}
#include<iostream.h>
void Queue::Init(){
elemHead=NULL;
elemTail=NULL;
length=0;
}
Queue::Node *Queue::creatNode(int val){
Node *pNew=new Node;
if(pNew=NULL){
cout<<"memory insufficient!";
return NULL;
}
else{
pNew->elem=val;
pNew->next=NULL;
return pNew;
}
}
void Queue::EnQueue(int newElem){
Node *pNew=creatNode(newElem);
if(pNew==NULL)
return ;
else{
if(elemTail==NULL)
elemTail=elemHead=pNew;
else{
elemTail->next=pNew;
elemTail=pNew;
}
}
length++;
}
int Queue::DelQueue(){
int ret=elemHead->elem;
Node *pDel=elemHead;
elemHead=elemHead->next;
delete pDel;
length--;
return ret;
}
void Queue::Print(){
Node* pNode=elemHead;
cout<<"Queue:";
while(pNode!=NULL){
cout<<pNode->elem<<" ";
pNode=pNode->next;
}
cout<<endl;
}
编译说是Init()有错误.哪位大虾指教一下
struct Node{
int elem;
Node * next;
};
public:
void Init();
void EnQueue(int);
int DelQueue();
int GetLength(){return length;}
void Print();
private:
Node * elemHead,*elemTail;
int length;
Node * creatNode (int );
}
#include<iostream.h>
void Queue::Init(){
elemHead=NULL;
elemTail=NULL;
length=0;
}
Queue::Node *Queue::creatNode(int val){
Node *pNew=new Node;
if(pNew=NULL){
cout<<"memory insufficient!";
return NULL;
}
else{
pNew->elem=val;
pNew->next=NULL;
return pNew;
}
}
void Queue::EnQueue(int newElem){
Node *pNew=creatNode(newElem);
if(pNew==NULL)
return ;
else{
if(elemTail==NULL)
elemTail=elemHead=pNew;
else{
elemTail->next=pNew;
elemTail=pNew;
}
}
length++;
}
int Queue::DelQueue(){
int ret=elemHead->elem;
Node *pDel=elemHead;
elemHead=elemHead->next;
delete pDel;
length--;
return ret;
}
void Queue::Print(){
Node* pNode=elemHead;
cout<<"Queue:";
while(pNode!=NULL){
cout<<pNode->elem<<" ";
pNode=pNode->next;
}
cout<<endl;
}
编译说是Init()有错误.哪位大虾指教一下