问题描述
如何在C中的函数按名称进行排序链表?
结构REC {
焦炭名[20];
INT NR;
结构体REC * NEXTREC;
};
typedef结构REC录制; / *代名词结构REC * /
typedef的拍摄* RecPtr; / *代名词指针* /无效SortLinkedList(RecPtr *特征码,INT p_size); / * *原型/
诠释主(){
RecPtr startPtr = NULL;
/ *填充UPP链接列表...大小=在列表*节点NR /
SortLinkedList(安培; startPtr,大小);
}无效SortLinkedList(RecPtr *特征码,诠释p_size){
INT I,J;
RecPtr tempPtr;
RecPtr currentPtr;
RecPtr nextPtr;
currentPtr = *特征码;
nextPtr = currentPtr-> NEXTREC;
为(J = 0; J&下; = p_size; J ++){/ *循环为节点NR * /
对于(I = 0; I&下; = p_size -1;我++){/ *循环一比NR节点更少* /
如果(的strcmp(currentPtr->名称,nextPtr->名称)小于0){/ *它小于... * /
tempPtr = currentPtr; / * ...交换与温度* /
currentPtr = nextPtr; / *但这种排序doesn'nt工作* /
nextPtr = tempPtr;
}
currentPtr = nextPtr;
currentPtr = currentPtr-> NEXTREC;
}
}
}
这个问题似乎是,你只是操纵指针,而不是对象本身。当你解决一个链表,需要的话必须打破链接和重新制定的。
例如,在你的情况下,有三通并且它们具有如在括号中指定要修改
prevPtr-> NEXTREC(这需要改变为指向nextPtr代替currentPtr)
currentPtr-> NEXTREC(这需要更改为指向nextPtr->的NEXTREC代替nextPtr)
nextPtr-> NEXTREC(这需要改变为指向currentPtr)
您一定需要有一个prevPtr并跟踪它在你的计划。
NEXTREC NEXTREC NEXTREC
prevPtr --------------> currentPtr -------------------------> nextPtr --------------------------->(nextPtr-> NEXTREC)
需要改变,以
NEXTREC NEXTREC NEXTREC
prevPtr ----------- ------------> nextPtr ------------------ > currentPtr ------------------->(nextPtr-> NEXTREC)
How do you sort a linked list by name in a function in C?
struct rec{
char name[20];
int nr;
struct rec *nextRec;
};
typedef struct rec Rec; /* synonym for struct rec */
typedef Rec *RecPtr; /* synonym for pointer */
void SortLinkedList(RecPtr *sPtr, int p_size);/* prototype */
int main() {
RecPtr startPtr = NULL;
/* filling upp the linked list... size = nr of nodes in list */
SortLinkedList(&startPtr, size);
}
void SortLinkedList(RecPtr *sPtr, int p_size){
int i, j;
RecPtr tempPtr;
RecPtr currentPtr;
RecPtr nextPtr;
currentPtr = *sPtr;
nextPtr = currentPtr->nextRec;
for( j = 0; j <= p_size; j++) { /* loop for nr of nodes */
for(i = 0; i <= p_size -1 ; i++) { /* loop for one less than nr of nodes */
if(strcmp(currentPtr->name, nextPtr->name) < 0) { /* it less than ...*/
tempPtr = currentPtr;/* ...swap with temp */
currentPtr = nextPtr; /* but this sorting doesn'nt work */
nextPtr = tempPtr;
}
currentPtr = nextPtr;
currentPtr = currentPtr->nextRec;
}
}
}
The problem seems to be that you are just manipulating the pointers and not the objects themselves. When you sort a linked list, you necessary have to break links and re-make them.
For example, in your case there are three links and they have to be modified as specified in the brackets.
prevPtr->nextRec (This needs to be changed to point to nextPtr instead of currentPtr)currentPtr->nextRec (This needs to be changed to point to nextPtr->nextRec instead of nextPtr)nextPtr->nextRec (This needs to be changed to point to currentPtr)
You necessarily need to have a prevPtr and keep track of it in your program.
nextRec nextRec nextRec
prevPtr -------------->currentPtr ------------------------->nextPtr---------------------------> (nextPtr->nextRec)
Needs to be changed to
nextRec nextRec nextRec
prevPtr ----------------------->nextPtr ------------------> currentPtr-------------------> (nextPtr->nextRec)
这篇关于手动排序在C编程语言中链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!