编程友友看过来

用双向链表实现字符串逆序输出的代码

以下是一个用C语言编写的双向链表,将字符串逆序输出的程序的示例。\n\n```\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\ntypedef struct node {\n    char data;\n    struct node* next;\n    struct node* prev;\n} Node;\n\nvoid insert(Node* head, char data) {\n    Node* new_node = (Node*)malloc(sizeof(Node));\n    new_node->data = data;\n    new_node->next = head->next;\n    new_node->prev = head;\n    head->next->prev = new_node;\n    head->next = new_node;\n}\n\nvoid reverse(Node* head) {\n    Node *current = head->next, *temp;\n    while (current != head) {\n        temp = current->next;\n        current->next = current->prev;\n        current->prev = temp;\n        current = temp;\n    }\n    temp = head->next;\n    head->next = head->prev;\n    head->prev = temp;\n}\n\nvoid print(Node* head) {\n    Node* current = head->next;\n    while (current != head) {\n        printf(\c\ current->data);\n        current = current->next;\n    }\n    printf(\n\}\n\nint main() {\n    char* str = \hello world\    int len = strlen(str);\n    Node* head = (Node*)malloc(sizeof(Node));\n    head->next = head;\n    head->prev = head;\n    for (int i = len - 1; i >= 0; i--) {\n        insert(head, str[i]);\n    }\n    printf(\Original: %s\\n\ str);\n    printf(\Reversed: \    reverse(head);\n    print(head);\n    return 0;\n}\n```\n\n此程序使用双向链表来存储和操作字符串,将字符串中的字符倒序插入到链表中,然后翻转链表并输出其中的字符,从而实现将字符串逆序输出的功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值