Question
/* Single Linked List Implementation in C++ */ #include using namespace std; class LinkedList{ private: struct node{ node *next; // Dynamic Data member. int data;
/*
Single Linked List Implementation in C++
*/
#include
using namespace std;
class LinkedList{
private:
struct node{
node *next; // Dynamic Data member.
int data;
};
node *head; // Dynamic data Member.
int size; // Static Date Member.
public:
// Default Constructor
LinkedList():head(NULL), size(0){}
// Custom Constructor
LinkedList(int data){
node* newNode = new node;
newNode->data = data;
newNode->next = NULL;
head = newNode;
size = 1;
}
//Destructor
~LinkedList(){
deleteList();
cout
}
// Copy Constructor
LinkedList(const LinkedList& rhs){
if(rhs.head == NULL){
head = NULL;
size = 0;
}
else{
head = new node;
node* rhsHead = rhs.head;
node* link = head;
link->data = rhsHead->data;
rhsHead = rhsHead->next;
while(rhsHead != NULL){
node* secondNode = new node;
secondNode->data = rhsHead->data;
//secondNode->next = NULL;
link->next = secondNode;
link = link->next;
rhsHead = rhsHead->next;
}
link->next = NULL;
}
}
// Assignment operator overloading
LinkedList& operator=(const LinkedList& rhs){
if(head == rhs.head){
return *this;
}
if(rhs.head == NULL){
deleteList();
return *this;
}
deleteList();
head = new node;
node* rhsHead = rhs.head;
node* link = head;
link->data = rhsHead->data;
rhsHead = rhsHead->next;
while(rhsHead != NULL){
node* secondNode = new node;
secondNode->data = rhsHead->data;
//secondNode->next = NULL;
link->next = secondNode;
link = link->next;
rhsHead = rhsHead->next;
}
link->next = NULL;
return *this;
}
// Getters
void printList(){
if(head == NULL)
return;
node* current = head;
while(current->next != NULL){
cout";
current = current->next;
}
cout } int listSize(){ return size; } // Setters || Mutators void append(int data){ node* newNode = new node; newNode->data = data; newNode->next = NULL; if(head == NULL){ head = newNode; size++; return; } node* current = head; while(current->next != NULL) current = current->next; current->next = newNode; } void insertFront(int data){ node* newNode = new node; newNode->data = data; newNode->next = NULL; newNode->next = head; head = newNode; } int frontElement(){ return -1; } int lastElement(){ return -1; } void deleteFront(){ if(head == NULL) return; node* current = head; head = head->next; delete current; } bool findElement(int value){ return false; } void deleteElement(int value){ } void deleteList(){ if(head == NULL) return; node* current = head; while(current->next != NULL){ node* temp = current; current = current->next; delete temp; } delete current; head = NULL; size = 0; } void deleteDuplicates(int value){ } void listReverse(){ } }; int main(){ LinkedList* l1 = new LinkedList(); LinkedList* l2 = new LinkedList(1); l1->append(90); (*l1).append(91); l1->append(92); // LinkedList l3 = l1; // copy constructor will be called l2->append(2); l1->printList(); l2->printList(); *l2 = *l1; // Assignment operator will be called l2->append(5); cout l1->printList(); l2->printList(); delete l1; delete l2; return 0; } Need to edit the code above to answer 1-10, on the screenshot below.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started