473,626 Members | 3,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(si zeof(sn));
create(head);
return 0;
}

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

Nov 14 '05 #1
7 3130
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<stdli b.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("Crea te a list");
head =(sn*)malloc(si zeof(sn));
create(head) ;
return 0;
}

int create(sn *temp)
{
int i=0;
printf("Ente r data element:");
scanf("%d",&te mp->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(siz eof(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(si zeof(sn));
create(head);
return 0;
}

int create(sn *temp)
{
int i=0;
printf("Enter data element:");
scanf("%d",&tem p->data);
printf("To continue press 1:");
scanf("%d",&i);
if (i==1)
{
temp->next = (sn*)malloc(siz eof(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******** *************** *******@localho st.talkaboutpro gramming.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******** *************** *******@localho st.talkaboutpro gramming.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(si zeof(sn));
create(head);
return 0;
}

int create(sn *temp)
{
int i=0;
printf("Enter data element:");
scanf("%d",&tem p->data);
printf("To continue press 1:");
scanf("%d",&i);
while (i==1)
{
temp->next = (sn*)malloc(siz eof(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
5573
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 fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in a loop, they either terminate, or repeat themselves. In the mythical halting problem scenario...
5
4928
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
4320
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
1920
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; down here am posting my code; please somebody who can point it out; let me know fast; whats wrong in it... //Delete a number next to the given number void afterdelete() { struct linklist*temp=NULL; int no=0;
2
2101
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 #include <iostream> #include <iostream> #include <string> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <fstream> using namespace std;
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.