Homework Help Question & Answers

C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

C++ LinkedList I need the code for copy constructor and assignment operator

#include <iostream>
#include <string>
using namespace std;

typedef string ItemType;

struct Node {
   ItemType value;
   Node *next;
};

class LinkedList {
private:
   Node *head;
   // You may add whatever private data members or private member functions you want to this class.
   void printReverseRecursiveHelper(Node *temp) const;
public:
   // default constructor
   LinkedList() : head(nullptr) { }

   // copy constructor
   LinkedList(const LinkedList& rhs);

   // Destroys all the dynamically allocated memory
   // in the list.
   ~LinkedList();

   // assignment operator
   const LinkedList& operator=(const LinkedList& rhs);

   // Inserts val at the rear of the list
   void insertToRear(const ItemType &val);

   // Prints the LinkedList
   void printList() const;

   // Sets item to the value at position i in this
   // LinkedList and return true, returns false if
   // there is no element i
   bool get(int i, ItemType& item) const;

   // Reverses the LinkedList
   void reverseList();

   // Prints the LinkedList in reverse order
   void printReverse() const;

   // Appends the values of other onto the end of this
   // LinkedList.
   void append(const LinkedList &other);

   // Exchange the contents of this LinkedList with the other
   // one.
   void swap(LinkedList &other);

   // Returns the number of items in the Linked List.
   int size() const;

};

0 0
Next > < Previous
ReportAnswer #1

LinkedList::LinkedList(const LinkedList& rhs)

{

    // if th elist rhs is empty

    if( rhs == NULL || rhs.head == NULL )

    {

        this->head = NULL;

        return;

    }

   

    // allocate memory to the node

    this->head = ( struct Node * )malloc( sizeof( struct Node ) );

   

    // set the value of the head node

    this->head->value = rhs.head.value;

   

    // point trav1 to the current list

    struct Node *trav1 = this->head;

   

    // point trav2 to the list rhs

    struct Node *trav2 = rhs.head.next;

   

    // trav the list

    while( trav2 != NULL )

    {

        // allocate memory to the node

        struct Node *new_node = ( struct Node * )malloc( sizeof( struct Node ) );

       

        // set the value of the head node

        new_node->value = trav2.value;

       

        // make the trav1 node point to the new node so

        // that we add this node to the current list

        trav1->next = new_node;

        

        // go to next node

        trav2 = trav2->next;

    }

   

    trav1->nxt = NULL;

}

const LinkedList& operator=(const LinkedList& rhs)

{

    LinkedList ob;

   

    // if the list rhs is empty

    if( rhs == NULL || rhs.head == NULL )

    {

        ob.head = NULL;

        return;

    }

   

    // allocate memory to the node

    ob.head = ( struct Node * )malloc( sizeof( struct Node ) );

   

    // set the value of the head node

    ob.head->value = rhs.head.value;

   

    // point trav1 to the current list

    struct Node *trav1 = ob.head;

   

    // point trav2 to the list rhs

    struct Node *trav2 = rhs.head.next;

   

    // trav the list

    while( trav2 != NULL )

    {

        // allocate memory to the node

        struct Node *new_node = ( struct Node * )malloc( sizeof( struct Node ) );

       

        // set the value of the head node

        new_node->value = trav2.value;

       

        // make the trav1 node point to the new node so

        // that we add this node to the current list

        trav1->next = new_node;

       

        // go to next node

        trav2 = trav2->next;

    }

   

    trav1->nxt = NULL;

   

    return ob;

}

Add Homework Help Answer
Add Answer of:
C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES
Related Questions