#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义单向链表 typedef struct __LinkedList_ts { char value; struct __LinkedList_ts* next; } LinkedList_ts; // 新建链表节点 LinkedList_ts* LinkedList_new(char value) { LinkedList_ts* result = NULL; result = (LinkedList_ts*)malloc(sizeof(LinkedList_ts)); result->value = value; result->next = NULL; return result; } // 添加节点并排序 LinkedList_ts* LinkedList_add(LinkedList_ts* head, char value) { LinkedList_ts* result = NULL; LinkedList_ts* curr = NULL; LinkedList_ts* next = NULL; LinkedList_ts* node = NULL; node = LinkedList_new(value); if (head == NULL) { head = node; } else { for (curr=head; curr!=NULL; curr = curr->next) { if (value >= curr->value) { next = curr->next; if (next == NULL) { curr->next = node; break; } else { if (value <= next->value) { curr->next = node; node->next = next; break; } else {} } } else { node->next = head; head = node; break; } } } result = head; return result; } int main() { char s[1001] = {}; LinkedList_ts* head = NULL; LinkedList_ts* curr = NULL; uint32_t length = 0,i; scanf("%s", s); length = strlen(s); // 便利字符串并使用链表排序 for (i=0; i<length; ++i) { head = LinkedList_add(head, s[i]); } // 输出排序后的链表 for (curr=head; curr!=NULL; curr = curr->next) { printf("%c", curr->value); } return 0; }