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

How to return the values of the char linked lists and store it as a string?

我创建了这个程序,它应该使用 C++ 中的双向链表检查用户以字符形式输入的字符串,但是我卡在了最后一点,我应该将原始单词与反转单词进行比较以查看如果这两个词是否回文,如何将函数display()reverse()的内容存储到一个字符串变量中,以便我可以return值并比较它们? 另外,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;
}

如果要用链表中的字符构建字符串,可以使用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;

您可以编写一个函数,将字符的链接列表作为输入,然后 returns 一个 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;
}

您的代码中存在一些错误。首先,我的提示是您需要制作一个 class/struct 来保存列表的头部和尾部。例如:

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

此外,如您所见,列表节点应该有一个 class,并且每个节点都应该有一个指向下一个节点和前一个节点的指针。不要忘记让第一个节点的 previous 指针指向 NULL,最后一个节点的 next 指针也是如此。我注意到的其他一些事情是您忘记释放 dynamic/heap 内存。使用 '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;
}