My code is below, I don\'t know if my code is right or wrong, can someone help m
ID: 3746842 • Letter: M
Question
My code is below, I don't know if my code is right or wrong, can someone help me to check? You don't need to run the code, pls fixed my code if you can.
############
to insert the value of 5 right before the value in the node pointed to by Node *current (non null). write several statements that accomplish this.
Here is the declaration for Node:
struct Node {
int data;
Node * previous, *next;
};
inserted using new and assign 5 to it's data member as well as give appropriate values to its previous and next pointers to link correctly. Be mindful of the case where current is at the beginning of the list, so current->previous will be null.
############
struct Node {
int data;
Node *previous, *next;
};
int main(){
Node *current;
insertAfter(5)
break;
}
void insertAfter(5)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(current->previous = NULL)
{
newNode->previous = NULL;
newNode->next = current;
current->previous = newNode;
}
}
Explanation / Answer
//corrected code
struct Node {
int data;
Node *previous, *next;
};
void insertAfter (Node* , int);
int main(){
Node *current;
insertAfter(current ,5);
}
void insertAfter(Node*current,int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(current->previous == NULL)
{
newNode->previous = NULL;
newNode->next = current;
current->previous = newNode;
}
else{ newNode->next =current;
current->previous->next=newNode;
newNode->previous = current ->previous;
current->previous = newNode;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.