Pocetnik sam i pokusavam uciti C++ iz knjige "C++ za 21 dan" J. Liberty. Dosao sam do primjera koji objasnjava povezane liste i koji mi nije jasan. Znam da je pitanje opsirno, ali ako neko ima strpljenja da mi razjasni, na pocetnickom nivou, kako radi ovaj kod, to bi mi otklonilo dosta nejasnoca. Naime objesnjenje iz knjige mi nije sve razjasnilo. Unapred zahvalan.
[Ovu poruku je menjao tOwk dana 07.06.2003. u 03:55 GMT]
Code:
#include<iostream.h>
class Cat
{
public:
Cat():
itsAge(1)
{};
Cat(int age):
itsAge(age)
{};
~Cat() {}
int GetAge() const {return itsAge;}
private:
int itsAge;
};
class Node
{
public:
Node(Cat*);
~Node();
void SetNext(Node * node) {itsNext = node;}
Node * GetNext() const {return itsNext;}
Cat * GetCat() const {return itsCat;}
void Insert(Node *);
void Display();
private:
Cat * itsCat;
Node * itsNext;
};
Node::Node(Cat * pCat):
itsCat(pCat), itsNext(0)
{};
Node::~Node()
{
cout<<"Deleting mode...\n";
delete itsCat;
itsCat = 0;
delete itsNext;
itsNext = 0;
}
void Node::Insert(Node * newNode)
{
if(!itsNext)
itsNext = newNode;
else
{
int nextCatsAge = itsNext->GetCat()->GetAge();
int newAge = newNode->GetCat()->GetAge();
int thisNodeAge = itsCat->GetAge();
if(newAge >= thisNodeAge && newAge < nextCatsAge)
{
newNode->SetNext(itsNext);
itsNext = newNode;
}
else
itsNext->Insert(newNode);
}
}
void Node::Display()
{
if(itsCat->GetAge() > 0)
{
cout<<"My cat is ";
cout<<itsCat->GetAge()<<" yrs. old\n";
}
if(itsNext)
itsNext->Display();
}
int main()
{
Node *pNode = 0;
Cat * pCat = new Cat(0);
int age;
Node * pHead = new Node(pCat);
while(1)
{
cout<<"New Cat's age? (0 to quit): ";
cin>>age;
if(!age)
break;
pCat = new Cat(age);
pNode = new Node(pCat);
pHead->Insert(pNode);
}
pHead->Display();
delete pHead;
cout<<"Exiting...\n";
return 0;
}
#include<iostream.h>
class Cat
{
public:
Cat():
itsAge(1)
{};
Cat(int age):
itsAge(age)
{};
~Cat() {}
int GetAge() const {return itsAge;}
private:
int itsAge;
};
class Node
{
public:
Node(Cat*);
~Node();
void SetNext(Node * node) {itsNext = node;}
Node * GetNext() const {return itsNext;}
Cat * GetCat() const {return itsCat;}
void Insert(Node *);
void Display();
private:
Cat * itsCat;
Node * itsNext;
};
Node::Node(Cat * pCat):
itsCat(pCat), itsNext(0)
{};
Node::~Node()
{
cout<<"Deleting mode...\n";
delete itsCat;
itsCat = 0;
delete itsNext;
itsNext = 0;
}
void Node::Insert(Node * newNode)
{
if(!itsNext)
itsNext = newNode;
else
{
int nextCatsAge = itsNext->GetCat()->GetAge();
int newAge = newNode->GetCat()->GetAge();
int thisNodeAge = itsCat->GetAge();
if(newAge >= thisNodeAge && newAge < nextCatsAge)
{
newNode->SetNext(itsNext);
itsNext = newNode;
}
else
itsNext->Insert(newNode);
}
}
void Node::Display()
{
if(itsCat->GetAge() > 0)
{
cout<<"My cat is ";
cout<<itsCat->GetAge()<<" yrs. old\n";
}
if(itsNext)
itsNext->Display();
}
int main()
{
Node *pNode = 0;
Cat * pCat = new Cat(0);
int age;
Node * pHead = new Node(pCat);
while(1)
{
cout<<"New Cat's age? (0 to quit): ";
cin>>age;
if(!age)
break;
pCat = new Cat(age);
pNode = new Node(pCat);
pHead->Insert(pNode);
}
pHead->Display();
delete pHead;
cout<<"Exiting...\n";
return 0;
}
[Ovu poruku je menjao tOwk dana 07.06.2003. u 03:55 GMT]