kimhoong79
Technical User
Hi, I am still in learning phrase on this matter. I have written a program to allow user to enter album's title and singer. I will then insert these information into a Linked List. After insertion and any other tasks, I will sort the Linked List. I think I wrote the sorting code correctly but it's still not working well. Please give comment. Here's the code:
void Sort(NodePtr *L, int criteria)
{
NodePtr p, q;
for(p=*L; p->Link!=NULL; p=p->Link)
for(q=p->Link; q->Link!=NULL; q=q->Link)
switch(criteria)
{
case 1:
if (strcmp(p->title,q->title)==1)
ChangeNode(&p,&q);
break;
case 2:
if (strcmp(p->singer,q->singer)==1)
ChangeNode(&p,&q);
break;
}
}
void ChangeNode(NodePtr *p, NodePtr *q)
{
NodePtr temp;
temp=(NodePtr)malloc(sizeof(NodeType));
CopyNode(&temp,p);
CopyNode(p,q);
CopyNode(q,&temp);
}
void CopyNode(NodePtr *temp, NodePtr *x)
{
strcpy((*temp)->title,(*x)->title);
strcpy((*temp)->singer,(*x)->singer);
(*temp)->price = (*x)->price;
(*temp)->year = (*x)->year;
}
void Sort(NodePtr *L, int criteria)
{
NodePtr p, q;
for(p=*L; p->Link!=NULL; p=p->Link)
for(q=p->Link; q->Link!=NULL; q=q->Link)
switch(criteria)
{
case 1:
if (strcmp(p->title,q->title)==1)
ChangeNode(&p,&q);
break;
case 2:
if (strcmp(p->singer,q->singer)==1)
ChangeNode(&p,&q);
break;
}
}
void ChangeNode(NodePtr *p, NodePtr *q)
{
NodePtr temp;
temp=(NodePtr)malloc(sizeof(NodeType));
CopyNode(&temp,p);
CopyNode(p,q);
CopyNode(q,&temp);
}
void CopyNode(NodePtr *temp, NodePtr *x)
{
strcpy((*temp)->title,(*x)->title);
strcpy((*temp)->singer,(*x)->singer);
(*temp)->price = (*x)->price;
(*temp)->year = (*x)->year;
}