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

Time:03-11

I created this program that should check the string entered by user in form of characters using doubly linked lists in C , however I got stuck at the last point in which I should compare the original word with the reversed one to see if the two words are palindrome or not, how to store the content of function display() and reverse() to a string variable so that I can return the value and compare them? Also, reverse() function doesn't display the reversed word

This is my code:

#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;
}

CodePudding user response:

You have some bugs in your code. First of all my tip is that you need to make a class/struct which will hold the head and tail of your list. For example:

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

Also, as you can see you should have a class for your list nodes, and every node should have a pointer to the next node, and to the node before. Don't forget to make the first node previous pointer to point to NULL, and also the last nodes next pointer. Some other things I noticed is that you forgot to deallocate the dynamic/heap memory. Fix that with using 'free' or consider using smart pointers, so you don't have any memory leaks. At the end, try to avoid using namespace std;. It is considered a bad habit, due to bad performance. Hope it helped you. Here is the not optimized code snippet.

#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;
}

CodePudding user response:

If you want to build a string with the characters in the linked list, you can use the std::string::operator = to concatenate the single characters together.

For instance, considering your display function:

void display(Storage* head, int no) {
    Storage* s = head;

    while (head != NULL) {
        int i = 0;
        cout << head->lett;
        s = head;
        head = head->next;
    }
}

instead of using cout << head->lett to print a single character, just concatenate that character to the result string using string::operator =:

// Assume: std::string result

result  = head->lett;

You could write a function that takes the linked list of characters as input, and returns a std::string, along these lines:

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;
}
  • Related