473,394 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Infinite loop problem in linklist

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
7 3109
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
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
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
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
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
[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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
2
Parul Bagadia
by: Parul Bagadia | last post by:
I have written a code for deleting certain value from linklist; it's not working; where as i have written one for deleting a no., after given no. which works fine! I even debugged it; but invain;...
2
by: dynamo | last post by:
this is a basic linklist,there seems to be a runtime error when i run the program(the main function) i suspect it has something to do with my del function but i see nothing wrong can you help.Thanx...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.