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

Assume that the node of a linked list is in the usual info-link form with the in

ID: 3678351 • Letter: A

Question

Assume that the node of a linked list is in the usual info-link form with

the info of type int. (list and ptr are pointers of type nodeType.) The

following code creates a linked list:

ptr = new nodeType;

ptr->info = 16;

list = new nodeType;

list->info = 25;

list->link = ptr;

ptr = new nodeType;

ptr->info = 12;

ptr->link = nullptr;

list->link->link = ptr;

Use the linked list created by this code to answer the following questions.

(These questions are independent of each other.) Declare additional pointers

if you need them. (3, 4)

a. Which pointer points to the first node of the linked list?

b. Determine the order of the nodes of the linked list.

c. Write a C++ code that creates and inserts a node with info 45 after

the node with info 16.

d. Write a C++ code that creates and inserts a node with info 58 before

the node with info 25. Does this require you to the change the value

of the pointer that was pointing to the first node of the linked list?

e. Write a C++ code that deletes the node with info 25. Does this

require you to the change the value of the pointer that was pointing to

the first node of the linked list?

Explanation / Answer

a)

ptr points to the first node of the linked list.

b)

order is

16->25->12

c)

we can write a simple insert function for the same.

void LinkedList::insertAfter(Node* after, int val)
{
if(after != NULL)
{
after->_pNext = new Node(val, after->_pNext);
}
}

This will take the node after which we want to insert and will insert the required value

4)

Same function can be used for this task as well. Yes it will require us to change the first node in case user wants to make the new node as head node.

5)

I have written a c++ for deletion. There are some cases here we need to handle.

a) if the node is the head node - In this case we need to delete the head node and the second node should become head.

b) if the node is at the end - in this case we need to free the last node and the second last node should point to null.

code:

void LinkedList::remove(int val)
{
Node *pPre = NULL, *pDel = NULL;
  
if (_pHead->_value == val) {
pDel = _pHead;
_pHead = pDel->_pNext;
delete pDel;
return;
}
  
pPre = _pHead;
pDel = _pHead->_pNext;
  
while (pDel != NULL) {
if (pDel->_value == val) {
pPre->_pNext = pDel->_pNext;
if (pDel == _pTail) {
_pTail = pPre;
}
delete pDel;
break;
}
pPre = pDel;
pDel = pDel->_pNext;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote