aph3x
03-29-2002, 04:23 AM
hello everyone, long time no... erm, post :)
as an assignment, im trying to sort a linked list using the insertion sort algorithm. sorting by swapping node values is trivial, however, i have to sort the list by manipulating the pointers of each node :eek:
the data being sorted is a paragraph from my data structures text. i have stored the paragraph in an array. i parse the paragraph and store each word in each struct's value array. each node has a previous pointer and a next pointer named plink and nlink, respectively.
the following code sorts the list, but for some reason it drops words (structs) from the list, and i can figure out why :confused: hopefully someone can help me find the bug...
head, temp, and this are pointers to a node structure.
head points to the first dynamically allocated node... below is code for how im currently implementing the insertion sort by pointer value
this = head->nlink;
for(i=1; this->nlink != 0x00; i++)
{
temp = this;
this = this->nlink;
j = i - 1;
while(j>=0 && (strncmp(temp->value, temp->plink->value, 24) < 0))
{
temp->plink->nlink = temp->nlink;
temp->nlink->plink = temp->plink;
temp->nlink = temp->plink;
temp->plink = temp->plink->plink;
temp->nlink->plink = temp;
if(temp->plink == 0x00)
{
head = temp;
break;
}
j--;
}
}
printf("\n\n\t\t\t -==[ Sorted Paragraph Text ]==-\n\n");
for(this=head; this->nlink != NULL; this=this->nlink)
printf("%s ", this->value);
if anyone needs ALL the code, let me know... TIA :)
as an assignment, im trying to sort a linked list using the insertion sort algorithm. sorting by swapping node values is trivial, however, i have to sort the list by manipulating the pointers of each node :eek:
the data being sorted is a paragraph from my data structures text. i have stored the paragraph in an array. i parse the paragraph and store each word in each struct's value array. each node has a previous pointer and a next pointer named plink and nlink, respectively.
the following code sorts the list, but for some reason it drops words (structs) from the list, and i can figure out why :confused: hopefully someone can help me find the bug...
head, temp, and this are pointers to a node structure.
head points to the first dynamically allocated node... below is code for how im currently implementing the insertion sort by pointer value
this = head->nlink;
for(i=1; this->nlink != 0x00; i++)
{
temp = this;
this = this->nlink;
j = i - 1;
while(j>=0 && (strncmp(temp->value, temp->plink->value, 24) < 0))
{
temp->plink->nlink = temp->nlink;
temp->nlink->plink = temp->plink;
temp->nlink = temp->plink;
temp->plink = temp->plink->plink;
temp->nlink->plink = temp;
if(temp->plink == 0x00)
{
head = temp;
break;
}
j--;
}
}
printf("\n\n\t\t\t -==[ Sorted Paragraph Text ]==-\n\n");
for(this=head; this->nlink != NULL; this=this->nlink)
printf("%s ", this->value);
if anyone needs ALL the code, let me know... TIA :)