Has to be in C++, and most ijmportantly it MUST BE USED WITH SMART POINTERS not
ID: 3798357 • Letter: H
Question
Has to be in C++, and most ijmportantly it MUST BE USED WITH SMART POINTERS not raw pointers. 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
#include<iostream>
#include<stdlib.h>
using namespace std;
struct ListRec
{
char value;
ListRec *next;
};
void print(ListRec* listHead)
{
ListRec *temp=listHead;
while(temp!=NULL)//printing list elements
{
cout<<temp->value<<" ";
temp=temp->next;
}
cout<<" ";
}
void deepCopy(ListRec* oldListHead,ListRec* newListHead)
{
ListRec* temp= oldListHead,*temp2=NULL;
while(temp!=NULL)
{
if(temp2==NULL)
{temp2 = new ListRec();//creating new node
temp2->value = temp->value;//copying contents
}
else
{
temp2->next = new ListRec();//creating new node
temp2->next->value = temp->value;
temp2=temp2->next;//copying contents
}
temp=temp->next;//iterating loop
}
newListHead =temp2;
}
int main()
{
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.