2. Removing nodes storing a given value from a linked list. You are given a link
ID: 3639256 • Letter: 2
Question
2. Removing nodes storing a given value from a linked list. You are given a linked list L storing integers and you are asked to remove all nodes storing a given value A. Hint: the algorithm traverses the linked list looking for nodes that store A. When a node is removed, the links need to be repaired, opposite to what we did in class for insertion.
a. Give a brief step by step description of the algorithm.
Look at the list
Match with the element to be removed to the current element in the list
When it finds the match in the list, it removes the element and adjusts the pointer of the previous element to the next element in the list
b. Give a pseudocode description of the algorithm.
c. Trace the algorithm for L = {1, 2, 3, 2} and A = 2.
d. If the number of nodes in the list is n, what is the algorithm running time? Explain.
If you know any of these a or b or c or d. Please help
Explanation / Answer
You have already given a pseudo-code so I will just write the code.
Its pretty easy and self explanatory.
Declaration of a node
the search function:
int getElementpos( olist L , char A ) {
int pos=0;
if (L->element == A) return pos;
else {
L= L->next;
++pos;
}
the delete function :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.