2025年单向链表逆序输出c语言(如何实现一个高效的单向链表逆序输出)

单向链表逆序输出c语言(如何实现一个高效的单向链表逆序输出)方法 1 nbsp nbsp nbsp nbsp nbsp include stdio h include string h void reverse char s nbsp nbsp int n 0 nbsp nbsp int m strlen s 1 nbsp nbsp while n string h stdio h




方法1:

        

#include<stdio.h>
#include<string.h>

void reverse(char *s){
    int n = 0;
    int m = strlen(s) - 1;
    while(n<m){
        char tmp = s[n];
        s[n] = s[m];
        s[m] = tmp;
        n++;
        m--;
    }
}

int main(){
    char s[] = "Hello world";
    reverse(s);
    printf("%s ",s);
    return 0;
}
方法2:

#include<stdio.h>
#include<string.h>

void reverse(char *s){
    int i;
    for(i=0;i<strlen(s)/2;i++){//循环次数不超过字符串长度的二分之一,否则逆序完成再逆序等于没有变化
        char tmp = s[i];
        s[i] = s[strlen(s) - i - 1];
        s[strlen(s) - i - 1] = tmp;
    }
}

int main(){
    char s[] = "Hello world";
    reverse(s);
    printf("%s ",s);
    return 0;
}
运行结果:

                 
———————————————————————————————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/abcd123hhg/article/details/132239892

编程小号
上一篇 2025-04-22 15:21
下一篇 2025-04-24 19:11

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/53471.html