Merge Two Linked-Lists














































Merge Two Linked-Lists



Introduction :

  • We are given two linked-lists, we have to merge them into a single one.

Problem Statement :

  • We have to make a user-interactive program which has following utilities :
    • Option to add a node to Linked-List-1
    • Option to add a node to Linked-List-2
    • Option to Merge the Two linked-lists
    • Option to display Linked-lists (either list-1 or list-2 or Merged-list)
    • Option to exit from program (The program will continue these options until user use this option).

  • Merging of the linked-lists means :
    • The two lists will be merged as per ascending order of their elements.
    • No element will be repeated in the merged list.

  • Try to code in your compiler first ...

Approach :

  • We are adding the elements in list-1 and list-2 only in ascending order.
  • While adding a new element, we are also checking if this element is already present in the list or not. If it is already present, we are not going to add it in the list altogether.

C++ Code :

#include<iostream>
using namespace std;

// Basic Template for Linked-List Node
struct node
{
	int data;
	struct node *next;
}*p, *q, *m;


// Add a New Node to List-1
void add_list1(int number)
{
	struct node* temp = p, *r, *t2;
	r = (struct node*)new(struct node);
	r->data = number;
	if(p==NULL || p->data > number)
	{
		p = r;
		p->next = temp;
	}
	while(temp != NULL)
	{
		if(temp->data<number && (temp->next==NULL || temp->next->data>number))
		{
			t2 = temp->next;
			temp->next = r;
			r->next = t2;
		}
		temp = temp->next;
	}
}


// Add a New Node to List-2
void add_list2(int number)
{
	struct node* temp = q, *r, *t2;
	r = (struct node*)new(struct node);
	r->data = number;
	if(q==NULL || q->data > number)
	{
		q = r;
		q->next = temp;
	}
	while(temp != NULL)
	{
		if(temp->data<number && (temp->next==NULL || temp->next->data>number))
		{
			t2 = temp->next;
			temp->next = r;
			r->next = t2;
		}
		temp = temp->next;
	}
}


/*
This function will merge the two given Linked Lists.
Rules of Mearging are - 
	>> No element will be repeated in the mearged Linked-List
	>> The merged list will be sorted in ascending order

Follows the simple algorithm to mearge two arrays.
*/
void merge_lists()
{
	if(p==NULL && q==NULL)
	{
		cout<<"Lists are Empty !!\n";
		return;
	}
	
	m = NULL;
	struct node *t1=p,*t2=q;
	struct node *z = NULL;
	
	while(t1!=NULL && t2!=NULL)
	{
		if(m == NULL)
		{
			m = (struct node*)new(struct node);
			z = m;
		}
		else
		{
			z->next = (struct node*)new(struct node);
			z = z->next;
		}
		
		if(t1->data < t2->data)
		{
			z->data = t1->data;
			t1 = t1->next;
		}
		else if(t1->data > t2->data)
		{
			z->data = t2->data;
			t2 = t2->next;
		}
		else if(t1->data == t2->data)
		{
			z->data = t1->data;
			t1 = t1->next ; t2 = t2->next;
		}
	}
	
	// if list-2 is smaller in size than list-1
	while(t1 != NULL)
	{
		if(z == NULL){ //if list_2 is entirely empty
			z = (struct node*)new(struct node);
			m = z;
		}
		else{
			z->next = (struct node*)new(struct node);
			z = z->next;
		}
		z->data = t1->data;
		t1 = t1->next;
	}
	
	// if list-1 is smaller in size than list-2
	while(t2 != NULL)
	{
		if(z == NULL){ //if list_1 is entirely empty
			z = (struct node*)new(struct node);
			m = z;
		}
		else{
			z->next = (struct node*)new(struct node);
			z = z->next;
		}
		z->data = t2->data;
		t2 = t2->next;
	}
	
	z->next = NULL; //last link of linked-list must be NULL
}


// Utility function to display List-1 or List-2
// Asks user to display which List (1 or 2)
void disp_list(int num)
{
	struct node* temp;
	
	if(num==1)		temp=p;
	else if(num==2)	temp=q;
	
	if(temp==NULL)
	{
		cout<<"List is Empty !!\n";
		return;
	}
	while(temp != NULL)
	{
		cout<<temp->data<<" ";
		temp = temp->next;
	}
	cout<<"\n";
}


// Utility function to display merged-list
void disp_merge_list()
{
	if(m == NULL)
	{
		cout<<"List is Empty !!\n";
		return;
	}
	struct node *temp = m;
	while(temp != NULL)
	{
		cout<<temp->data<<" ";
		temp = temp->next;
	}
	cout<<"\n";
}


// DRIVER CODE
int main()
{
	p=NULL ; q=NULL ; m=NULL;
	int optn, num;
	
	while(1)
	{
		cout<<"\nOpeartions available :\n";
		cout<<"1. Add node for list_1\n2. Add node for list_2\n";
		cout<<"3. Merge the two lists\n";
		cout<<"4. Display list\n5. Display merged list\n6. Exit\n\n";
		cout<<"Enter your choice : ";
		cin>>optn;
		
		switch(optn)
		{
			case(1):
				cout<<"Enter the number : ";
				cin>>num;
				add_list1(num);
				break;
			case(2):
				cout<<"Enter the number : ";
				cin>>num;
				add_list2(num);
				break;
			case(3):
				merge_lists();
				break;
			case(4):
				cout<<"Which list you want to see ?(1 or 2) : ";
				cin>>num;
				if(num==1 || num==2)	disp_list(num);
				else			cout<<"Invalid list number\n";
				break;
			case(5):
				disp_merge_list();
				break;
			case(6):
				exit(0);
		}
	}
	return 0;
}


Output :

Opeartions available :
1. Add node for list_1
2. Add node for list_2
3. Merge the two lists
4. Display list
5. Display merged list
6. Exit

Enter your choice : 1
Enter the number : 12

Opeartions available :
1. Add node for list_1
2. Add node for list_2
3. Merge the two lists
4. Display list
5. Display merged list
6. Exit

Enter your choice : 1
Enter the number : 13

Opeartions available :
1. Add node for list_1
2. Add node for list_2
3. Merge the two lists
4. Display list
5. Display merged list
6. Exit

Enter your choice : 2
Enter the number : -67

Opeartions available :
1. Add node for list_1
2. Add node for list_2
3. Merge the two lists
4. Display list
5. Display merged list
6. Exit

Enter your choice : 2
Enter the number : 56

Opeartions available :
1. Add node for list_1
2. Add node for list_2
3. Merge the two lists
4. Display list
5. Display merged list
6. Exit

Enter your choice : 4
Which list you want to see ?(1 or 2) : 1
12 13

Opeartions available :
1. Add node for list_1
2. Add node for list_2
3. Merge the two lists
4. Display list
5. Display merged list
6. Exit

Enter your choice : 4
Which list you want to see ?(1 or 2) : 2
-67 56

Opeartions available :
1. Add node for list_1
2. Add node for list_2
3. Merge the two lists
4. Display list
5. Display merged list
6. Exit

Enter your choice : 3

Opeartions available :
1. Add node for list_1
2. Add node for list_2
3. Merge the two lists
4. Display list
5. Display merged list
6. Exit

Enter your choice : 5
-67 12 13 56



Stay connected for more related articles ...
Like the post if you find it worthy ...




More Articles of Keshav Kabra:

Name Views Likes
C++ MLPACK :: CharExtract 670 4
Star Pattern - 3 642 5
Things a Beginner Programmer MUST Do 737 12
Ramanujan Numbers 1908 2
Star Pattern - 4 630 8
Implementation of Stack using Singly Linked-List 2621 10
C++ MLPACK :: Recall 642 7
C++ MLPACK Introduction 834 13
Star Pattern - 1 745 13
C++ MLPACK :: ZCAWhitening 639 4
Factorial using Stack 8893 5
C++ MLPACK :: Installation 2290 7
C++ MLPACK :: DatasetMapper 606 6
C++ MLPACK :: AdaBoost 1256 10
C++ MLPACK :: ImageInfo 541 3
C++ MLPACK :: F1-Score (F1) 1345 7
C++ MLPACK :: MedianImputation 549 5
C++ MLPACK :: Split 1126 7
C++ MLPACK :: Clustering 1043 4
C++ MLPACK :: MeanNormalization 649 6
C++ MLPACK :: SimpleCV 694 8
Postfix Expression Evaluation by Stack 6291 14
C++ Program to Identify People Invited in a Party 1163 3
C++ MLPACK :: ListwiseDeletion 641 5
C++ MLPACK :: ColumnsToBlock 440 2
C++ MLPACK :: Confusion Matrix 1681 7
C++ MLPACK :: Binarize 631 5
Print Statement Without Using Semi-Colon 506 4
C++ MLPACK :: MinMaxScaler 1492 5
C++ MLPACK :: MeanShift 1438 3
C++ MLPACK :: MSE (Mean Squared Error) 2337 6
C++ MLPACK :: Data-Normalization 991 3
Mistakes People do While Learning Programming 626 12
Shift 3 Numbers without using Extra Memory 527 7
C++ MLPACK :: Perceptron 1486 2
C++ MLPACK :: LinearSVM 1141 11
Left-Shift and Right-Shift Operators 584 5
Circular Queue using Array 3786 8
C++ MLPACK :: LinearRegression 2912 15
C++ MLPACK :: NeighborSearch 893 14
C++ MLPACK :: StandardScaler 1455 4
Sorting a Singly Linked-List 642 2
Star Pattern - 7 628 5
C++ MLPACK :: Precision 635 8
C++ MLPACK :: MaxAbsScaler 685 4
C++ MLPACK :: KMeans 2167 4
C++ MLPACK :: NaiveBayesClassifier 1442 4
C++ MLPACK :: NaiveKMeans 491 3
C++ MLPACK :: MeanImputation 525 5
C++ MLPACK :: DBSCAN 3104 3
C++ MLPACK :: Accuracy 590 8
C++ MLPACK :: DecisionStump 687 2
C++ MLPACK :: Ensemble Learning 908 9
C++ MLPACK :: EMST :: DTBRules 452 3
C++ MLPACK :: RangeType 496 2
Reverse a Singly Linked-List 597 6
Star Pattern - 5 (Pascal Triangle) 969 3
C++ MLPACK :: Imputer 763 6
Move Text on Pressing Keys 1132 4
C++ MLPACK :: PCAWhitening 589 3
Star Pattern - 2 587 8
C++ MLPACK :: EMST :: EdgePair 514 3
C++ MLPACK :: Over-Fitting and Under-Fitting 841 7
Pythagorean Triplets 5183 3
Merge Two Linked-Lists 3954 5
Singly Circular Linked-List 562 4
C++ MLPACK :: LogisticRegression 2847 15
C++ MLPACK :: KFoldCV 1082 9
Star Pattern - 6 607 5
C++ MLPACK :: LoadCSV 923 6
Sieve of Eratosthenes 6552 4
C++ MLPACK :: StringEncoding 1109 6
C++ MLPACK :: EMST :: DTBStat 432 3
C++ MLPACK :: SplitByAnyOf 531 4
C++ MLPACK :: GaussianDistribution 541 3
C++ MLPACK :: SoftmaxRegression 955 12
Armstrong Numbers 559 3

Comments

  • meeloun
    1-Feb-2024 01:55:38 PM
    When students compare how much it costs to Essay ghostwriting    http://www.proessay.cn/our_service1.php    in English, they also need to evaluate the writer's ability, after all, their homework is ultimately completed by the writer, so it is necessary to find a suitable organization.