Sorry for the trouble, but this should be pretty trivial. Some of the code
For some reason, the list is still empty after the first head. It's probably because the newly made list is a local variable when it's made and when it's returned the local variable is destroyed?Code:list.h: namespace List { struct list_t { std::string name; list_t* prev; list_t* next; }; list_t* create(std::string name); list_t* add(list_t* ll, std::string name); } list.c: List::list_t* List::create(std::string name) { List::head = new List::list_t; head->prev = NULL; head->next = NULL; head->name = name; return List::head; } List::list_t* List::add(List::list_t* list, std::string name) { if(list == NULL) { List::list_t* new_list = List::create(name); return new_list; } list->next = List::add(list->next, name); return list; } main.c List = List::add(NULL, "CPlusPlus"); List = List::add(List, "C"); cout << list->name /*works*/ cout << list->next->name /*doesn't work, program crashes here =(*/
Thanks.



LinkBack URL
About LinkBacks




CornedBee