为何递归调用析够?进来看一下吧!
#ifndef _charLink_
#define _charLink_
#include <iostream>
using namespace std;
class node{
char c;
node* next;
public:
node(){
c = ' ';
next = NULL;
}
node(char ch){
c = ch;
next = NULL;
}
node(node& n){
c = n.c;
*next = *n.next;
}
node& operator= (const node& n){
c = n.c;
*next = *n.next;
return *this;
}
~node(){
cout<< "~node()" << endl;
if(next != NULL){
delete next;
next = NULL;
}
}
void setNext(node* n){
this->next = n;
}
char getNode(){
return c;
}
//friend class cStack;
};
class cStack{
node* header;
node* tmp;
int cout;
public:
cStack();
~cStack();
void push(char);
char pop();
bool isEmpty() const;
void reset();
int getCout() const;
};
#endif
部分实现如下:
void cStack::push(char c){
tmp = new node(c);
if(tmp == NULL)
throw "Out of memery";
tmp->setNext(header);//tmp->next = header;
header = tmp;
cout++;
}
char cStack::pop(){
if(cout>0){
char lc = header->getNode();
tmp = header;
tmp->setNext(header);//header = header->next;
cout--;
delete tmp;
return lc;
}else{
throw "Stack is empty!";
}
}
主函数:
int main()
{
cStack sck;
int i = 0;
int j = 0;
try{
sck.push('a');//这里出现递归调用~node(),应该怎么改?
或者怎么实现这个程序?
cout << sck.pop() << endl;
}catch(char *c) {
cout << c << endl;
}