Question

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
Add a comment Improve this question Transcribed image text
Answer #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 a comment
Know the answer?
Add Answer to:
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:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

    C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...

  • I want the full code for this part and Its needs to be done in C++...

    I want the full code for this part and Its needs to be done in C++ Use a linked list to implement the following skeleton of an unsorted list typedef int ItemType: struct NodeType ItemType item; NodeType "next; class List List(); // default constructor List( const List &x); I copy constructor: deep copy is required List & operator = (const List &x); // assignment operator: deep copy. 1s required bool IsThere(ItemType x); identify if x is in the list void...

  • #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...

    #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {     Contact item;     NodePtr next;     friend class ContactList; }; class ContactList { public:     ContactList(char* clfile) ;     ~ContactList();     void display       (ostream & output) const;     int   insert        (Contact record_to_insert);     int   insert        (ContactList contact_list);     int   remove        (Contact record_to_delete);     int   size          () const;     int   save          () const;     void find_by_lname (ostream & output, string lname) const;     void...

  • Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using...

    Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using namespace std; class List; class Iterator; class Node { public: /* Constructs a node with a given data value. @param s the data to store in this node */ Node(string s); /* Destructor */ ~Node() {} private: string data; Node* previous; Node* next; friend class List; friend class Iterator; }; class List { public: /** Constructs an empty list. */ List(); /* Destructor. Deletes...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

    C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. Your goal is to overload the operators for a generic “BigInt” class. You will need to write...

  • USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot...

    USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot be viewed from outside of the private class. //only the class functions within the class can access private members. private: string name; string email; string phone; //the public class is accessible from anywhere outside of the class //however can only be within the program. public: string getName() const { return name; } void setName(string name) { this->name = name; } string getEmail() const {...

  • Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...

    Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node    //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT