如何返回char链表的值并将其存储为字符串



我创建了这个程序,该程序应该使用C++中的双链表检查用户以字符形式输入的字符串,但我在最后一点遇到了困难,我应该将原始单词与反向单词进行比较,看看这两个单词是否是回文的,如何将函数display()reverse()的内容存储到字符串变量中,以便返回值并进行比较?此外,reverse()功能不显示反向单词

这是我的代码:

#include <iostream>
using namespace std;
class Storage {
public:
char lett;
Storage* next;
Storage* prev;
};
void push(char lett1, Storage** head) {
Storage* n = new Storage();
n->lett = lett1;
n->next = NULL;
if (*head == NULL) {
*head = n;
}
else {
n->next = *head;
*head = n;
}
}
void display(Storage* head, int no) {
Storage* s = head;
while (head != NULL) {
int i = 0;
cout << head->lett;
s = head;
head = head->next;
}
}
void reverse(Storage* tail) {
Storage* t = tail;
//  Storage* original= tail;
while (t != NULL) {
cout << t->lett;
t = t->prev;
}
}
/*
string checkPalindrome() {
string check;
if ()
check == "Yes";
else
check == "No";
return check;
}
*/

int main() {
Storage* head = NULL; Storage* tail = NULL;;
char lett;
int size;
string result;
cout << ":: Palindrome Program ::n" << endl;
cout << "Enter total character: ";
cin >> size;
cout << "Enter character: ";
for (int i=0; i < size; i++) {
cin >> lett;
push(lett, &head);
}
cout << "Your word: "; 
display(head, size);    //compare content of this 
cout << "nReversed word: "; 
reverse(tail);  // with this
/*
result = checkPalindrome();
cout << "Palindrome: " << result << endl;
*/
return 0;
}

您的代码中有一些错误。首先,我的提示是,您需要创建一个类/结构,它将容纳列表的头部和尾部。例如:

class DLList{
public:
NODE *head;
NODE *tail;
};

此外,正如您所看到的,您应该为列表节点有一个类,每个节点都应该有一个指向下一个节点和之前节点的指针。不要忘记将上一个指针指向NULL的第一个节点,以及下一个指针的最后一个节点。我注意到的其他一些事情是,您忘记了释放动态/堆内存。使用"free"或考虑使用智能指针来解决这个问题,这样就不会有任何内存泄漏。最后,尽量避免using namespace std;。由于表现不好,这被认为是一个坏习惯。希望它能帮助你。这是未优化的代码片段。

#include <iostream>
using namespace std;
class Storage {
public:
char lett;
Storage* next;
Storage* prev;
};
void push(char lett1, Storage** head, Storage **tail) {
Storage* n = new Storage();
n->lett = lett1;
n->next = NULL;
n->prev = NULL;
if (*head == NULL) {
*head = n;
*tail = n;
}
else {

n->next = *head;
(* head)->prev = n;
*head = n;
}
}
std::string display(Storage* head) {
Storage* s = head;
std::string org = "";
while (s != NULL) {
org += s->lett;
s = s->next;
}
return org;
}
std::string reverse(Storage* tail) {
Storage* t = tail;
std::string rev = "";
//  Storage* original= tail;
while (t != NULL) {
rev += t->lett;
t = t->prev;
}
return rev;
}

bool checkPalindrome(Storage* head, Storage* tail) {
return display(head) == reverse(tail);
}


int main() {
Storage* head = NULL; Storage* tail = NULL;;
char lett;
int size;
cout << ":: Palindrome Program ::n" << endl;
cout << "Enter total character: ";
cin >> size;
cout << "Enter character: ";
for (int i = 0; i < size; i++) {
cin >> lett;
push(lett, &head,&tail);
}
cout << "Your word: ";
cout<<display(head)<<endl;    //compare content of this 
cout << "nReversed word: ";
cout<<reverse(tail)<<endl;  // with this

cout << "nPalindrome: " << checkPalindrome(head, tail) << endl;

return 0;
}

如果要使用链表中的字符构建字符串,可以使用std::string::operator+=将单个字符连接在一起。

例如,考虑您的display函数:

void display(Storage* head, int no) {
Storage* s = head;
while (head != NULL) {
int i = 0;
cout << head->lett;
s = head;
head = head->next;
}
}

不使用cout << head->lett打印单个字符,只需使用string::operator+=:将该字符连接到结果字符串

// Assume: std::string result
result += head->lett;

您可以编写一个函数,将链接的字符列表作为输入,并返回一个std::string,如下所示:

std::string ToString(const Storage* head) {
std::string result;
// For each node in the linked list
while (...) {
// Append current node's character to the result string
result += currentNode->lett;
}
return result;
}

相关内容

  • 没有找到相关文章

最新更新