Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include<stdlib.h> using namespace std; struct node { //Task

ID: 3798321 • Letter: #

Question

#include <iostream>
#include<stdlib.h>
using namespace std;

struct node { //Task 1
char val;
node *next;
};

void printList(node *head) { //Task 2
if (head == NULL) {
cout << "Empty list" << endl;
return;
}

node *temp = head;
int count = 0;
while(temp != NULL) {
cout << "Node " << count << ": " << temp ->val << endl;
count++;
temp = temp->next;
}
}
struct node *globalhead = NULL;
void deepCopy(node *head1, node *head2) { //Task 3
if (head1 == NULL) {
cout << "Empty list" << endl;
return;
}

node *temp = head1;
node *curr = NULL;
node *prev = NULL;
while(temp != NULL) {
  
curr = (struct node *)malloc(sizeof(struct node));
curr->val = temp->val;
if(prev==NULL){
head2 = curr;
globalhead = curr;
}else{
  
prev->next = curr;
}
prev = curr;
temp = temp->next;

}

}
int main() { //Task 4

// Example #1
node p00, p01, p02, p03, p10, p11, p12, p13;

// List 1: A B B B
p00.val = 'A'; p00.next = &p01;
p01.val = 'B'; p01.next = &p02;
p02.val = 'B'; p02.next = &p03;
p03.val = 'B'; p03.next = NULL;

p10.next = NULL;

cout<<"First List:"<<endl;
printList(&p00);
cout<<"Deep Copied List:"<<endl;
deepCopy(&p00,globalhead);
printList(globalhead);

return 0;
}

Hi, so as you see thats the assignment and thats the code. As you see there is each task listed and its all done. Now my issue is that all the tasks MUST BE REDONE USING SMART POINTERS and i cant figure it out. Thanks :))))

Consider that individual nodes in an unsorted linked list have the following definition: struct List Rec char value; ListRec next 1.Without implementing a separate class to maintain the list, simply create a simple list directly in the main function. You should create a pointer to point to the head ofthe list, and name that pointer head. Use the previously stated definition of anode, and create the following list in main: head 'A' CT MN In the above visual depiction, the means that the next data member points to NULL or nullptr) 2.Write a void function in the same file as main to print out a linked list, given that the head is passed into the function as an argument. The prototype (and subsequently, the header) of the function should look like void print (ListRec listHead) //print out the elements in the list 3.Wnte another function that takes two parameters: The head of a list to be copied The head of another list that will contain the copy of the first The function performs a deep copy. Recall that with a deep copy, you must create anew node and copy over the value from the corresponding node in the list being copied to the list that will contain the copy. The function header is as follows: void deepCopy(ListRec oldListHead, ListRec newListHead) //perform a deep copy from old list to new list 4.Write the main function to perform a test of each of the above tasks. It must create the list (task 1), call the print function (from task 2, and the deepCopy function (from task 3.0 From main, you should 1. make a copy of the original list 2. change the data in first node of the original list 3. call the print function on both the original list you created, and the copied list to verify that the deep copy worked as expected.

Explanation / Answer

//Tested on Windos OS Dev c++

/*********************LinkedList.cpp********************/

#include <iostream>
#include<stdlib.h>

using namespace std;
struct node { //Task 1
char val;
node *next;
};
void printList(node *head) { //Task 2
if (head == NULL) {
cout << "Empty list" << endl;
return;
}
node *temp = head;
int count = 0;
while(temp != NULL) {
cout << "Node " << count << ": " << temp ->val << endl;
count++;
temp = temp->next;
}
}
struct node *globalhead = NULL;

void deepCopy(node *head1, node **head2) { //Task 3
if (head1 == NULL) {
cout << "Empty list" << endl;
return;
}
node *temp = head1;
node *curr = NULL;
node *prev = NULL;
while(temp != NULL) {
  
curr = (struct node *)malloc(sizeof(struct node));
curr->val = temp->val;
curr->next = NULL;
if(*head2==NULL){
   *head2=curr;
}else{
   prev=*head2;
   while(prev->next!=NULL){
       prev=prev->next;
   }
   prev->next=curr;
}

temp = temp->next;
}
}
int main() { //Task 4
// Example #1
node p00, p01, p02, p03, p10, p11, p12, p13;
// List 1: A B B B
p00.val = 'A'; p00.next = &p01;
p01.val = 'B'; p01.next = &p02;
p02.val = 'B'; p02.next = &p03;
p03.val = 'B'; p03.next = NULL;
p10.next = NULL;
cout<<"First List:"<<endl;
printList(&p00);
cout<<"Deep Copied List:"<<endl;
deepCopy(&p00,&globalhead);
printList(globalhead);
return 0;
}

/************************output************************/

First List:
Node 0: A
Node 1: B
Node 2: B
Node 3: B
Deep Copied List:
Node 0: A
Node 1: B
Node 2: B
Node 3: B

--------------------------------
Process exited after 0.01713 seconds with return value 0
Press any key to continue . . .

Thanks a lot