Answer to Question #214190 in C++ for Ayesha

Question #214190

Write a c++ program that contains


-a function that that compare two singly Linked lists


-a function that swaps the values inside them


-a function that sorts the values inside them in ascending order


1
Expert's answer
2021-07-06T02:08:37-0400
using namespace std;


struct Node 
{
   int data;
   struct Node *next;
};
struct Node* head = NULL;




struct Node_1 
{
   int data_1;
   struct Node_1 *next;
};
struct Node_1* head_1 = NULL;


struct Node_2 
{
   int data_2;
   struct Node_2 *next;
};
struct Node_2* head_2 = NULL;




void InsertData(int NewtData, int NodeNo)
{
   	if(NodeNo==1)	
   	{
		struct Node_1* new_node = (struct Node_1*) malloc(sizeof(struct Node_1));
   		new_node->data_1 = NewtData;
   		new_node->next = head_1;
   		head_1 = new_node;
	}
   	if(NodeNo==2)	
   	{
		struct Node_2* new_node = (struct Node_2*) malloc(sizeof(struct Node_2));
   		new_node->data_2 = NewtData;
   		new_node->next = head_2;
   		head_2 = new_node;
	}
}


void DisplayLinkedList(int NodeNo) 
{
   if(NodeNo==0) 
   	{
   		struct Node* ptr; ptr=head;
  		while (ptr != NULL) 
   		{
      		cout<< ptr->data <<" ";
      		ptr = ptr->next;
   		}
	}


   if(NodeNo==1) 
   	{
   		struct Node_1* ptr; ptr=head_1;
  		while (ptr != NULL) 
   		{
      		cout<< ptr->data_1 <<" ";
      		ptr = ptr->next;
   		}
	}
   
   if(NodeNo==2) 
   	{
   		struct Node_2* ptr; ptr=head_2;
  		while (ptr != NULL) 
   		{
      		cout<< ptr->data_2 <<" ";
      		ptr = ptr->next;
   		}
	}
}


int CompareLinkedLists(void)
{
	int l1=0,l2=0,Flag=0;
	struct Node_1* ptr1; ptr1=head_1;
	struct Node_2* ptr2; ptr2=head_2;
    while (ptr1 != NULL) {l1++;  ptr1 = ptr1->next;}
    while (ptr2 != NULL) {l2++;  ptr2 = ptr2->next;}
    cout<<"\nL1 = "<<l1<<"\t"<<l2;
    ptr1=head_1;	ptr2=head_2;
	if(l1==l2)
    {
    	Flag=0;
  		while (ptr1 != NULL) 
   		{
      		if(ptr1->data_1 == ptr2->data_2)  Flag++;
      		ptr1 = ptr1->next;
      		ptr2 = ptr2->next;
   		}
   		if(Flag==l1) Flag=1;
	}
    else Flag=0;
    return(Flag);
}


void SortLinkdedList(int NodeNo)
{
	int l,l1=0,l2=0;
	struct Node_1* ptr1; ptr1=head_1;
	struct Node_2* ptr2; ptr2=head_2;
    while (ptr1 != NULL) {l1++;  ptr1 = ptr1->next;}
    while (ptr2 != NULL) {l2++;  ptr2 = ptr2->next;}
    if(NodeNo==1) l=l1; else l = l2;
    int temparr[l];
    
    cout<<"\n\nL = "<<l;
	cout<<"\n\nBefore Soting: "; DisplayLinkedList(NodeNo);
    ptr1=head_1;	ptr2=head_2;
	l1=0;	
	if(NodeNo==1) 	while (ptr1 != NULL) {temparr[l1] = ptr1->data_1;  ptr1 = ptr1->next; l1++;}
	else			while (ptr2 != NULL) {temparr[l1] = ptr2->data_2;  ptr2 = ptr2->next; l1++;}


	int n;
    for (int i = 0; i < l; ++i) 
    {
        for (int j = i + 1; j < l; ++j) 
        {
            if (temparr[i] > temparr[j]) 
                {
                    n = temparr[i];
                    temparr[i] = temparr[j];
                    temparr[j] = n;
                }
            }
    }
    ptr1=head_1;	ptr2=head_2;
	for(n=0;n<l;n++) InsertData(temparr[n],NodeNo);	
	cout<<"\n\nAfter Soting: "; 
	for (int i = 0; i < l; ++i) cout<<temparr[i]<<", ";
}




int main() 
{
	int List1[5] = {1, 3, 5, 33, 22};
	int List2[5] = {1, 3, 5, 33, 22};
    int n;
    for(n=0;n<5;n++) {InsertData(List1[n],1);	InsertData(List2[n],2);}
    
    cout<<"\nThe linked list-1 is: ";
    DisplayLinkedList(1);
    cout<<"\nThe linked list-2 is: ";
    DisplayLinkedList(2);
	n = CompareLinkedLists();
	cout<<"\n\nLinked Lists Comparison Status: ";
	if(n==1)	cout<<"The two Linked Lists are identical.";
	else		cout<<"The two Linked Lists are NOT identical.";
	
	SortLinkdedList(1);
    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