Answer to Question #233666 in C++ for Richard

Question #233666
Add a function to task 1 which accepts a linear linked list List and converts it to a circular linked
list.
Where List is a pointer to the front of the list.
Prototype of the function is as following
Node *Convert( Node *List)
1
Expert's answer
2021-09-06T05:29:04-0400
#include<stdlib.h>
#include <iostream>
 using namespace std;


struct Node {
    int data;
    struct Node* next;
};
 


struct Node* Convert(struct Node* list)
{
    
    struct Node* startNode = list;
 
    
    while (list->next != NULL)
        list = list->next;
         
   
    list->next = startNode;
    return startNode;
}
 
void insert(struct Node** head, int data)
{
    
    struct Node* tempNode = (struct Node*)malloc
                          (sizeof(struct Node));
 
   
    tempNode->data = data;
 
    
    tempNode->next = (*head);
 
    
    (*head) = tempNode;
}
 


void printList(struct Node* head)
{
    struct Node* start = head;
 
    while (head->next != start) {
        cout<<"  "<<head->data;
        head = head->next;
    }
     
    
    cout<<head->data<<" \t";
}
 


int main()
{
    
    struct Node* head = NULL;
 
    
    insert(&head, 1);
    insert(&head, 2);
    insert(&head, 3);
    insert(&head, 4);
    insert(&head, 5);
 
    
    Convert(head);
 
    cout<<"Printing list: \n";
    printList(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
APPROVED BY CLIENTS