Home > Software engineering >  Is there any way I could print these two linked lists side by side, instead of being on top of each
Is there any way I could print these two linked lists side by side, instead of being on top of each

Time:05-05

appendName is a function that inserts a string node at the end of the first linked list.

appendPrice is a function that inserts an integer node at the end of the second linked list.

printName is a function that prints the first linked list (linked list 1)

printPrice is a function that prints the second linked list (linked list 2)

struct nodeName
{
    string Name;
    nodeName *link;
};

void appendName(nodeName** head_name_ref, string new_Name)
{
    nodeName* new_name_node = new nodeName();
    nodeName *last = *head_name_ref;
    new_name_node->Name = new_Name;
    new_name_node->link = NULL;
    if (*head_name_ref == NULL)
    {
        *head_name_ref = new_name_node;
        return;
    }
    while (last->link != NULL)
    {
        last = last->link;
    }
    last->link = new_name_node;
    return;
}

void printName(nodeName *node)
{
    while (node != NULL)
    {
        cout<<" "<<node->Name<<endl;
        node = node->link;
    }
}

struct nodePrice
{
    int Price;
    nodePrice *link;
};

void appendPrice(nodePrice** head_ref, int new_Price)
{
    nodePrice* new_node = new nodePrice();
    nodePrice *last = *head_ref;
    new_node->Price = new_Price;
    new_node->link = NULL;
    if (*head_ref == NULL)
    {
        *head_ref = new_node;
        return;
    }
    while (last->link != NULL)
    {
        last = last->link;
    }
    last->link = new_node;
    return;
}

void printPrice(nodePrice *node)
{
    while (node != NULL)
    {
        cout<<" "<<node->Price<<endl;
        node = node->link;
    }
}

int main()
{
    nodeName* headingNode1 = NULL;
    appendName(&headingNode1, "item#1");
    appendName(&headingNode1, "item#2");

    nodePrice* pricingNode1 = NULL;
    appendPrice(&pricingNode1, 6);
    appendPrice(&pricingNode1, 14);

    cout<<"Created Linked list is"<<endl;
    printName(headingNode1);
    printPrice(pricingNode1);
    return 0;
}

Here is a picture of the output I don't want:

image

CodePudding user response:

You can add another struct to include both lists:

struct nodeList
{
    nodeName* names;
    nodePrice* prices;
};

A new function to print the lists:

void printList(nodeList* list)
{
    while (list->names != NULL && list->prices != NULL)
    {
        std::cout << " " << list->names->Name << std::endl;
        list->names = list->names->link;
        std::cout << " " << list->prices->Price << std::endl;
        list->prices = list->prices->link;
    }
}

And in the main function:

nodeList* list  = new nodeList();
list->names = headingNode1;
list->prices = pricingNode1;

std::cout << "Created Linked list is" << std::endl;
//printName(headingNode1);
//printPrice(pricingNode1);
printList(list);

Output:

Created Linked list is
 item#1
 6
 item#2
 14

CodePudding user response:

void printNameAndPrice(nodeName1 *node1, nodeName2 *node2){

while (node1 != NULL && node2 != NULL)
{
    cout<<" "<<node1->Name<<endl;
    cout<<" "<<node2->Price<<endl;

    node1 = node1->link;
    node2 = node2->link;
}}

Yes you can! you just want to add them in one method.

So the idea is to read them in parallel then update till the end.

Note: you can do the same in appending elements to the Linked List

  • Related