这是我之前在新浪博客上写的一篇文章,感觉思想挺简单的,实现起来确实没有想象的那么简单~~~
/////////////////////////////////////////////////////////////////
CPPObject.h:
#ifndef CPPOBJECT_H
#define CPPOBJECT_H
#include
using namespace std;
class CPPObject
{
public:
CPPObject()
{
m_oSuccessor = NULL;
}
virtual void print()
{
;
}
CPPObject* GetSuccessor() const
{
return m_oSuccessor;
}
void SetSuccessor(CPPObject* obj)
{
m_oSuccessor = obj;
}
private:
CPPObject* m_oSuccessor ;
};
#endif
/////////////////////////////////////////////////////////////////
CPPNode.h:
#ifndef CPPNODE_H
#define CPPNODE_H
#include "CPPObject.h"
class CPPNode:public CPPObject
{
public:
CPPNode(char e);
void print();
private:
char m_cE;
};
#endif
/////////////////////////////////////////////////////////////////
CPPNode.cpp:
#include "CPPNode.h"
CPPNode::CPPNode(char e):CPPObject()
{
m_cE = e;
}
void CPPNode::print()
{
cout << m_cE << endl;
}
/////////////////////////////////////////////////////////////////
#ifndef CPPLIST_H
#define CPPLIST_H
#include "CPPObject.h"
class CPPList:public CPPObject
{
public:
CPPList();
void print();
void AddObj(CPPObject* tobj);
private:
CPPObject* obj;
};
#endif
/////////////////////////////////////////////////////////////////
CPPList.cpp:
#ifndef CPPLIST_H
#define CPPLIST_H
#include "CPPObject.h"
class CPPList:public CPPObject
{
public:
CPPList();
void print();
void AddObj(CPPObject* tobj);
private:
CPPObject* obj;
};
#endif
/////////////////////////////////////////////////////////////////
Manager.cpp:
#include "CPPList.h"
#include "CPPNode.h"
#include
#include
using namespace std;
int main(int argc, char* argv[])
{
CPPNode n1('a');
CPPNode n2('b');
CPPNode n3('c');
CPPNode n4('d');
CPPNode n5('e');
CPPNode n6('f');
CPPList l1;
CPPList l2;
CPPList l3;
l1.AddObj(&n2);
l1.AddObj(&n3);
l2.AddObj(&l1);
l2.AddObj(&n4);
l2.AddObj(&n5);
l2.AddObj(&n6);
l3.AddObj(&n1);
l3.AddObj(&l2);
l3.print();
/////////////////////////////////////////////////////////////////
CPPObject.h:
#ifndef CPPOBJECT_H
#define CPPOBJECT_H
#include
using namespace std;
class CPPObject
{
public:
CPPObject()
{
m_oSuccessor = NULL;
}
virtual void print()
{
;
}
CPPObject* GetSuccessor() const
{
return m_oSuccessor;
}
void SetSuccessor(CPPObject* obj)
{
m_oSuccessor = obj;
}
private:
CPPObject* m_oSuccessor ;
};
#endif
/////////////////////////////////////////////////////////////////
CPPNode.h:
#ifndef CPPNODE_H
#define CPPNODE_H
#include "CPPObject.h"
class CPPNode:public CPPObject
{
public:
CPPNode(char e);
void print();
private:
char m_cE;
};
#endif
/////////////////////////////////////////////////////////////////
CPPNode.cpp:
#include "CPPNode.h"
CPPNode::CPPNode(char e):CPPObject()
{
m_cE = e;
}
void CPPNode::print()
{
cout << m_cE << endl;
}
/////////////////////////////////////////////////////////////////
#ifndef CPPLIST_H
#define CPPLIST_H
#include "CPPObject.h"
class CPPList:public CPPObject
{
public:
CPPList();
void print();
void AddObj(CPPObject* tobj);
private:
CPPObject* obj;
};
#endif
/////////////////////////////////////////////////////////////////
CPPList.cpp:
#ifndef CPPLIST_H
#define CPPLIST_H
#include "CPPObject.h"
class CPPList:public CPPObject
{
public:
CPPList();
void print();
void AddObj(CPPObject* tobj);
private:
CPPObject* obj;
};
#endif
/////////////////////////////////////////////////////////////////
Manager.cpp:
#include "CPPList.h"
#include "CPPNode.h"
#include
#include
using namespace std;
int main(int argc, char* argv[])
{
CPPNode n1('a');
CPPNode n2('b');
CPPNode n3('c');
CPPNode n4('d');
CPPNode n5('e');
CPPNode n6('f');
CPPList l1;
CPPList l2;
CPPList l3;
l1.AddObj(&n2);
l1.AddObj(&n3);
l2.AddObj(&l1);
l2.AddObj(&n4);
l2.AddObj(&n5);
l2.AddObj(&n6);
l3.AddObj(&n1);
l3.AddObj(&l2);
l3.print();
system("pause");
return 0;
}