Thread: How do I call these functions?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    122

    How do I call these functions?

    I am having trouble calling these functions in my cpp file. I have created the linked list and I just need to cover each of these functions. I have deleted a few of them since I already got them working. I am having trouble understanding what
    Code:
    LListNode<Data>*
    portion of the functions parameter is. I just don't know what to put in there. I am sure it is really simple too. Also, I am not sure what the
    Code:
    struct LListNode
    does. If anyone could help I would be extremely thankful.

    Code:
    template <typename Data>
    struct LListNode
    {
      Data data;
      LListNode<Data>* next;
    
      LListNode() {next = 0;}
      LListNode (const Data& d, LListNode<Data>* nxt = 0)
        : data(d), next(nxt)
      {}
    };
    
    
    template <typename Data>
    struct LListHeader {
    
      LListNode<Data>* first;
    
      LListHeader();
    
      // Add value after the indicated position
      //pre: afterThis != NULL
      void addAfter (LListNode<Data>* afterThis, const Data& value);
    
      // Add value before the indicated position
      //pre: beforeThis != NULL
      void addBefore (LListNode<Data>* beforeThis, const Data& value);
    
      // Remove value at the indicated position
      //pre: here != NULL
      void remove (LListNode<Data>* here);
    
      // Add value after the indicated position
      //pre: afterThis != NULL
      void removeAfter (LListNode<Data>* afterThis);
    
      // Search for a value. Returns null if not found
      LListNode<Data>* find (const Data& value) const;
    
      // Search an ordered list for a value. Returns null if not found
      LListNode<Data>* findOrdered (const Data& value) const;
    
      // Append another list onto the end of this one
      void append (const LListHeader<Data>& list);
    
    };

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    LListNode<Data>*
    can be read as a pointer to a LListNode<Data>

    struct LListNode
    means you are defining a type named LListNode. the keyword struct is the same as class, with the only difference being all members are public by default (vs private for class).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Yeah...funny thing is it says the LListNode is not a type when I try to say it is.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    LListNode, by itself, is not a type, since you have the struct definition inside a template definition. You can have a LListNode<int>, or a LListNode<double>, or ....

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Yeah I know about the whole <int> or whatever type of linked list I want but I don't know how to call those functions and actually use them and also what the node aspect is supposed to do. I am pretty sure I created a linked list with only one node which stored numerous numbers from the array I created. I am trying to create a linked list of in that is entirely self-contained. I create the arrays by
    Code:
    array[] = {1,2,3,4}
    etc. I use this array to store the data into a linked list. I am sure I need to use the node part to actually create each linked list. Could anyone help with that?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Assuming you have all those functions written, you seem to be missing "create a list", unless addAfter or addBefore can handle a 0 pointer. The preconditions say they can't. So you'll need a way to create a list -- you could make a constructor that takes a Data& to use for the head element, or you could make a constructor that takes a LListNode object (but that's no fun).

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    I can't alter the header file. This is provided by my professor and I am just needing to do statement coverage with gcov. Basically, what I have posted in the first post is what I have to work with. I can't be writing functions just the main function in the cpp file. This is what I have.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, it's not very object-oriented; but since LListHeader is a struct we can access its members. So create a LListNode<whatever> with your first data point, and create an LListHeader<whatever>. Then you can assign biglist.first = listnode (or whatever you called your actual variables). Then you can use addBefore/addAfter as appropriate.
    Last edited by tabstop; 11-26-2008 at 10:47 PM. Reason: s'tra'y a'pos'trophes'

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    This addToEnd would handle a 0 pointer wouldn't it?

    Code:
    template <typename Data>
    void LListHeader<Data>::addToEnd (const Data& value)
    {
      LListNode<Data>* newNode = new LListNode<Data>(value, NULL);
      if (first == NULL)
        {
          first = newNode;
        }
      else
        {
          // Move to last node
          LListNode<Data>* current = first;
          while (current->next != NULL)
    	current = current->next;
    
          // Link after that node
          current->next = newNode;
        }
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why yes, yes it would. It can be amazing what's hiding in the woodwork sometimes.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Hmmm still using that doesn't seem to work. I have a linked list of int with the numbers 2,3,4,6 in it. Now, when I try the addInOrder function with a 5, it just puts the 5 at the beginning. Something still isn't right. Here is what I used to build my llist:

    Code:
    int main(int argc, char** argv)
    {
        int array[] = {2,3,4,6};
        int array2[] = {6,7,8,9};
        int listLength = 10;
        LListNode<int> listNode;
        LListHeader<int> first;
    
        for(int i=0; i<listLength; ++i)
        {
            first.addToEnd(array[i]);
        }
        first.addInOrder(5);
        first.display();
        return 0;
    }
    Obviously still haven't used array2 yet but I am planning on using that to append the two linked lists. Am I doing something wrong in building the list? Because if I am, I don't see it :/

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    addToEnd should give you 2, 3, 4, 6, garbage, garbage, garbage, garbage, garbage, and garbage -- adding ten numbers to the list when you only have four elements can only lead to heartbreak. And I don't know whether addInOrder is supposed to work when the original list isn't sorted or not.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Sorry..mental mistake there. But that doesn't fix anything. And yes, the array is supposed to be ordered before we add another element to the list via addInOrder.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you fix that, and you still get 5, 2, 3, 4, 6, then addInOrder is broken.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    This is his function...so I don't know why it would be. But apparently it is. This is it

    Code:
    // Add value in sorted order.
    //Pre: all existing values are already ordered
    template <typename Data>
    void LListHeader<Data>::addInOrder (const Data& value)
    {
      if (first == NULL)
        first = new LListNode<Data>(value, NULL);
      else
        {
          LListNode<Data>* current = first;
          LListNode<Data>* prev = NULL;
          while (current != NULL && value < current->data)
    	{
    	  prev = current;
    	  current = current->next;
    	}
          // Add between prev and current
          if (prev == NULL)
    	addToFront (value);
          else
    	addAfter (prev, value);
        }
    }
    I myself don't see anything wrong but I guess there is!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 02:40 AM
  2. seg fault with any function call
    By LegoMan in forum C Programming
    Replies: 5
    Last Post: 04-15-2009, 05:30 PM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM