你如何比较和排序链表中的字符,你不能像这样比较“Smith”>“Andersson”?

struct person {
char name[20];
struct person *nextPerson;
};

.
void createNode(PersonPtr *sPtr, struct person t[]){
  PersonPtr newPtr; /* pointer to new node */
  PersonPtr previousPtr; /* pointer to previus node in  list */
  PersonPtr currentPtr; /* pointer to current node in list */
.
/* loop to find correct location in the list */
while (currentPtr != NULL && t->name > currentPtr->name) { /* this will not sort on name */
  previousPtr = currentPtr; /* walk to... */
  currentPtr = currentPtr->nextPerson; /* ...next node */
}/* end while */

最佳答案

很接近,但不是很接近,你不能只在字符串上使用“>”或“strcmp。
你想要的是:

while (currentPtr != NULL && strcmp(t->name,currentPtr->name) > 0)

关于c - 用C编程语言比较链表中的字符数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1886827/

10-11 21:46