Terrenium
Terrenium

Reputation: 91

OOP, Linked list using classes

I have a class of events. Now All I have to do is to create events and store them in a linked list. but I don't know how to fix the head position. I mean if i define head to be NULL in a constructor then for every new event it would re-defined to be NULL. Thus I will have only one event in my linked list. My code is something like this:

// event.h
class event{
    private:
        Event *head;
        Event *nxt;
        int date;
        string name;

    public:
        event();
        event(int d, string n);
        Add_item();
}

//event.cpp
event::event(){}
event::event(int date, string name): date(date), name(name){
head=NULL;

// Now each time I call a constructor, head would be re-defined as NULL. What should I do???

Upvotes: 1

Views: 7642

Answers (3)

Anand
Anand

Reputation: 541

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;
    }
}

Upvotes: 2

Jonathan Wood
Jonathan Wood

Reputation: 67193

A better approach is to have two classes here: One to store each event and another to manage and store information about the list.

There will be many event classes in your list. There will only be one list class.

Now, your event class will in fact need some helper methods such as next and previous pointers. You can implement them directly or create a third class that is either inherited from or have it contain the event class.

But either way, the class that manages the list should only require one instance. And in that instance the head can be initialized to null, and it will be updated as needed.

Upvotes: 1

DemonShi
DemonShi

Reputation: 109

You need some controlling code for your list. List items cannot control themself.

I would recommend you use a container std::list which implements all list operations.

If you want to implement your list, you should create controlling class, eg EventContainer and implement insert/delete and search operations.

Upvotes: 1

Related Questions