Thread: doubt in recursion

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    18

    doubt in recursion

    I have a doubt in a question on "Merge 2 sorted linked lists" .
    I tried really hard before posting, i tested on paper many times - it comes out correct, but there is something wrong with my code.

    Basically, i am not getting the recursion right.
    Also, is there some software/tool where i can see the interiors of the stack when recursion is going on?

    My code is as follows
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    typedef struct node
    {
        int data;
        struct node* link;
    }Node;
    Node* start1;
    Node* start2;
    Node* start;
    Node* node;
    Node* result;
    
    int main(void)
    {
        printf("\nEnter the elements of the first ll , Enter -1 to stop entering data");
        start1=form();
        printf("\nEnter the elements of the second ll , Enter -1 to stop entering data");
        start2=form();
    
        printf("\nLinked list 1 contents are ");
        display(start1);
        printf("\nLinked list 2 contents are ");
        display(start2);
    
        start=NULL;
        merge(start1,start2);
        display(start);
    
        printf("\nThe value stored in result = %d",result->data);
    
        getch();
        return 0;
    }
    
    int merge(Node* node1, Node* node2)
    {
      if(node1==NULL)
        return node2;
      if(node2==NULL)
        return node1;
    
      if(node1!=NULL &&  node2!=NULL)
      {
       if(node1->data <= node2->data)
       {
        if(start == NULL)
        start = node1;
        result=node1;
        result->link = merge(node1->link,node2);
       }
    
    
       else
       {
        if(start == NULL)
        start=node2;
        result=node2;
        result->link = merge(node1,node2->link);
       }
    
       return result;
      }
      //return result;
    }
    
    
    int display(Node* node)
    {
        while(node)
        {
            printf("%d ",node->data);
            node = node->link;
        }
    }
    
    int form()
    {
        int num=0;
        Node* ptr;
        Node* last;
        Node* start=NULL;
    
        start=NULL;
    
        while(1)
        {
            scanf("%d",&num);
            if(num == -1)
            {
                printf("Linked list formation over ");
                return start;
            }
            else
            {
                ptr=(Node*)malloc(sizeof(Node));
                ptr->data = num;
                ptr->link = NULL;
    
                if(start == NULL)
                {
                    start=ptr;
                    last=ptr;
                }
                else
                {
                        last->link = ptr;
                        last=ptr;
                }
            }
        }
    }
    Thanks
    Aftab
    Last edited by Salem; 08-17-2011 at 12:53 AM. Reason: removed bold

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    1. Global variables are bad, get rid of them.
    2. You have start declared locally and globally, that is bad. Additionally, you pass start1 and start2 to your function but you have them declared globally.
    3. You have been told before, you do not need to cast the return of malloc.
    4. You should always check the return of malloc to ensure it worked.
    5. Forget about recursion for now, get the program working using a loop, then adjust to a recursive solution.

    EDIT: Someone nicer than me will probably debug what you have, however this code is too broke for me to care about.
    Last edited by AndrewHunter; 08-17-2011 at 01:10 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    18
    Having a look at the merge function alone would be sufficient to point out my mistake.

    I have written int merge(),
    I have corrected that to Node* merge, still doesnt work

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    18
    @AndrewHunter, sir, i dint get that part about not having to cast the return type of malloc.
    I need a pointer of type Node right, but malloc returns (void*).
    Sorry sir, i am relatively new to programming.
    Yes, i shall correct the global variables.

    Many people have told me global variables are bad, but why is that ???

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Read: Why Global Variables are Bad. Especially in your case since based on some of the items I posted in my original post.

    EDIT: Also your form() function should have a return type of Node*

    EDIT EDIT: Here this is what your form function should look like and how you should handle your list:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Node{
    	int data;
    	struct Node *next;
    };
    
    void form(struct Node**);
    
    int main(void){
    
    	struct Node *List = NULL;
    
    	form(&List);
    	while(List){
    		printf("%d ", List->data);
    		List = List->next;
    	}
    
    
    	getchar();
    	return (0);
    }
    void form(struct Node **newList){
    
    	struct Node *head=NULL, *add=NULL;
    	int num = 0;
    
    	printf("Enter a value(-1 to quit): ");
    	scanf("%d", &num);
    	
    	while(num != -1){
    		add = malloc(sizeof(*add));
    		if(!add){
    			printf("Error with malloc");
    			exit(1);
    		}
    		add->data = num;
    		add->next = NULL;
    		if(*newList == NULL){
    			*newList = add;
    			head = *newList;
    		}
    		else{
    			(*newList)->next = add;
    			*newList = (*newList)->next;
    		}
    		printf("Enter a value(-1 to quit): ");
    		scanf("%d", &num);
    	}
    	*newList = head;
    }
    Last edited by AndrewHunter; 08-17-2011 at 02:24 AM. Reason: being nice
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very Useful, BUT its a Doubt
    By cavestine in forum Linux Programming
    Replies: 1
    Last Post: 10-15-2007, 06:14 AM
  2. doubt
    By chinuchinu in forum C Programming
    Replies: 5
    Last Post: 08-19-2006, 03:36 AM
  3. doubt...
    By anntenna333 in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 11:35 AM
  4. c doubt
    By cbankgoesblank in forum C Programming
    Replies: 9
    Last Post: 09-28-2002, 05:04 PM
  5. doubt??
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-05-2002, 01:42 PM