Thread: Using linked list templates

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

    Using linked list templates

    I am sorry about this but this really confuses me for some reason. I am just trying to do statement coverage for a header file that I am including in my cpp file. This header file contains various functions used to do different things to the linked list. I am really stuck on this and I'm sure it's a simple thing. Here is the header file that contains all the functions I need to use for my statement coverage:

    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();
    
      void addToFront (const Data& value);
      void addToEnd (const Data& value);
    
      // Add value after the indicated position
      void addAfter (LListNode<Data>* afterThis, const Data& value);
      
      // Add value before the indicated position
      void addBefore (LListNode<Data>* beforeThis, const Data& value);
    
      // Add value in sorted order.
      //Pre: all existing values are already ordered
      void addInOrder (const Data& value);
    
      // Remove value at the indicated position
      void remove (LListNode<Data>* here);
      
      // Add value after the indicated position
      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;
    
    
      // Empty the list
      void clear();
    
      // Append another list onto the end of this one
      void append (const LListHeader<Data>& list);
    };

    Also, here is my attempt at making a cpp file run all the functions:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <fstream>
    #include <cstdlib>
    
    #include "sflistUtils.h"
    
    using namespace std;
    
    
    
    // Unit test driver for Singly Linked Lists
    
    
    
    
    
    int main(int argc, char** argv)
    {
    
      string line;
      getline(cin, line);
      while(cin)
      {
          istringstream in (line);
          cout << line << endl;
          string toSearchFor;
          in >> toSearchFor;
          string LListNode(line);
          addToFront(toSearchFor);
          getline(cin, line);
      }
    
      return 0;
    }
    I know I just attempted at addToFront but I can't get it to work!
    Thanks for the help guys!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    string LListNode(line);
    I think perhaps you wanted
    Code:
    LListNode<string> node(line);
    That's the syntax for declaring an instance of a templated class.

    You'll also need to create an LListHeader object in much the same way so that you can call the addToFront() method of this class.

    Remember, if you want to call a method of a class:
    Code:
    Class object;
    object.method();
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Dont you need to declare a LListHeader class?
    Initialize it and call the addToFront?
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    I'm sorry but I just don't get it. I can't seem to call ANY function because I apparently have no idea what I'm doing. I don't know why this is so confusing for me when I can easily build a linked list without templates. If anyone could just do ONE example for me or just explain it a little clearer I would greatly appreciate it.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    All the function bodies for the different things done to the linked list are in the header file. I don't need to write the function bodies. I just need to use these functions! This is very frustrating!

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your code how you create class instances and how you call member functions is all wrong. The usage is not really different from non-templated classes (except the instance declaration takes the template arguments in <>).

    What is different is that templated code must be implemented in the header file as the implementations need to be available to the compiler while it is compiling any file that includes the header (your main function). You can't separate template classes between a .h and .cpp file.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    How does this look:

    Code:
    int main(int argc, char** argv)
    {
        ifstream in;
        int num1, num2, num3, num4;
        in.open("sfltest.dat");
    
        LListNode<int> next;
        LListHeader<int> list;
    
        if(!in)
        {
            cerr << "Please put sfltest.dat in directory of executable" << endl;
        }
    
        in >> num1 >> num2 >> num3 >> num4;
        while(in)
        {
        list.addToFront(num1);
        list.addToEnd(num2);
        in >> num1 >> num2 >> num3 >> num4;
        }
    
    
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM