Thread: Linked List: Static variable and retieving single value?

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Linked List: Static variable and retieving single value?

    I seemed to have lost my last post?

    I am having trouble sending one value at a time to the driver program. I tried using a static variable to keep track of the current list pointer but it will not compile. Is there an easy way to read the value of a linked list ONE at a time and remember the location to get the next one?

    How can I use a get function to accomplish the retrieval of one value?

    Error from static variable declared below:
    syntax error: miswsing ';' before *
    ListNode missing storage-class or type specifiers
    T undeclared identifier
    LinkedList, use of class template requires template argument list
    static data member cannot be initialized via derived class

    Code:
    template <class T>
    
    class LinkedList  {
    
    public:
    
         LinkedList() (head = NULL;)
         :
         :
    
    private:
    
         struct ListNode {
               T value;
               struct ListNode *next;
          }
    
          ListNode *head;
    
          static ListNode *location;
    
    };  // end class LinkedList
    
    template <class T>
    ListNode *LinkedList<T>::location = head;  // does not work

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    And if you have two lists, what then? Maybe something more like this:
    Code:
    class LinkedList {
    private:
      struct ListNode {
        T value;
        struct ListNode *next;
      }
    public:
      typedef ListNode *iterator;
    public:
      ...
      iterator begin() { return head; }
      iterator end() { return NULL; }
    private:
      ListNode *head;
    }
    Code:
    int main()
    {
      LinkedList list;
      ...
      LinkedList::iterator location = list.begin();
      while ( location != list.end() ) {
        ...
        location = location->next;
      }
    }
    My best code is written with the delete key.

  3. #3
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    location?

    So, how do I incorporate "location" into this?

    Do I leave it defined as static?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So, how do I incorporate "location" into this?
    Have the client declare it.

    >Do I leave it defined as static?
    No, you remove it completely. Having a current location variable as a member of a linked list class isn't the best design to begin with. Making it static only complicates matters because a static member exists across all objects of the class. So if you have two lists then you still have one location. Imagine the confusion that would be caused by that.

    The solution is to provide a location type instead of a variable, that lets the client code declare their own location variable. This way you give the client more flexibility. Now instead of being forced to use one location, they can have several independent locations pointing into the list, as well as multiple lists existing simultaneously.
    My best code is written with the delete key.

  5. #5
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    It Seems to Work.

    It spit out the numbers in main(). I had to make it all public because I could not figure out how to I did it any other way but this:

    If this works, let me know if there is something bad about this implementation. I am assuming I can just assign the returned variable to a "T variable" (template).

    thanks.

    Code:
    public:
         typedef ListNode *iterator;
         ListNode *location;
    
         iterator begin() {return head;}
         iterator end()  {return NULL;}
    
    // Driver code:
    
    list->location = list->begin();
    
    while (list->location != list->end() )  {
         cout << "Test: " << list->location->value << endl;
         list->location = list->location->next;
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If this works
    It should.

    >let me know if there is something bad about this implementation
    I already did. Having an "official" iterator embedded in every object is a bad design for a container class.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed