How would I pass a pointer to my object?

I have a struct defined as private in a class list and I want to pass a pointer to one of my structs to one of my functions. This is my struct in my LinkedList class:

1
2
3
4
5
6
7
8
9
10
11
12
13
class LinkedList{

private:   
struct ListNode
   {
      /** A data item on the list. */
      String item;
      /** Pointer to next node. */
      ListNode    *next;
      ListNode    *prev;
   }; // end ListNode

};


I want to (in a different class) create a function that takes in a string to be added and a pointer to the first node in my list. Something like this:
1
2
void insert(const string myString, POINTER TO FIRST NODE IN LIST){	
}


What do I put in that second part of the parameter so that I can have a pointer to the "head" of my list?
Last edited on
If you had a function in LinkedList that returned a pointer to ListNode you could have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class ListNode // unless you absolutely must define ListNode within LinkedList I don't see the benefit
{
  string Item;
  ListNode *next;
  ListNode *prev;
};

class LinkedList
{
  private:
    ListNode **nodeList;
    ListNode *listHead;
    ListNode *listTail;
  public:
    ListNode *Head() { return listHead; }
    ListNode *Tail() { return listTail; }
};


Then in the other class:
 
void insert(const string myString, ListNode *listHead);


And wherever you use it in the code:
1
2
3
LinkedList myLL;
...
insert("Foo",myLL.Head());


But since insert takes a ListNode pointer it could really be applied to any node as long as you had a function in LinkedList to expose it.
Topic archived. No new replies allowed.