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;

}

Know the 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.
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) {...

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

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template ...

    Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Starter testDeque.cpp which contains: Timer class holder (you need to go through the LearnCpp Ch15 and import it in) Node class Deque class specification (you need to fill out the definition) here is the testDeque.cpp code: // C++ implementation of doubly linked list Deque doubly linked list...

  • Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deq...

    Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Recommended Steps testDeque.cpp : // C++ implementation of doubly linked list Deque doubly linked list #include <bits/stdc++.h> using namespace std; class Timer { // To replace with the full timer class definition // inside this folder: LearnCpp9_18_timeSortArray.cpp }; // Node of a doubly linked list template<class T> class...

  • Create an h file called list. It should have the following features: lisi's funnc In no particular order: List(): Default constructor. This should construct an empty List, the member variables sh...

    Create an h file called list. It should have the following features: lisi's funnc In no particular order: List(): Default constructor. This should construct an empty List, the member variables should be initialized to reflect this state. This function is already fully implemented. 1. List(const List<Type>& other): Copy constructor for the linked list. This should create an entirely new linked list with the same number of Nodes and the Values stored these Nodes in the same order as seen the...

  • In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

    In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

  • PLEASE HELP WITH THE FIX ME'S #include #include #include #include "CSVparser.hpp" using namespace std; //==...

    PLEASE HELP WITH THE FIX ME'S #include #include #include #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; //============================================================================ // Linked-List class definition //============================================================================ /** * Define a class containing data members and methods to *...

  • C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supp...

    C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The...

Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
Share Your Knowledge

Post an Article
Post an Answer
Post a Question with Answer

Self-promotion: Authors have the chance of a link back to their own personal blogs or social media profile pages.