G
Georg Madler
Hi, I've got the following newbie-problem:
I try to create a linked list existing out of 5 elements for example.
To later process the list I try to create a pointer pointing on the
first element in the list. But as soon as I add a new element to the
list this pointer changes to the new element, too. How do I store a
pointer pointing on the first element of the list? Thanks...
class Point {
public:
Point New();
void SetCounter(int count);
private:
Point *next;
int counter;
};
Point Point::New() {
this->next = new Point;
return *this->next;
}
void Point::SetCounter(int count) {
this->counter = count;
}
int main()
{
Point *point = new Point;
Point *start;
//Save pointer on the first element of the list
start = point;
for(int i=1;i<=5;i++) {
point->SetCounter(0);
*point = point->New();
}
return EXIT_SUCCESS;
}
I try to create a linked list existing out of 5 elements for example.
To later process the list I try to create a pointer pointing on the
first element in the list. But as soon as I add a new element to the
list this pointer changes to the new element, too. How do I store a
pointer pointing on the first element of the list? Thanks...
class Point {
public:
Point New();
void SetCounter(int count);
private:
Point *next;
int counter;
};
Point Point::New() {
this->next = new Point;
return *this->next;
}
void Point::SetCounter(int count) {
this->counter = count;
}
int main()
{
Point *point = new Point;
Point *start;
//Save pointer on the first element of the list
start = point;
for(int i=1;i<=5;i++) {
point->SetCounter(0);
*point = point->New();
}
return EXIT_SUCCESS;
}