The copyList function here is not working working. Anyone have any ideas?

// header file

#ifndef H_orderedLinkedList
#define H_orderedLinkedList

#include <list>
#include <iostream>

using namespace std;

struct coordinates
{
    int xValue;            //variable to hold x coordinate
    int yValue;            //variable to hold y coordinate
};

struct node
{
    coordinates info;
    node *link;
};

class orderedLinkedList
{
        //Overload the stream insertion operator.
    friend ostream& operator<< (ostream&, const orderedLinkedList&);

public:
    void initializeList();
        //Function to initialize the list to an empty state.
        //Postcondition: first = NULL; count = 0

    bool isEmptyList();
        //Function to determine whether the list is empty.
        //Postcondition: Returns true if the list is empty;
        //                 otherwise, returns false.

    int length();
        //Function to return the number of nodes in the list.
        //Postcondition: The value of count is returned.

    void destroyList();
        //Function to delete all the nodes of the list.
        //Postcondition: first = NULL; count = 0

    void insertNode(int, int);
        //Function to insert newItem in the list.
        //Postcondition: first points to the new list and newItem is 
        //                 inserted at the proper place in the list, count++

    void deleteNode(int, int);
        //Function to delete deleteItem from the list.
        //Postcondition: If found, the node containing deleteItem
        //                 is deleted from the list; first points to
        //                 the first node of the new list, count++
        //                 If deleteItem isn't in the list, an error message displays.

    orderedLinkedList();
        //default constructor
        //Initializes the list to an empty state.
        //Postcondition: first = NULL; count = 0

    orderedLinkedList(const orderedLinkedList& otherList);
        //copy constructor

    ~orderedLinkedList();
        //destructor
        //Deletes all the nodes from the list.
        //Postcondition: The list object is destroyed.

private:
    void copyList(const orderedLinkedList& otherList); 
        //Function to make a copy of otherList.
        //Postcondition: A copy of otherList is created 
        //               and assigned to this list.

    int count;            //variable ot store the number of elements in the list.
    node *first;    //pointer that points to first element
    node *other;    //pointer to traverse through the list

};

#endif

//copyList Function//

void orderedLinkedList::copyList(const orderedLinkedList &otherList)
{
   node *newNode; //pointer to create a node
   node *current; //pointer to travel the list

   if(first != NULL)    //if the list is nonempty, make it empty
      destroyList();

   if(otherList.first == NULL) //otherList is empty
   {
        first = NULL;
        other = NULL;
         count = 0;
   }
   else
   {
        current = otherList.first;  //current points to the 
                                    //list to be copied
        count = otherList.count;

            //copy the first node
        first = new node;  //create the node

         assert(first != NULL);

        first->info = current->info; //copy the info
        first->link = NULL;           //set the link field of 
                                     //the node to NULL
        other = first;                 //make last point to the 
                                     //first node
        current = current->link;     //make current point to  
                                        //the next node

            //copy the remaining list
        while(current != NULL)
        {
            newNode = new node;  //create a node

            assert(newNode!= NULL);

            newNode->info = current->info;    //copy the info
            newNode->link = NULL;           //set the link of 
                                        //newNode to NULL
            other->link = newNode;         //attach newNode after last
            other = newNode;               //make last point to
                                        //the actual last node
            current = current->link;    //make current point to
                                           //the next node
        }//end while
    }//end else
}//end copyList

Recommended Answers

All 2 Replies

bump

what do you mean by "is not working" ?

Doesn't destroyList() set all variables to 0 before returning? Is so then why do it again when otherList.first == NULL?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.