Search code examples
c++structdoubly-linked-listfunction-definition

create a function to add a doubly linked list to itself at the end


#include <iostream>
#include <string>
using namespace std;
class Node{
public:
double data;
Node* next;
Node* prev;
};
void insert(Node**,int);
void display(Node*);
void finaloutput(Node*,Node**);
int main()
{
    Node* head=NULL;
    Node** second=NULL;
    insert(&head,1);
    insert(&head,2);
    insert(&head,3);
    insert(&head,4);
    insert(&head,5);
    cout<<"List is"<<endl;
    display(head);
    cout<<endl;
    cout<<"Final output is"<<endl;
    second=&head;
//  Node* second=(*&head);
finaloutput(head,second);
display(head);
}
void insert(Node** headref,int new_data)
{
Node* new_node=new Node();
new_node->data=new_data;
new_node->next=(*headref);
new_node->prev=NULL;
if(*headref!=NULL)
{
(*headref)->prev=new_node;
}
(*headref)=new_node;
}
void display(Node* headref)
{
Node* temp=headref;
while (temp!=NULL)
{
    cout<<temp->data<<" ";
    temp=temp->next;
}

}
void finaloutput(Node* headref,Node** second)
{  
Node ptrbegin;
Node* temp=(headref);
while(temp->next!=NULL)
{
    temp=temp->next;
}

temp->next=(*second);
}

I want to create a function to add a link list to itself at end but in this after temp->next=(*second) this end up showing an infinite loop. I tried to get only value copy of Node* head as second but it didn't help. Please giude me with example code in c++.

the output that i want is like '5 4 3 2 1 5 4 3 2 1'


Solution

  • If the assignment is

    create a function to add a doubly linked list to itself at the end

    then the expected by you output

    5 4 3 2 1 4 5 4 3 2 1
    

    is incorrect because before doubling the list it has the following values

    5 4 3 2 1
    

    So the output of the doubled list will be

    5 4 3 2 1 5 4 3 2 1
    

    You need to add new nodes to the list with copies of currently stored data.

    The data member data of your class Node is declared with the type specifier double

    class Node{
    public:
    double data;
    Node* next;
    Node* prev;
    };
    

    However the function insert deals with data of the type int

    void insert(Node**,int);
    

    So you need to change either the type of the data member or the type of the parameter. There is no great sense to use the class key class instead of the class key struct. Also as the program is a C++ program then you should pass the pointer to the head node by reference in the C++ meaning instead of the C meaning through a pointer to pointer.

    Here is a demonstrative program that shows how the function finaloutput that I renamed in the program to double_list can be written.

    #include <iostream>
    
    struct Node
    {
        int data;
        Node *next;
        Node *prev;
    };
    
    void insert( Node * &head, int data )
    {
        head = new Node { data, head, nullptr };
        if ( head->next ) head->next->prev = head;
    }
    
    std::ostream & display( const Node * head, std::ostream &os = std::cout )
    {
        for ( const Node *current = head; current != nullptr; current = current->next )
        {
            os << current->data << " -> ";
        }
        
        return os << "null";
    }
    
    void double_list( Node * &head )
    {
        if ( head )
        {
            Node *second_head = nullptr;
            
            Node **first = &head;
    
            for ( Node **second = &second_head, *prev = nullptr; 
                  *first;
                  first = &( *first )->next, prev = *second, second = &( *second )->next ) 
            {
                *second = new Node { ( *first )->data, nullptr, prev };
            }
            
            *first = second_head;
        }
    }
    
    int main() 
    {
        Node *head = nullptr;
        
        insert( head, 1 );
        insert( head, 2 );
        insert( head, 3 );
        insert( head, 4 );
        insert( head, 5 );
    
        display( head ) << '\n';
        
        double_list( head );
        
        display( head ) << '\n';
    
        return 0;
    }
    

    The program output is

    5 -> 4 -> 3 -> 2 -> 1 -> null
    5 -> 4 -> 3 -> 2 -> 1 -> 5 -> 4 -> 3 -> 2 -> 1 -> null