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

this is i have code for double linked list with insert at sorted list. i have so

ID: 3669224 • Letter: T

Question

this is i have code for double linked list with insert at sorted list. i have some error that

it keep give me this error i am not sure how to fix is anyone possible to help me?

#include <stdio.h>
#include <stdlib.h>

typedef struct list {
   struct list* next;
   struct list* prev;
   int val;
} *LIST;

//   DO NOT MODIFY ABOVE THIS LINE

//   MODIFY THIS FUNCTION
//
//   Given a pointer to the head of the list. Insert x in its correct sorted place.
//   For example, if the list is [ 1 <-> 4 <-> 9 <-> 12 ], and x is 10, then the list
//       should look like [ 1 <-> 4 <-> 9 <-> 10 <-> 12 ] after insertion.

LIST InsertDouble(LIST head, int x) {
  
LIST New_node = (LIST)malloc(sizeof(struct list));  
  
struct list *Currentptr = NULL;
  
New_node -> next = NULL;                               // make new node pointer to null
  
New_node ->val = x;                                   // make new node value set at x
  
if(New_node == NULL){                                   // if new node have no x value it terminate
  
printf("No exist memory");
    return 0;
}
// add at front of list
if (head == NULL){
  
head = New_node;
  
}
  
//if x is less then first node of list
else if(New_node->val < head -> val ){
  
   New_node -> next = head;
  
head = New_node;
  
}
  
// make pointer for head so i can make move around
Currentptr = *head;                               // set a pointer which is current one
//to save New_node to end of the list
while(Currentptr != NULL){
  
if(Currentptr-> next == NULL){
  
Currentptr->next = New_node;
  
}
else if(Currentptr ->val < New_node -> val){
  
    New_node -> next = Currentptr -> next;
  
   Currentptr ->next = New_node;
  
   New_node->prev = Currentptr;
  
   Currentptr ->next ->next = New_node;
  
  
}
  
Currentptr = Currentptr->next;
}
  
  
/*
while (head != NULL){
if(Currentptr ->next == NULL && Currentptr -> val < New_node ->val){
  
New_node -> prev = Currentptr;
  
Currentptr -> next = New_node;
  
}
else if(Currentptr -> next != NULL && New_node ->val > Currentptr -> val){
  
  
}
  
Currentptr = Currentptr -> next;
  
}
  
*/
return head;
  
}
  

Explanation / Answer

The modified code will be:

#include <stdio.h>
#include <stdlib.h>

typedef struct list {
   struct list* next;
   struct list* prev;
   int val;
} *LIST;

LIST* InsertDouble(LIST *head, int x)

{

LIST *newNode = (LIST*) malloc(sizeof(list));

newnode -> next = NULL;                               // make new node pointer to null
  
newnode ->val = x;   

if(newnode == NULL){                                   // if new node have no x value it terminate
  
printf("No exist memory");
    return 0;
}
// add at front of list
if (head == NULL)

{

newnode -> next = NULL;

newnode->prev =NULL;

return newnode;

}

if(newnode->val <= head -> val )

{
  
newnode -> next = head;

newnode->prev =NULL;
  
head->prev = newnode;
  
head =newnode;

}

else {

       

       LIST *Currentptr = head;

while(Currentptr->next != NULL && Currentptr->next->val < newNode->val)
{

Currentptr = Currentptr->next;

}

newNode->prev = Currentptr;

       newNode->next = Currentptr->next

if(Currentptr -> next != NULL)

{

Currentptr->next->prev = newNode;

}

Currentptr ->next = newNode;

return head;
  
}