Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Two linked lists are given as
A: 1, 2, 3, 4, 5, 6
B: 7, 8, 9, 10, 11, 12
how to create a third linked list so that it is in the following order.
C: 1,7,2,8,…..,6,12
thanks in advance
regards
Posted

Create a new list and add elements alternately from A and B. What exactly is your problem?
 
Share this answer
 
Comments
shivani 2013 6-Mar-12 5:06am    
how to add the elements?
Richard MacCutchan 6-Mar-12 5:23am    
I have no idea, since you have not shown us your implementation of the linked list.
Albert Holguin 6-Mar-12 11:25am    
+5, if he had provided any details about what linked lists he was using, OP could get a clearer response, but since he didn't, this is really as accurate as you can get.
It should be work:

Java
public class LinkedList
{

	public static void main( String[] args )
	{
		int[] A = { 1, 2, 3, 4, 5, 6 };
		int[] B = { 7, 8, 9, 10, 11, 12 };

		int[] C = new int[ A.length + B.length ];

		for ( int index = 0; index < C.length / 2; index += 2 )
		{
			C[ index ] = A[ index ];
			C[ index + 1 ] = B[ index ];
		}

		for ( int index = 0; index < C.length; index++ )
		{
			System.out.printf( "%d, ", C[ index ] );
		}

	}

}
 
Share this answer
 
check the below linklist in C. It fills the third linklist in alternate order of first two link list

#include<iostream>
#include<conio.h>

using namespace std;

typedef struct node
{
	int data;	// will store information
	node *next;	// the reference to the next node
};
 
//Show the link list
void printLinkList(node *head)
{
	while(head != NULL)
	{
		printf("%d ",head->data);
		head = head->next;
	}
}

int main()
{
	node *head1 = NULL;	//empty linked list
	node *head2 = NULL;	//empty linked list
	node *head3 = NULL;	//empty linked list
	node *traverse = NULL;
	node *traverse1 = NULL;
	node *traverse2 = NULL;
	node *traverse3 = NULL;
	node *temp = NULL;


	int counter = 0;
	int LL1[6] = {1,2,3,4,5,6};
	int LL2[6] = {7,8,9,10,11,12};
	
	//Insert Data in 1 st link list
	while(counter < 6)
	{
		if(head1 == NULL)
		{
			node *temp;						
			temp = (node*)malloc(sizeof(node));	
			temp->data = LL1[counter++];			
			temp->next = NULL;			
			head1 = traverse = temp;				
		}
		else
		{
			temp = NULL;						
			temp = (node*)malloc(sizeof(node));	
			temp->data = LL1[counter++];			
			temp->next = NULL;			
			traverse->next = temp;				
			traverse = traverse->next;
		}
	}

	counter = 0;
	traverse = NULL;
	//Insert Data in 2nd link list
	while(counter < 6)
	{
		if(head2 == NULL)
		{
			node *temp;						
			temp = (node*)malloc(sizeof(node));	
			temp->data = LL2[counter++];			
			temp->next = NULL;			
			head2 = traverse = temp;				
		}
		else
		{
			temp = NULL;						
			temp = (node*)malloc(sizeof(node));	
			temp->data = LL2[counter++];			
			temp->next = NULL;			
			traverse->next = temp;				
			traverse = traverse->next;
		}
	}

	//Now insert in the third link list
	counter = 0;
	traverse = NULL;
	traverse1 = head1;
	traverse2 = head2;
	while(traverse1 != NULL && traverse2 != NULL)
	{
		if(head3 == NULL && traverse1 != NULL)
		{
			temp = NULL;						
			temp = (node*)malloc(sizeof(node));	
			temp->data = traverse1->data;			
			temp->next = NULL;			
			head3 = traverse3 = temp;
			traverse1 = traverse1->next;
		}
		else if(traverse1 != NULL)
		{
			temp = NULL;						
			temp = (node*)malloc(sizeof(node));	
			temp->data = traverse1->data;			
			temp->next = NULL;			
			traverse3->next = temp;
			traverse1 = traverse1->next;
			traverse3 = traverse3->next;
		}

		if(traverse2 != NULL)
		{
			temp = NULL;						
			temp = (node*)malloc(sizeof(node));	
			temp->data = traverse2->data;			
			temp->next = NULL;			
			traverse3->next = temp;
			traverse2 = traverse2->next;
			traverse3 = traverse3->next;
		}
	}
	printLinkList(head1);
	printf("\n");
	printLinkList(head2);
	printf("\n");
	printLinkList(head3);
	printf("\n");
	return 0;
}
</conio.h></iostream>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900