I've written a sort for a linked list (at least i thought i did), and can not figure out what is wrong. If I could get any help it would be appreciated.
html posted code
[edit]
below is excerpt from the source code. what i'm trying to do is call this public class member from another public class member that extracts the data from 2 other instansiated link list objects. the objects and the new sorted linked list are from the same class. i'm passing the object that I want the final list to be. basically i'm combining 2 already built lists and sorting them in the below function after i've extracted each individual node. it isn't working. i'm not getting any compile errors, but the logic must not be correct. when i run it it doesn't crash, but it doesn't fill the list with anything. I have been accused of not being percise enough, but i hope the details i'm providing cover the full aspect of my frustration. I know there are some genises that answer questions here so I beg you for your input.
Code:
void linklist::orderedlist(linklist *CopyC)
{ //
link *current , *previous;
current = previous = head;
link *newlink = new link;
for(x=0;x<=SIZE;x++)
{
newlink->fname[x] = buffer1[x];
}
for(x=0;x<=SIZE;x++)
{
newlink->lname[x] = buffer2[x];
}
for(x=0;x<=SIZE;x++)
{
newlink->ssnumber[x] = buffer3[x];
}
newlink->age=age;
newlink->weight=weight;
newlink->next = NULL;
if (head == NULL) //this tells me I have an empty list
{
head = newlink;
tail = head;
}
else
{
//find the position to insert it
while ( strcmp(current->lname,buffer2)==1 && current->next !=NULL)
{
previous = current;
current = current->next;
}
if (current==head)//it will become the new head
{
newlink->next=head;
head=newlink;
}
else
{
if (current->next==NULL)//becomes the new tail
{
tail->next=newlink;
tail = newlink;
}
else
{
previous->next=newlink;
newlink->next=current;
}
}
}
}
i also tried this, but it doesn't work either.
Code:
void linklist::orderedlist(linklist *CopyC)
{ //
*CopyC->current = *CopyC->previous = *CopyC->head;
*CopyC->link->node = new link;
for(x=0;x<=SIZE;x++)
{
*CopyC->link->fname[x] = buffer1[x];
}
for(x=0;x<=SIZE;x++)
{
*CopyC->link->lname[x] = buffer2[x];
}
for(x=0;x<=SIZE;x++)
{
*CopyC->link->ssnumber[x] = buffer3[x];
}
*CopyC->link->age=age;
*CopyC->link->weight=weight;
*CopyC->link->next = NULL;
if (*CopyC->head == NULL) //this tells me I have an empty list
{
*CopyC->head = *CopyC->link;
*CopyC->tail = *CopyC->head;
}
else
{
//find the position to insert it
while ( strcmp(*CopyC->current->lname,buffer2)==1 && *CopyC->current->next !=NULL)
{
*CopyC->previous = *CopyC->current;
*CopyC->current = *CopyC->current->next;
}
if (*CopyC->current==*CopyC->head)//it will become the new head
{
*CopyC->link->next=*CopyC->head;
*CopyC->head=*CopyC->link;
}
else
{
if (*CopyC->current->next==NULL)//becomes the new tail
{
*CopyC->tail->next=*CopyC->link;
*CopyC->tail = *CopyC->link;
}
else
{
*CopyC->previous->next=*CopyC->link;
*CopyC->link->next=*CopyC->current;
}
}
}
}
[\edit]