题解 | 字符串排序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MaxSize 1000

typedef struct LListNode{
    char str[MaxSize];
}LListNopde, *LList;
void sort_struct(LList l,int n){//直接插入排序思想
    int i,j;
    char temp[1000] ;
    for(i = 1;i <= n;i++){
        strcpy(temp, (l + i)->str);
        j = i - 1;
        while(j >= 0 && (strcmp(temp,(l + j)->str)) < 0){
            strcpy((l+j+1)->str, (l+j)->str);
            j--;
        }
        strcpy((l + j + 1)->str,temp);

    }
}
int main() {
    int n,i;
    scanf("%d",&n);
    getchar();//清除掉缓冲区的'\n'
    LList l;
    l = (LListNopde *)malloc(sizeof(LListNopde) * n);
    for(i = 0;i < n;i++){
        fgets((l + i)->str,sizeof((l + i)->str),stdin);//此处fgets读入的str以'\n'结尾
    }
    sort_struct(l,  n - 1);//排序函数
    for( i = 0;i < n;i++){
       printf("%s",(l + i)->str); 
    }
    
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 14:10
点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
06-26 22:20
门头沟学院 Java
码农索隆:让你把简历发给她,她说一些套话,然后让你加一个人,说这个人给你改简历,然后开始卖课
我的求职精神状态
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务