#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_LENGTH 100
#define MAX_WORDS 100000
// 函数原型
int compareStrings(const char *str1, const char *str2);
void swap(char **p1, char **p2);
int main() {
int n;
scanf("%d", &n);
if (n <= 0 || n > MAX_WORDS) {
fprintf(stderr, "Invalid number of words.\n");
return 1;
}
// 动态分配内存
char **words = (char **)malloc(n * sizeof(char *));
if (!words) {
fprintf(stderr, "Memory allocation failed.\n");
return 1;
}
// 读取单词并存储
for (int i = 0; i < n; i++) {
words[i] = (char *)malloc(MAX_WORD_LENGTH * sizeof(char));
if (!words[i]) {
fprintf(stderr, "Memory allocation failed.\n");
return 1;
}
scanf("%s", words[i]);
}
// 冒泡排序
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (compareStrings(words[i], words[j]) > 0) {
swap(&words[i], &words[j]);
}
}
}
// 输出排序后的单词
for (int i = 0; i < n; i++) {
printf("%s\n", words[i]);
free(words[i]); // 释放内存
}
free(words); // 释放内存
return 0;
}
// 用于比较两个字符串的函数
int compareStrings(const char *str1, const char *str2) {
return strcmp(str1, str2);
}
// 用于交换两个字符串指针的函数
void swap(char **p1, char **p2) {
char *temp = *p1;
*p1 = *p2;
*p2 = temp;
}