题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
#include <stdio.h>
#include <string.h>
struct node {
char value[100];
struct node* next;
};
int main() {
int n;
struct node* header = (struct node*) calloc(sizeof(struct node), 1);
char value[100];
scanf("%d", &n);
for (int i = 0 ; i < n; i++) {
scanf("%s", value);
if (header->next == NULL) {
struct node* indexNode = (struct node*) calloc(sizeof(struct node), 1);
header->next = indexNode;
strcpy(indexNode->value, value);
} else {
struct node* p = header;
while (p != NULL) {
if (p->next == NULL) {
struct node* new = (struct node*) calloc(sizeof(struct node), 1);
new->next = p->next;
p->next = new;
strcpy(new->value, value);
break;
} else if (strcmp(p->next->value, value) >= 0) {
struct node* new = (struct node*) calloc(sizeof(struct node), 1);
new->next = p->next;
p->next = new;
strcpy(new->value, value);
break;
} else {
p = p->next;
}
}
}
}
struct node* p = header;
while (p->next != NULL) {
printf("%s\n", p->next->value);
p = p->next;
}
return 0;
}
这题的思路我的做法跟之前哪个合并索引算法是一样的,没想到有什么其他更好的办法
