Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the Rule of Three for your linked list class. This include creating a destructor, copy constructor, and overloaded assignment operator. Your destructor should delete

Implement the "Rule of Three" for your linked list class. This include creating a destructor, copy constructor, and overloaded assignment operator. Your destructor should delete all nodes in the list, your copy constructor should create a "deep copy" when passing your linked list as a parameter to a function, and your assignment operator should create a "deep copy" when assigning a previous linked list to another linked list object.

image text in transcribed

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

linkedlist.h (Header File)

#ifndef LINKEDLIST_H #define LINKEDLIST_H

#include

using namespace std;

struct Node{ int data; Node* next; };

class LinkedList{ private: Node * head = nullptr; public: // Constructor LinkedList(); // Destructor ~LinkedList(); // Copy Constructor LinkedList(const LinkedList& listToCopy); // Copy Assignment Override LinkedList& operator=(const LinkedList& listToCopy); void push(int); int pop(); void print(); // Returns numnber of nodes in the list int size(); // Adds a node at the specified index within the list void insert(int, int); };

#endif

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Linkedlist File

#include "linkedlist.h"

// Constructor LinkedList::LinkedList(){ head = nullptr; }

// Destructor LinkedList::~LinkedList(){ // pop all items off list while(head != nullptr){ pop(); } }

// Copy Constructor LinkedList::LinkedList(const LinkedList& listToCopy){ // Copy linked list conents to new object }

// Copy Assignment Override LinkedList& LinkedList::operator=(const LinkedList& listToCopy) { if (this != &listToCopy) {

} return *this; }

void LinkedList::push(int new_data) { Node* new_node = new Node; new_node->data = new_data; new_node->next = head; head = new_node; }

int LinkedList::pop(){ int returnVal = head->data; Node* oldHead = head; head = head->next; delete oldHead; return returnVal; }

void LinkedList::print() { Node* ptr = head;

while (ptr != nullptr) { coutdata next; } cout

int LinkedList::size(){ Node* ptr = head; int count = 0; while (ptr != nullptr) { count++; ptr = ptr->next; } return count; }

void LinkedList::insert(int value, int index){ if(index > this->size()) return; else if(index == 0) push(value); Node* ptr = head; Node* lastPtr = nullptr; int idx = 0; while(idx next; } Node* newNode = new Node; newNode->data = value; newNode->next = ptr; lastPtr->next = newNode; }

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Main.cpp File

#include #include #include "linkedlist.h"

using namespace std;

void testCopyConstructor(LinkedList list){ list.pop(); list.print(); // CORRECT: 4, 7 }

int main() { LinkedList list; list.push(7); list.push(10); list.push(3); list.print(); // 3, 10, 7 cout Implement the "Rule of Three" for your linked list class. This include creating a destructor, copy constructor, and overloaded assignment operator. Your destructor should delete all nodes in the list, your copy constructor should create a "deep copy" when passing your linked list as a parameter to a function, and your assignment operator should create a "deep copy" when assigning a previous linked list to another linked list object. Your function prototypes should appear as follows: ll Destructor LinkedList): Copy Constructor LinkedList(const LinkedList& listToCopy); Copy Assignment Override LinkedList& operator-(const LinkedList& listToCopy); You can use the following template to get started. Use the main) function to verify your code is working correctly: RoTExample.zip NOTE: In this example code, "addNode" has been renamed to "push" to resemble functions with similar functionality in other list type data structures

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David J. Auer

3rd Edition

0131986252, 978-0131986251

More Books

Students also viewed these Databases questions