Write C++ code to answer the questions below. Use the provided definition of a l
ID: 3798898 • Letter: W
Question
Write C++ code to answer the questions below. Use the provided definition of a linked list node. Write constructors for this struct that initializes an object to i. the default values, zero and nullity, respectively the initial values, e and n, respectively. Use the last constructor to create an object called new Node on the heap part of the memory with the value 7. Insert new Node after the node with an address prep Node. Assume that prep Node! = nullity. What is the running time of this operation? Write code that deletes a node after prep Node and displays the value of element of the deleted node on the screen using court. Assume that prep Node! = nullity. What is the running time of this operation?Explanation / Answer
struct ListNode {
int element;
ListNode *next;
};
a)
i)
ListNode(){
element = 0;
next = nullptr;
}
ii)
ListNode(int e, ListNode *n){
element = e;
next = n;
}
b)
ListNode *newNode = new ListNode(7, nullptr);
c)
newNode->next = prevNode->next;
prevNode->next = newNode;
Running time : O(1)
d)
// if there is an node after prevNode, then delete it
if(prevNode->next != nullptr){
ListNode *temp = prevNode->next;
prevNode->next = prevNode->next->next;
delete temp;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.