#include <iostream>
#include <string>
using namespace std;
class CData
{
private:
int i;
string name;
CData *next;
public:
CData():next(NULL), i(0){}
string getName(){ return this->name; }
void setName(string name){ this->name = name; }
CData *getNext(){ return this->next; }
void setNext(CData *cNextData){ this->next = cNextData; }
};
class CLink
{
private:
CData* head;
public:
CLink():head(NULL){}
~CLink();
void create();
void insert(CData data);
void print();
};
CLink::~CLink()
{//销毁链表
CData *p = this->head;
CData *q = this->head->getNext();
while(q)
{
delete p;
p = q;
q = q->getNext();
}
delete p;
}
void CLink::print()
{
CData *p = this->head->getNext();
while(p)
{
cout << p->getName() <<endl;
p = p->getNext();
}
}
void CLink::create()
{//创建链表
this->head = new CData();
}
void CLink::insert(CData data)
{
CData *dataNew = new CData;
*dataNew = data;
dataNew->setNext(this->head->getNext());
this->head->setNext(dataNew);
}
int main(int argc, char *argv[])
{
CLink link;
link.create();
CData a;
a.setName("Edgar");
link.insert(a);
a.setName("Alan");
link.insert(a);
a.setName("Eric");
link.insert(a);
a.setName("James");
link.insert(a);
link.print();
return 0;
}
#include <string>
using namespace std;
class CData
{
private:
int i;
string name;
CData *next;
public:
CData():next(NULL), i(0){}
string getName(){ return this->name; }
void setName(string name){ this->name = name; }
CData *getNext(){ return this->next; }
void setNext(CData *cNextData){ this->next = cNextData; }
};
class CLink
{
private:
CData* head;
public:
CLink():head(NULL){}
~CLink();
void create();
void insert(CData data);
void print();
};
CLink::~CLink()
{//销毁链表
CData *p = this->head;
CData *q = this->head->getNext();
while(q)
{
delete p;
p = q;
q = q->getNext();
}
delete p;
}
void CLink::print()
{
CData *p = this->head->getNext();
while(p)
{
cout << p->getName() <<endl;
p = p->getNext();
}
}
void CLink::create()
{//创建链表
this->head = new CData();
}
void CLink::insert(CData data)
{
CData *dataNew = new CData;
*dataNew = data;
dataNew->setNext(this->head->getNext());
this->head->setNext(dataNew);
}
int main(int argc, char *argv[])
{
CLink link;
link.create();
CData a;
a.setName("Edgar");
link.insert(a);
a.setName("Alan");
link.insert(a);
a.setName("Eric");
link.insert(a);
a.setName("James");
link.insert(a);
link.print();
return 0;
}