0

0

将链表节点中的每个单词反转

王林

王林

发布时间:2023-09-01 10:25:03

|

927人浏览过

|

来源于tutorialspoint

转载

将链表节点中的每个单词反转

链表是一种类似链式的线性数据结构,其中元素不像数组那样按相邻的方式保存在内存中。在特定的链表中,元素通过指针与下一个元素相连。简单来说,链表是一系列数据容器,我们可以在这些元素中找到一条路径或引用链接到下一个节点。在链表中,有一个头指针作为第一个元素。如果该特定链表的第一个节点为空,则它不指向任何内容或为空。

在数据结构中存在不同类型的链表。

  • Singly Linked list − It is a basic type of linked list present in data structure, where every node contains some data with a pointer of the same data type for the next node. Here for this linked list both the time complexity and auxiliary space is O(n).

  • 双向链表 − 它是一个复杂的双向链表,其中包含一个指针作为前一个节点的序列。这种类型的链表包含三个不同的部分:数据源、指针和下一个节点。通过这种链表,我们可以以反向的方式遍历整个列表。

  • Circular Linked List − In a circular linked list the first node pointer indicated by the last node of the list. It means, the list has no start and no ending point. To traverse a circular linked list, the user can start from any node and traverse the list in forward or backward direction as their wish.

  • 双向循环链表 - 这是一个双向循环链表,它包含了前一个节点和后一个节点的指针。它的第一个节点的前一个节点不包含空值。

在本文中,我们将为上述提到的链表构建一些代码,通过这些代码,我们将学习如何在C++环境中反转链表节点中的每个单词。

Algorithm to reverse each word present in a linked list node

  • 第一步 - 声明一个临时数组。

  • Step 2 − Traverse a linked list.

  • Step 3 − If the current element is an alphabet then store the element.

  • Step 4 − Else, increase node by 1 pointer.

  • 第五步 - 再次从头部遍历。

  • Step 6 − If the current element is alphabet then copy it to the last element.

  • 步骤 7 - 减少当前索引。

    奇布塔
    奇布塔

    基于AI生成技术的一站式有声绘本创作平台

    下载
  • 第8步 - 必须进行迭代。

  • 第9步 - 否则,将其增加一。

将链表节点中的每个单词反转的语法

insertEnd(head, new_node)
Declare last
   if head == NULL then
      new_node->next = new_node->prev = new_node
      head = new_node
      return
   last = head->prev
   new_node->next = head
   head->prev = new_node
   new_node->prev = last
   last->next = new_node
reverse(head)
   Initialize new_head = NULL
   Declare last
   last = head->prev
   Initialize curr = last, prev
   while curr->prev != last
      prev = curr->prev
      insertEnd(&new_head, curr)
      curr = prev
   insertEnd(&new_head, curr)
return new_head

跟随的方法:

  • Approach 1 − Reverse each word present in a linked list

  • Approach 2 − Reverse the whole sentence present in a linked list.

  • Approach 3 − Reverse a doubly circular linked list.

  • Approach 4 − Reverse a circular linked list.

  • 途径5 − 在不影响特殊字符的情况下反转链表。

Reverse each word present in a linked list by using C++

Here in this particular C++ build code we have reversed each word present in a linked list.

Example 1

的中文翻译为:

示例 1

#include 
using namespace std;
struct Node {
   string c;
   struct Node* next;
};
struct Node* newNode(string c){
   Node* temp = new Node;
   temp->c = c;
   temp->next = NULL;
   return temp;
};
void reverse_word(string& str){
   reverse(str.begin(), str.end());
}
void reverse(struct Node* head){
   struct Node* ptr = head;
   while (ptr != NULL) {
      reverse_word(ptr->c);
      ptr = ptr->next;
   }
}
void printList(struct Node* head){
   while (head != NULL) {
      cout << head->c << " ";
      head = head->next;
   }
}
int main(){
   Node* head = newNode("Train number 13109");
   head->next = newNode("Maitree Express");
   head->next->next = newNode("is an international train");
   head->next->next->next = newNode("runs between");
   head->next->next->next->next = newNode("Kolkata");
   head->next->next->next->next->next = newNode("and");
   head->next->next->next->next->next->next = newNode("Dhaka");
   cout << "The list here present before reverse: \n";
   printList(head);
   reverse(head);
   cout << "\n\nList after reverse we can see like: \n";
   printList(head);
   return 0;
}

Output

The list here present before reverse: 
Train number 13109 Maitree Express is an international train runs between Kolkata and Dhaka 

List after reverse we can see like: 
90131 rebmun niarT sserpxE eertiaM niart lanoitanretni na si neewteb snur atakloK dna akahD 

反转链表中的整个句子

在这个特定的代码中,我们已经将链表中的整个句子进行了反转。

#include 
using namespace std;
string reverseString(string str){
   reverse(str.begin(), str.end());
   str.insert(str.end(), ' ');
   int n = str.length();
   int j = 0;
      for (int i = 0; i < n; i++) {
      if (str[i] == ' ') {
         reverse(str.begin() + j,
         str.begin() + i);
         j = i + 1;
      }
   }
   str.pop_back();
   return str;
}
int main(){
   string str = "13110, Maitree Express Is An International Train Runs Between Dhaka And Kolkata";
   string rev = reverseString(str);
   cout << rev;
   return 0;
}

Output

Kolkata And Dhaka Between Runs Train International An Is Express Maitree 13110,

Reverse a doubly circular linked list

Here in this particular code we have reversed a doubly circular linked list.

Example 3

的中文翻译为:

示例3

#include 
using namespace std;
struct Node {
   int data;
   Node *next, *prev;
};
Node* getNode(int data){
   Node* newNode = (Node*)malloc(sizeof(Node));
   newNode->data = data;
   return newNode;
}
void insertEnd(Node** head, Node* new_node) {
   if (*head == NULL) {
      new_node->next = new_node->prev = new_node;
      *head = new_node;
      return;
   }
   Node* last = (*head)->prev;
   new_node->next = *head;
   (*head)->prev = new_node;
   new_node->prev = last;
   last->next = new_node;
}
Node* reverse(Node* head) {
   if (!head)
   return NULL;
   Node* new_head = NULL;
   Node* last = head->prev;
   Node *curr = last, *prev;
   while (curr->prev != last) {
      prev = curr->prev;
      insertEnd(&new_head, curr);
      curr = prev;
   }
   insertEnd(&new_head, curr);
   return new_head;
}
void display(Node* head){
   if (!head)
   return;
   Node* temp = head;
   cout << "Forward direction data source: ";
   while (temp->next != head) {
      cout << temp->data << " ";
      temp = temp->next;
   }
   cout << temp->data;
   Node* last = head->prev;
   temp = last;
   cout << "\nBackward direction data source: ";
   while (temp->prev != last) {
      cout << temp->data << " ";
      temp = temp->prev;
   }
   cout << temp->data;
}
int main(){
   Node* head = NULL;
   insertEnd(&head, getNode(16));
   insertEnd(&head, getNode(10));
   insertEnd(&head, getNode(07));
   insertEnd(&head, getNode(2001));
   insertEnd(&head, getNode(1997));
   cout << "Current list here present:\n";
   display(head);
   head = reverse(head);
   cout << "\n\nReversed list here present:\n";
   display(head);
   return 0;
}

Output

Current list here present:
Forward direction data source: 16 10 7 2001 1997
Backward direction data source: 1997 2001 7 10 16

Reversed list here present:
Forward direction data source: 1997 2001 7 10 16
Backward direction data source: 16 10 7 2001 1997

反转循环链表

在这个特定的代码中,我们已经反转了一个循环链表的数据集。

Example 4

的中文翻译为:

示例4

#include 
using namespace std;
struct Node {
   int data;
   Node* next;
};
Node* getNode(int data){
   Node* newNode = new Node;
   newNode->data = data;
   newNode->next = NULL;
   return newNode;
}
void reverse(Node** head_ref){
   if (*head_ref == NULL)
   return;
   Node* prev = NULL;
   Node* current = *head_ref;
   Node* next;
   do {
      next = current->next;
      current->next = prev;
      prev = current;
      current = next;
   } while (current != (*head_ref));
   (*head_ref)->next = prev;
   *head_ref = prev;
}
void printList(Node* head){
   if (head == NULL)
   return;
   Node* temp = head;
   do {
      cout << temp->data << " ";
      temp = temp->next;
   } while (temp != head);
}
int main(){
   Node* head = getNode(10);
   head->next = getNode(16);
   head->next->next = getNode(07);
   head->next->next->next = getNode(2022);
   head->next->next->next->next = head;
   cout << "Given circular linked list is here: ";
   printList(head);
   reverse(&head);
   cout << "\nReversed circular linked list after method: ";
   printList(head);
   return 0;
}

Output

Given circular linked list is here: 10 16 7 2022 
Reversed circular linked list after method: 2022 7 16 10 

反转一个链表而不影响特殊字符

Here in this particular code we have reversed the data set of linked list without affecting special characters.

Example 5

#include 
using namespace std;
struct Node {
   char data;
   struct Node* next;
};
void reverse(struct Node** head_ref, int size){
   struct Node* current = *head_ref;
   char TEMP_ARR[size];
   int i = 0;
   while (current != NULL) {
      if ((current->data >= 97 && current->data <= 122) ||
      (current->data >= 65 && current->data <= 90)) {
         TEMP_ARR[i++] = current->data;
         current = current->next;
      }
      else
      current = current->next;
   }
   current = *head_ref;
   while (current != NULL) {
      if ((current->data >= 97 && current->data <= 122) ||
      (current->data >= 65 && current->data <= 90)) {
         current->data = TEMP_ARR[--i];
         current = current->next;
      }
      else
      current = current->next;
   }
}
void push(struct Node** head_ref, char new_data){
   struct Node* new_node = new Node();
   new_node->data = new_data;
   new_node->next = (*head_ref);
   (*head_ref) = new_node;
}
void printList(struct Node* head){
   struct Node* temp = head;
   while (temp != NULL) {
      cout << temp->data;
      temp = temp->next;
   }
}
// Driver program to test above function
int main() {
   struct Node* head = NULL;
   push(&head, 'R');
   push(&head, 'U');
   push(&head, 'D');
   push(&head, 'R');
   push(&head, 'A');
   push(&head, 'K');
   push(&head, 'O');
   push(&head, 'L');
   push(&head, 'K');
   push(&head, 'A');
   push(&head, 'T');
   push(&head, 'A');
   push(&head, '0');
   push(&head, '1');
   push(&head, '0');
   push(&head, '@');
   cout << "Given linked list is here: ";
   printList(head);
   reverse(&head, 13);
   cout << "\nReversed Linked list is here: ";
   printList(head);
   return 0;
}

Output

Given linked list is here: B@010ATAKLOKARDUR
Reversed Linked list is here: R@010UDRAKOLKATAB

Conclusion

在本文中,我们学习了如何反转链表节点中的每个单词。我们在这里构建了C++代码,以展示可能的反转过程,以便您对链表节点的反转有一个广泛的了解。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

18

2026.01.28

包子漫画在线官方入口大全
包子漫画在线官方入口大全

本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。

4

2026.01.28

ao3中文版官网地址大全
ao3中文版官网地址大全

AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。

15

2026.01.28

php怎么写接口教程
php怎么写接口教程

本合集涵盖PHP接口开发基础、RESTful API设计、数据交互与安全处理等实用教程,助你快速掌握PHP接口编写技巧。阅读专题下面的文章了解更多详细内容。

1

2026.01.28

php中文乱码如何解决
php中文乱码如何解决

本文整理了php中文乱码如何解决及解决方法,阅读节专题下面的文章了解更多详细内容。

3

2026.01.28

Java 消息队列与异步架构实战
Java 消息队列与异步架构实战

本专题系统讲解 Java 在消息队列与异步系统架构中的核心应用,涵盖消息队列基本原理、Kafka 与 RabbitMQ 的使用场景对比、生产者与消费者模型、消息可靠性与顺序性保障、重复消费与幂等处理,以及在高并发系统中的异步解耦设计。通过实战案例,帮助学习者掌握 使用 Java 构建高吞吐、高可靠异步消息系统的完整思路。

4

2026.01.28

Python 自然语言处理(NLP)基础与实战
Python 自然语言处理(NLP)基础与实战

本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。

23

2026.01.27

拼多多赚钱的5种方法 拼多多赚钱的5种方法
拼多多赚钱的5种方法 拼多多赚钱的5种方法

在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。

122

2026.01.26

edge浏览器怎样设置主页 edge浏览器自定义设置教程
edge浏览器怎样设置主页 edge浏览器自定义设置教程

在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。

51

2026.01.26

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Swoft2.x速学之http api篇课程
Swoft2.x速学之http api篇课程

共16课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号