1. Consider a struct type Node whose members are Node* link and int val (the dat
ID: 3905314 • Letter: 1
Question
1. Consider a struct type Node whose members are Node* link and int val (the data stored in that node). Implement the following functions:
// Remove first node after *f whose val is equal to the parameter val.
// If val doesn’t occur then do nothing.
void deleteVal(Node * f, int val)
{
}
// Insert val into the list right after the node *f.
void insertVal(Node * f, int val)
{
}
2. Implement a recursive function void print(Node * first) to print the values in a linked list in forward order. (If first is NULL then the function should print nothing.) . Assume Node is a struct type as in Question 1.
Explanation / Answer
If you have any doubts, please give me comment..
void deleteVal(Node * f, int val)
{
if(f==NULL)
return;
Node *temp = f, prev = NULL;
while(temp!=NULL &&temp->val != val){
prev = temp;
temp = temp->link;
}
prev->link = temp->link;
free(temp); //in c++ delete temp
}
// Insert val into the list right after the node *f.
void insertVal(Node * f, int val)
{
Node *temp = new Node();
temp->val = val;
Node *t = f->link;
f-link = temp;
temp->next = t;
}
void print(Node * first){
if(first==NULL)
return;
printf("%d ", first->val); // in c++ cout<<first->val;
print(first->link);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.