您好,欢迎访问一九零五行业门户网

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

链表是一种类似链式的线性数据结构,其中元素不像数组那样按相邻的方式保存在内存中。在特定的链表中,元素通过指针与下一个元素相连。简单来说,链表是一系列数据容器,我们可以在这些元素中找到一条路径或引用链接到下一个节点。在链表中,有一个头指针作为第一个元素。如果该特定链表的第一个节点为空,则它不指向任何内容或为空。
在数据结构中存在不同类型的链表。
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 - 减少当前索引。
第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_nodereverse(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 <bits/stdc++.h>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;}
outputthe 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 <bits/stdc++.h>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;}
outputkolkata and dhaka between runs train international an is express maitree 13110,
reverse a doubly circular linked listhere in this particular code we have reversed a doubly circular linked list.
example 3的中文翻译为:示例3#include <bits/stdc++.h>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;}
outputcurrent list here present:forward direction data source: 16 10 7 2001 1997backward direction data source: 1997 2001 7 10 16reversed list here present:forward direction data source: 1997 2001 7 10 16backward direction data source: 16 10 7 2001 1997
反转循环链表在这个特定的代码中,我们已经反转了一个循环链表的数据集。
example 4的中文翻译为:示例4#include <bits/stdc++.h>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;}
outputgiven 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 <iostream>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 functionint 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;}
outputgiven linked list is here: b@010ataklokardurreversed linked list is here: r@010udrakolkatab
conclusion在本文中,我们学习了如何反转链表节点中的每个单词。我们在这里构建了c++代码,以展示可能的反转过程,以便您对链表节点的反转有一个广泛的了解。
以上就是将链表节点中的每个单词反转的详细内容。
其它类似信息

推荐信息