问题
I am working on a project about linked list, I have trouble with inserting a number to a sorted linked list. The number inserted to the second position every time, I cannot figure out where the problem is.Here is my code:
void insertSort(struct linkedList *n,int num,int *length){ //insert number to a sort linked list
node *new = (node *) malloc(sizeof(node)); //create a new node
new->next=NULL;
new->data = num;
while(n!=NULL&&n->data > new->data){ // find which position num should insert in sorted list
n = n->next;
}
new->next = n->next;
n->next= new;
length += 1;
}
n is head of the linked list. I initialized head points to the first node and has not value. Here is how I call this function:
insertSort(head->next,num,&length);
The number inserted to the second position every time. Like I want to insert 45 into sorted linked list <16,32,72,81,97>, after inserting, the list would be <16,45,32,72,81,97>. 45 inserts to the second position for some reason.
回答1:
The problem :
The number inserted to the second position every time...
is occurring because in this part of code:
while(n!=NULL&&n->data > new->data){ // find which position num should insert in sorted list
n = n->next;
}
new->next = n->next;
n->next= new;
you are inserting the new node after the n->next
.
Assume the first node of your linked list is having data 16
and now you want to insert the new node with data 45
in the linked list, the while
loop condition will fail as 16 > 45
will evaluate to false
.
And the statement after while
loop new->next = n->next;
will set the next of new node to next of the first node and n->next= new;
will insert the new node after first node. Hence, the new node inserted to second position every time.
There are few more problems with your function insertSort()
like:
- It does not track the
head
of the linked list while inserting a node to the linked list and, - What will happen if the node inserted is the first node of the linked list? In that case
n
willNULL
and ininsertSort()
afterwhile
loop it is accessingnext
ofn
-new->next = n->next;
.
Looking at the example you have given - <16,32,72,81,97>, you want to insert in ascending order. You can do:
struct linkedList *insertSort(struct linkedList *n, int num, int *length) {
struct linkedList *first_node = n;
struct linkedList *new_node = malloc(sizeof(struct linkedList)); //create a new node
new_node->next=NULL;
new_node->data = num;
if (first_node == NULL || first_node->data >= new_node->data) {
new_node->next = first_node;
first_node = new_node;
} else {
struct linkedList *temp = first_node;
while (temp->next != NULL && temp->next->data < new_node->data) {
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
}
*length += 1;
return first_node;
}
Here, you can see that I have changed return type void
to struct linkedList *
so that after inserting new node to appropriate location in linked list insertSort()
will return the head
of the linked list. That way you can keep track of head
of linked list after every insertion. You just need to do:
head = insertSort(head, num, &length);
from wherever you are calling insertSort()
.
Alternatively, you can pass the address of head
pointer in insertSort()
and keep track of it, if you don't want to change the return type of insertSort()
, like this:
void insertSort(struct linkedList **head, int num, int *length) {
struct linkedList *new_node = malloc(sizeof(struct linkedList)); //create a new node
new_node->next=NULL;
new_node->data = num;
if (*head == NULL || (*head)->data >= new_node->data) {
new_node->next = *head;
*head = new_node;
} else {
struct linkedList *temp = *head;
while (temp->next != NULL && temp->next->data < new_node->data) {
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
}
*length += 1;
}
You can call insertSort()
like this:
insertSort(&head, 32, &length);
Hope this helps.
回答2:
because you are never testing the position of the node . your code only puts the new node at 2nd position . it does not checks where it should be in the sorted list try below
void sortedInsert(struct Node** head_ref, struct Node* new_node) {
struct Node* current; /* Special case for the head end */
if(*head_ref == NULL || (*head_ref)->data >= new_node->data) {
new_node->next = *head_ref;
*head_ref = new_node;
}
else {
/*Locate the node before the point of insertion */
current =*head_ref;
while (current->next!=NULL && current->next->data < new_node->data)
{
current = current->next;
}
new_node->next =current->next;
current->next = new_node; }
}
来源:https://stackoverflow.com/questions/47233224/insert-a-number-to-a-sorted-linked-list-why-the-number-inserts-to-the-second-po