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++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...

  • 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...

  • In C++, change this code to a circular linked list #include <iostream> using namespace std; struct...

    In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...

    CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node {    char c;    Node *next; }; class LinkedString {    Node *head; public:    LinkedString();    LinkedString(char[]);    LinkedString(string);    char charAt(int) const;    string concat(const LinkedString &obj) const;    bool isEmpty() const;    int length() const;    LinkedString substring(int, int) const;    //added helper function to add to linekd list private:    void add(char c); };...

  • 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...

  • 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...

  • #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&...

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