First off, the code shows that class Event is self-contained. Which is to say that head and nxt are part of the object itself. If you wish to use a Linked List of objects linking to each other but not maintaining the head outside then I would do the following...
// event.cpp
event::event() {
head = NULL;
}
event::event(int date, string name): date(date), name(name) {
head = NULL;
}
event::event(event *prev, int date, string name): date(date), name(name) {
if (prev->head != NULL) {
this->head = prev->head;
} else {
prev->head = this->head = prev;
}
prev->nxt = this;
this->nxt = NULL;
}
An example of using this would be as follows:
event *tailEvent = new event(1, 'first');
event *nextEvent = new event(tailEvent, 2, 'second');
event *thirdEvent = new event(nextEvent, 3, 'third');
...
tailEvent = lastEvent;
and so on and so forth. So, tailEvent->head will always point to the first created event and the tailEvent->nxt will follow in the list.
BUT... This is highly prone to errors, so I would recommend keeping the list itself outside, if possible using STL. See Learning C++: A sample linked list for an example.
EDIT:
Better approach:-
class Event {
private:
Event *next;
int date;
string name;
public:
Event() {};
Event(int date, string name) : date(date), name(name) {};
setNext(Event *next) { this->next = next; };
int date() { return date; };
string name() { return name; };
Event *next() { return next; };
};
class EventList {
private:
Event *head;
public:
EventList() { head = NULL };
void add(int date, string name);
Event *head() { return head; }
}
void EventList::add(int date, string name) {
Event *newEvent = new Event(date, name);
newEvent->setNext(NULL);
Event *tmp = head;
if (tmp != NULL) {
while (tmp->next() != NULL) tmp = tmp->next();
tmp->setNext(newEvent);
} else {
head = newEvent;
}
}