Merge Link list

This is a discussion on Merge Link list within the C Programming forums, part of the General Programming Boards category; Hello, Following code works partially. I's trying to merge two lists into one lists so that their nodes in merged ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Merge Link list

    Hello,

    Following code works partially. I's trying to merge two lists into one lists so that their nodes in merged lists are exactly at alternative posistions.

    Please let me know, where I am wrong. merge works fine is boths list have same number of nodes or first node has more nodes. But seems like does not work when second list has more nodes.

    Code:
    # include<stdio.h>
    
    struct node
    {
    	int data;
    	struct node *next;
    };
    
    
    void append(struct node **p,int num)
    {
    	static struct node *temp;
    	
    	if ( *p == NULL )
    	{
    		*p = (struct node *)malloc(sizeof(struct node));
    		temp = *p;
    		(*p)->next = NULL;
    		(*p)->data = num;
    	}
    	else
    	{
    		temp->next =  (struct node *)malloc(sizeof(struct node));
    		temp = temp->next;
    		temp->data = num;
    		temp->next = NULL;
    	}
    }
    
    void display( struct node *p)
    {
    	printf("\n\n");
    	while ( p != NULL )
    	{
    		printf("%d\t",p->data);
    		p = p->next;
    	}
    }
    
    struct node * mergelist(struct node **p,struct node **q)
    {
    	struct node *saveP, *saveQ, *tempP, *tempQ;
    
    	tempP = *p;
    	tempQ = *q;
    
    	while ( tempP && tempQ )
    	{
    		saveP = tempP->next;
    		saveQ = tempQ->next;
    
    		if ( saveP && saveQ )
    		{
    //			if ( tempP != *p )
    			tempP->next = tempQ;
    			tempQ->next = saveP;
    			tempP = saveP->next;
    			saveP->next = saveQ;
    			tempQ = saveQ->next;
    			saveQ->next = tempP;
    		}
    	}
    	/*if ( tempP == 0)
    		tempP = tempQ;
    	else
    		tempQ = tempP;*/
    
    	return *p;
    }
    
    void main()
    {
    	int val;
    	struct node *p,*q,*r;
    
    	p = q = r = NULL;
    
    	printf("\nCreate P list\n\n");
    	scanf("%d",&val);
    
    	while ( val)
    	{
    		append(&p,val);
    		scanf("%d",&val);
    	}
    	display(p);
    
    	printf("\n\nCreate Q list\n");
    	scanf("%d",&val);
    
    	while ( val)
    	{
    		append(&q,val);
    		scanf("%d",&val);
    	}
    	display(q);
    
    	r = mergelist(&p,&q);
    	display(r);
    }

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    When the second one has more nodes and it doesn't work, what exact error messages are you getting, or what does the program do that it shouldn't?

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Program should append remaining nodes as it is as there are no nodes in other list.


    PHP Code:
    Create P list

    1
    3
    5
    7
    0


    1       3       5       7

    Create Q 
    list
    2
    4
    6
    8
    10
    12
    0


    2       4       6       8       10      12

    Merged 
    list
    1       2       3       4       5       6       7       8 

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    it's because you don't check whether the second list is empty or not after the while loop is finished.
    add smt like this just before return:
    Code:
    if (tempQ != NULL)
      tempP = tempQ;
    :wq

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    37

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21