Results 1 to 2 of 2

Thread: Stu, more ?'s cometh

  1. #1

    Stu, more ?'s cometh

    Two questions for ya
    1. This is a ? that I don't know
    Say I wanted to make this class a class template with a type parameter of DataType. How could I do this.

    class LinkedList
    class Node
    //members of Node
    //members of linked list

    And yes, this is for learning linked lists.

    And 2.
    What if I wanted to write a function to merges one linked listed object variable into another linked list obj var. keeping the items in order in the resulting list. The client prog would use list1.mergelists(list2); to merge them. How would I do this, or start to do this? Any ideas? I am only interested in the function to do this one operation.

    Thanks a bunch again stu.
    "Good...Bad...I'm the guy with the gun"
    -- Army of Darkness --

    In case I forget, thanks STU!!

  2. #2
    Regular Member
    Join Date
    Aug 1999
    First question, something like the example that follows would work:

    template<class DataType>
    class LinkedList {
      class Node {
        DataType the_data;
        // Other stuff...
      Node _ll_node;  // Private node variable, for demo purposes; assume this is the current node
      //  Other stuff...
      // Access member function/method; returns current node value
      DataType get_value(){
        return _ll_node.the_data;
      }  // End get_value()
      // Other stuff...
    The second question is tough. Because there exists more than one type of linked list. I will assume you just want to insert the data of list2 to the end of the unordered/unsorted non-circular linked list list1 leaving list2 intact (the least complex problem).

    Then the member function/method could be defined something like this:

    // Assumptions:
    //   1.  This is a singly unordered non-circular linked list
    //   2.  Your insertion member function is defined as 'void LinkedList::insert(DataType val)'
    //   3.  That you have a 'void LinkedList::begin()' that moves you to the front of the list
    //   4.  That you have a 'bool LinkedList::next()' that moves you to the next item in the list
    //       and returns true; or returns false if there are no more items in the list
    //   5.  That you have a 'DataType LinkedList::get_value()' that returns the value of the current node
    //   6.  That you have a 'bool LinkedList::is_empty()' that returns true if the list has no items in it
    void LinkedList::mergelists(LinkedList<DataType> &); 
    void LinkedList::mergelists(LinkedList<DataType> & other_list){
      // If the list is empty, we are done!
        // The list is not empty.
        // Move to the first item in the other list...
        // There exists at least one item; So, it is safe to use a do/while condition.
        // Loop through the items in the other list and add them to this one...
        do {
          // Copy it in...
        } while(
      }  // End if
    }  // End LinkedList::mergelists()
    Finally, you will probably never--outside of a programming class--need to write your own linked list. The ANSI/ISO C++ Standard Template Library (STL) provides a 'list' template class, which is more than sufficient for most situations you need a list.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts