By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,614 Members | 1,620 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,614 IT Pros & Developers. It's quick & easy.

Creating Linked List using Recursion

P: 34
Hi All,
As a beginner in Computer Programming, I decided to create Linked List using recursion, but I am not getting right answer. I think it is fundamentally correct, but I am stuck.
Please help me,


Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2.  
  3. struct node
  4. {
  5.     int n;
  6.     struct node* link;
  7. };
  8.  
  9. struct node* node1=NULL;
  10. struct node* node2=NULL;
  11.  
  12. void create();
  13.  
  14. main()
  15. {
  16.     char c;
  17.     printf("Do you want to create a linked list? ");
  18.     scanf("%c",&c);
  19.     if(c=='y')
  20.         create();
  21.     //Displaying Elements of the Linked List
  22.     while(node2!=NULL)
  23.     {
  24.         printf("%d",node2->n);
  25.         node2=node2->link;
  26.     }    
  27. }
  28.  
  29.  
  30. void create()
  31. {
  32.     struct node a;
  33.     char ch;
  34.     if(node1==NULL)
  35.     {
  36.         printf("\nEnter the Integer Value: ");
  37.         scanf("%d",&(a.n));
  38.         a.link=NULL;
  39.         node1=&a;
  40.         node2=&a;
  41.     }
  42.     else
  43.     {
  44.         printf("\nEnter the Integer Value: ");
  45.         scanf("%d",&(a.n));
  46.         a.link=NULL;
  47.         node1->link=&a;
  48.         node1=node->link;
  49.     }
  50.     printf("\nDo you want to enter more elements in linked list? ");
  51.     scanf("%s",&ch);
  52.     if(ch=='n')
  53.         return;
  54.     else
  55.         create();
  56. }
Thanx

Suyash.Upadhyay
Mar 25 '07 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 1,510
in create()
Expand|Select|Wrap|Line Numbers
  1. void create()
  2. {
  3.     struct node a;
  4.  
a is a local variable which is lost on function exit.
make a a pointer and use malloc() to allocate storage, e.g.
Expand|Select|Wrap|Line Numbers
  1. void create()
  2. {
  3.     struct node *a=malloc(sizeof(struct node));
  4.     char ch;
  5.     if(node1==NULL)
  6.     {
  7.         printf("\nEnter the Integer Value: ");
  8.         scanf("%d",&(a->n));
  9.         a->link=NULL;
  10.         node1=a;
  11.         node2=a;
  12.     }
  13. ... etc etc
  14.  
Mar 25 '07 #2

P: 34
Thanx,
I got right Output, but can you please clarify one doubt:-


Structure is user defined data type, so when we instatiate it some space in memory should be get allocated for that variable( just like when we declare int a; some space allocates for a), then why should we explicitly use malloc function.
Please help me,


Suyash.Upadhyay
Mar 26 '07 #3

Expert 100+
P: 181
Thanx,
I got right Output, but can you please clarify one doubt:-


Structure is user defined data type, so when we instatiate it some space in memory should be get allocated for that variable( just like when we declare int a; some space allocates for a), then why should we explicitly use malloc function.
Please help me,


Suyash.Upadhyay
there are four scopes in c, in general
block, function, file and program scope. some people refer global scope as program scope.
At the end of scope usually the variable space or memory occupied by variable is freed and returned to operating system. (for this reason usually these variables are stored in stack)
Memory allocated by malloc are from heap and this persist between function calls(unless you free them using free()).
Mar 26 '07 #4

P: 34
there are four scopes in c, in general
block, function, file and program scope. some people refer global scope as program scope.
At the end of scope usually the variable space or memory occupied by variable is freed and returned to operating system. (for this reason usually these variables are stored in stack)
Memory allocated by malloc are from heap and this persist between function calls(unless you free them using free()).


Thanx but I have still a problem.

In my program node2 is defined in program scope.I have printed node->n immediately after assignment of node2=&a ( where a is an instance of structure)
it gives right result, but when I tried to print node2->n just outside that block it always gives result as 0. What's the problem with it. Please have a glance at my code.

Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2.  
  3. struct node
  4. {
  5.     int n;
  6.     struct node* link;
  7. };
  8.  
  9. struct node* node1=NULL;
  10. struct node* node2=NULL;
  11. struct node* node3;
  12.  
  13. void create();
  14.  
  15. main()
  16. {
  17.     char c;
  18.     printf("Do you want to create a linked list? ");
  19.     scanf("%c",&c);
  20.     if(c=='y')
  21.         create();
  22. }
  23.  
  24.  
  25. void create()
  26. {
  27.     struct node a;
  28.     char ch;
  29.     if(node1==NULL)
  30.     {
  31.         printf("\nEnter the Integer Value: ");
  32.         scanf("%d",&(a.n));
  33.         a.link=NULL;
  34.         node1=&a;
  35.         node2=&a;
  36.         printf("\nTesting1 %d",node2->n);
  37.     }
  38.     else
  39.     {
  40.         printf("\nEnter the Integer Value: ");
  41.         scanf("%d",&(a.n));
  42.         a.link=NULL;
  43.         node1->link=&a;
  44.         node1=node1->link;
  45.         printf("\nTesting2 %d",node1->n);
  46.     }
  47.     printf("\nDo you want to enter more elements in linked list? ");
  48.     scanf("%s",&ch);
  49.     printf("\nTesting2.5 %d",node2->n);  //it gives output 0
  50.     if(ch=='n')
  51.         return;
  52.     else
  53.     {
  54.         printf("\nTesting3 %d",node2->n);
  55.         while(node3!=NULL)
  56.         {
  57.             printf("\nTesting4 %d",node2->n);  // always prints 0
  58.             printf("\n");
  59.             node3=node3->link;
  60.         }
  61.         create();
  62.     }
  63. }

Please help me.

Suyash.Upadhyay
Mar 26 '07 #5

Expert 100+
P: 1,510
in the following you read ch which is a char using conversion specification %s which is for a string
Expand|Select|Wrap|Line Numbers
  1.     printf("\nDo you want to enter more elements in linked list? ");
  2.     scanf("%s",&ch);
  3.  
and you are corrupting memory
change the conversion specification to %c to read a char
Expand|Select|Wrap|Line Numbers
  1.     printf("\nDo you want to enter more elements in linked list? ");
  2.     getchar(); // read last newline
  3.     scanf("%c",&ch);
  4.     printf("\nTesting2.5 %d",node2->n);  //it gives output 0
  5.  
you need the getchar() to remove the newline entered after the last integer value
Mar 26 '07 #6

Ganon11
Expert 2.5K+
P: 3,652
In my program node2 is defined in program scope.I have printed node->n immediately after assignment of node2=&a ( where a is an instance of structure)
it gives right result, but when I tried to print node2->n just outside that block it always gives result as 0. What's the problem with it.
In the function, you have created a node a with function scope. Then you have node2 point to this temporary node - during the function, node2 is properly defined and will act as planned. But as soon as the function exits, the node created by a is de-allocated. Suddenly, node2 points to de-allocated (a.k.a. useless) memory.

If you are using C++, you should create a node object for node2 using the new operator. If you are using C, you should create a node object using malloc.
Mar 26 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.