Answer to Question #278633 in C++ for Huma

Question #278633

Using singly and doubly linked lists do the following :




(1)create a class that implements the following :




a->abdullah->ali




b->ball->bill




c->cat->cute




d->dark->dog




Note:




a, b, c and d are connected using doubly links and rest of the links are all singly links.





(2)no global declarations are allowed




(3)run test the code in main




(4)make a menu

1
Expert's answer
2021-12-13T00:02:35-0500
#include <iostream>
using namespace std;


struct node {
    char data;
    node* next;
};


node* add(char data)
{
    node* newnode = new node;
    newnode->data = data;
    newnode->next = NULL;
    return newnode;
}


node* string_to_linked_list(string text, node* head)
{
    head = add(text[0]);
    node* curr = head;


    for (int i = 1; i < text.size(); i++) {
        curr->next = add(text[i]);
        curr = curr->next;
    }
    return head;
}


void print(node* head)
{
    node* curr = head;
    while (curr != NULL) {
        cout << curr->data << " -> ";
        curr = curr->next;
    }
}


int main()
{


    string text = "ABDULLAH";


    node* head = NULL;
    head = string_to_linked_list(text, head);


    print(head);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog