Reputation: 1097
I am trying to create a program that inserts numbers into a linked list in ascending order. This is my insert function. It works for inserting some numbers but not others. I think it has something to do with the last part, but i cant figure it out.
node* insert(node* head, int value) {
//check if head hasn't been created
if (head == NULL) {
head = malloc(sizeof(node));
if(head == NULL) {
printf("Failed to create head node");
return head;
}
head->value = value;
head->next = NULL;
return head;
}
//create a new node
node *newNode;
newNode = malloc(sizeof(node));
if(newNode == NULL) {
printf("Failed to create node");
return newNode;
}
newNode->value = value;
newNode->next = NULL;
//see if new node should be placed before head
if (value < head->value) {
newNode->next = head;
return newNode;
}
//search through to find correct spot and insert the node
node *temp = NULL;
temp = head;
while(temp->next != NULL && temp->value < value) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
return head;
}
Upvotes: 3
Views: 46229
Reputation: 6477
It would be much better if you firstly implement (and test) functions like: push_front()
, insert()
(insert before), and push_back()
, (probably advance (Node* curr, int steps);
) and then simply take into consideration all the possibilities of insertion, i.e.:
push_front / back()
)advance()
) over all elements from head
and on, till:
insert()
before it.push_back()
.in your new function insert_ordered()
.
Upvotes: 0
Reputation: 1
//This code of mine works perfectly.
void insertInAscOrder(int val)
{
node *new1;
node *temp;
node *previous;
//create new node
new1 = (node *)malloc(sizeof(node));
//check whether node is created or not
if(new1 == NULL)
{
printf("Insufficient memory.");
return;
}
//Updating different parts of the node
new1 -> info = val;
new1 -> next = NULL;
//checking whether the node created is only node or not
if (start == NULL)
{
start = new1;
}
//If value is less than the value of first node
else if(val < start -> info)
{
new1 -> next = start;
start = new1;
}
else
{
previous = start;
temp = start -> next;
//Go to the position where node is to be inserted
while(temp != NULL && val > temp -> info)
{
previous = temp;
temp = temp -> next;
}
//Insert the node at particular position
if(temp == NULL)
{
previous -> next = new1;
}
else
{
new1 -> next = temp;
previous -> next = new1;
}
}
}
Upvotes: 0
Reputation: 40155
Part of the following bad
//search through to find correct spot and insert the node
node *temp = NULL;
temp = head;
while(temp->next != NULL && temp->value < value) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
e.g. to fix like this:
node *temp ,*prev;
temp = head;
while(temp != NULL && temp->value <= value) {
prev = temp;
temp = temp->next;
}
newNode->next = temp;
prev->next = newNode;
or
node *temp ,*prev;
temp = head->next;
prev = head;
while(temp != NULL && temp->value < value) {
prev = temp;
temp = temp->next;
}
newNode->next = temp;
prev->next = newNode;
Upvotes: 4