Thread: Small help on doubly linked list.

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Small help on doubly linked list.

    Sorry for the trouble, but this should be pretty trivial. Some of the code

    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 =(*/
    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?

    Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Er I managed to fix the problem. Sorry for the trouble. Couldn't find a way to delete/lock the topic though...

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't need to. Others can benefit from your problem and learn from it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    They could benefit even more if you could post the solution you found.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. WIP Borland C Doubly linked list
    By BCWarrior in forum C Programming
    Replies: 41
    Last Post: 09-25-2007, 12:06 AM
  3. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM
  4. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM