Thread: Better to add data or new node as parameter

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    Better to add data or new node as parameter

    I'm learning graphs and I am building a digraph via adjacent list, so I'm going to use an array of ptrs, so each index in array stores head ptr to a linked list of adjacente vertices where first node is the starting vertex to which all nodes in this indice are adjacent to.

    Is it better to store pass the data and create new node inside function or better to create new node outside function and then pass that as parameter to my insert_to_end function:

    Code:
    void insert_at_end(int vertex)
    {
      LL newNode = new LL;
      newNode.vertex = vertex;
      newNode.next = this.array[this.size - 1];//point to whatever head in this index points to
      this.array[this.size - 1] = newNode;
    }
    
    /**vs*/
    
    void insert_at_end(LL newNode)//assuming it's created earlier
    {
      newNode.next = this.array[this.size - 1];//point to whatever head in this index points to
      this.array[this.size - 1] = newNode;
    }
    Does it really matter in terms of good design?
    Last edited by monkey_c_monkey; 10-01-2012 at 12:13 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you take your cues from std::list, then you shouldn't let users of your list class touch the nodes of the list.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The most robust interface is one that allow its users to do the least stupid things, such as passing in nodes that are just pointers to stack-based variables.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-01-2012, 02:38 PM
  2. Linked List Node Deletion based on Data
    By gvkalra in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2008, 08:15 AM
  3. trying to set data for a node class, not working?
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2005, 09:56 AM
  4. typecasting data for parameter passing
    By daluu in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 02:58 PM
  5. Plz help with placing node in a data structure
    By slopeski0007 in forum C Programming
    Replies: 5
    Last Post: 01-31-2003, 12:12 PM