#ifndef _ELEMENT_H_
#define _ELEMENT_H_
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
class element : public entity {
private:
protected:
entity * ent; // pointer to entity
element * right; // pointer to next element
public:
element();
element(entity * ENT);
virtual ~element();
element * get_right(); // get the "right" (next) element of this object
entity * get_ent(); // get object that this element holds
void set_right(element * el); // link this element to another element
Bool equal(entity * ENT); // are the given object and the held object
equal?
Bool equal(void * ENT);
char * get_name(); // returns name of the object
void print(); //print the name
};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
#endif
By applying the set_right(element * el) function of class element properly, a linked-list can be constructed easily. For example here is an example main program:
int main() {
// create 4 new elements for the linked-list
element * elem1 = new element;
element * elem2 = new element;
element * elem3 = new element;
element * elem4 = new element;
// link all of the elements together
elem3->set_right(elem4); // right side of elem3 is elem4
elem2->set_right(elem3); // right side of elem2 is elem3
elem1->set_right(elem2); // right side of elem1 is elem2
return 0;
}