Infinite loop problem in linklist

V

vjay

I want to just create a linklist.The program below goes into an endless
loop.The srange behaviour is that i can exit from the program if i create
only two nodes.After two goes into infinite loop.


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
typedef struct node sn;


int create (sn *);
//int add(sn *);
//int delete(sn *);
//int length(sn *);
int main()
{
sn *head;
printf("Create a list");
head =(sn*)malloc(sizeof(sn));
create(head);
return 0;
}

int create(sn *temp)
{
int i=0;
printf("Enter data element:");
scanf("%d",&temp->data);
printf("To continue press 1:");
scanf("%d",&i);
while (i==1)
{
temp->next = (sn*)malloc(sizeof(sn));
temp = temp->next;
create(temp);
}
temp->next = NULL;
return 0;
}
 
N

Nick Austin

I want to just create a linklist.The program below goes into an endless
loop.The srange behaviour is that i can exit from the program if i create
only two nodes.After two goes into infinite loop.


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
typedef struct node sn;


int create (sn *);
//int add(sn *);
//int delete(sn *);
//int length(sn *);
int main()
{
sn *head;
printf("Create a list");
head =(sn*)malloc(sizeof(sn));
create(head);
return 0;
}

int create(sn *temp)
{
int i=0;
printf("Enter data element:");
scanf("%d",&temp->data);
printf("To continue press 1:");
scanf("%d",&i);
while (i==1)

This is an infinite loop (i does not change).
{
temp->next = (sn*)malloc(sizeof(sn));
temp = temp->next;
create(temp);
}
temp->next = NULL;
return 0;
}

Nick.
 
V

vjay

create() is called inside while loop which asks me press 1 to conitinue.Iam
entering some other number and it doesn't go inside the loop but after the
return 0 statement it goes to while statement. I observed this behaviour
by tracing.
 
S

Sriram Rajagopalan

Vijay,

Try this,

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};

typedef struct node sn;


int create (sn *);
//int add(sn *);
//int delete(sn *);
//int length(sn *);
int main()
{
sn *head;
printf("Create a list");
head =(sn*)malloc(sizeof(sn));
create(head);
return 0;
}

int create(sn *temp)
{
int i=0;
printf("Enter data element:");
scanf("%d",&temp->data);
printf("To continue press 1:");
scanf("%d",&i);
if (i==1)
{
temp->next = (sn*)malloc(sizeof(sn));
temp = temp->next;
create(temp);
}
else
{
temp->next = NULL;
}
return 0;
}

This would create the linked list as expected.

vjay said:
create() is called inside while loop which asks me press 1 to conitinue.Iam
entering some other number and it doesn't go inside the loop but after the
return 0 statement it goes to while statement. I observed this behaviour
by tracing.

Well 'create' is a function call made within the while loop, so after
'create' is called if a number other than '1' is pressed , we come out of
the called function "create" to the callee parent "create". In the parent
"create" still i=1, hence the loop wud not be exited.

Hope that solved your doubt?

Regards,
Sriram.
 
F

Flash Gordon

create() is called inside while loop which asks me press 1 to
conitinue.Iam entering some other number and it doesn't go inside the
loop but after the return 0 statement it goes to while statement. I
observed this behaviour by tracing.

Please don't delete all context. If I had read this a few hours after
reading the previous post instead of seconds I would have no idea what
you are talking about. However, through luck I happen to remember.

I suggest you look up automatic variables (possible also called local
variables) in your text book. Also look up recursion, which is a very
bad solution to your problem, but it is what you are doing.

Basically, when a function calls itself (either directly or indirectly)
it get new versions of any automatic variables. So if it modified them
it does not affect the version in the previous instance.

Based on your post you have serious need of reading a book on C and
possibly a general book on programming as well. I would recommend K&R,
you can find the details in the FAQ for comp.lang.c
 
V

vjay

Thanks a lot sriram & flash.I got it now .Jumped on to pointers too
early.Anyway i have to go through that scope of variables chapter
again.Once again thanx guys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top