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

Infinite loop problem in linklist

P: n/a
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;
}

Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Thu, 28 Oct 2004 11:51:26 -0400, "vjay" <vi***********@gmail.com>
wrote:
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.

Nov 14 '05 #2

P: n/a
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.

Nov 14 '05 #3

P: n/a
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" <vi***********@gmail.com> wrote in message
news:53******************************@localhost.ta lkaboutprogramming.com...
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.
Nov 14 '05 #4

P: n/a
On Thu, 28 Oct 2004 13:49:56 -0400
"vjay" <vi***********@gmail.com> wrote:
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
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #5

P: n/a
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.

Nov 14 '05 #6

P: n/a
[snip]
//int add(sn *);
//int delete(sn *);
//int length(sn *);


That's C++ style for comments. In C, use /* ... */ to comment code.
Nov 14 '05 #7

P: n/a
You are casting malloc(). Casting malloc is unnecessary and possibly
dangerous.
Read this page for more information:

http://users.powernet.co.uk/eton/c/hackerhotel.html

-Sastira

"vjay" <vi***********@gmail.com> wrote in message
news:a4******************************@localhost.ta lkaboutprogramming.com...
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;
}

Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.