Originally posted by Parul Bagadia
value of strct object being overwritten
Collapse
X
-
-
No it's not global in the code in question because your definition of the function insertion isOriginally posted by Parul Bagadiafirst is my global variable and not local;
[code=c]
void insertion(struc t linklist*first)
{
...
}
[/code]The input parameter to the function is named first and hides your global variable. Not only that if it were not local then the function would cause a disaster because your while loop would cause the actual first value to be lost.
I explicitly told you what was wrong with the while loop, you make 1 iteration too many (because you check first rather than first->next but don't just bindly change this think about what happens the first time the function is called when first is NULL) and then you assign to the wrong place (first instead of first->next again) so only local data is changed instead of list data.Originally posted by Parul Bagadiaand second thing;
first->next=add;
add->next=NULL;
add=add->next;
i know this doesnt traverse the list;i did that coz my code will at least display 2 valus; instead of one;
but i didnt understand whats wrong with my while loop.
I know its overwritting the values;
but there lies my whole problem, why is it overwritting?
in case if i comment out
; first->next=add;
add->next=NULL;
add=add->next;
and just stick to while loop it doesnt work...Comment
-
No, you are not getting it.
earlier i had assigned first->next=add;
but at that time my program used to stop executing n used to give a fatal error.
Because in while loop i have assigned; first=first->next;
and given the condition for first coz first is itself travesing the list; and after i come out of while loop; first is pointing to NULL; and at that time if i do first->next..progra m will of course stop coz its unassigned pointer.Comment
-
That is exactly what I meant by "you are making 1 iteration of the loop too many". Testing first == NULL brings you out of the loop when first is NULL (obviously) but you want to come out of the loop when first points to the last item in your list, the iteration before the one you actually stop on.Originally posted by Parul BagadiaNo, you are not getting it.
earlier i had assigned first->next=add;
but at that time my program used to stop executing n used to give a fatal error.
Because in while loop i have assigned; first=first->next;
and given the condition for first coz first is itself travesing the list; and after i come out of while loop; first is pointing to NULL; and at that time if i do first->next..progra m will of course stop coz its unassigned pointer.
What feature that is unique to the last item in the list can you use to stop the loop on?Comment
-
It's not overwriting, you need to declare a temporary variable, rather than looping through with first. Otherwise, you can't access any nodes before the last the next time you try to do something with the list. Thus, it's effectively overwriting because you can't get to the old insertions.Comment
-
No, i still dont follow you.Originally posted by BanfaThat is exactly what I meant by "you are making 1 iteration of the loop too many". Testing first == NULL brings you out of the loop when first is NULL (obviously) but you want to come out of the loop when first points to the last item in your list, the iteration before the one you actually stop on.
What feature that is unique to the last item in the list can you use to stop the loop on?
ok, i accept i stop my loop at first when its pointing to NULL; but if i stop it 1 iteration before of it; first->next will be equal to NULL.
The unique feature about last node is it's next is pointing to NULL.
But its one and the same thing; main aim is whenever a node is pointing to NULL we need to assign it to new node.Comment
-
Thanx for that suggestion; ill try it out; but will you please explain me that in bit detail why to use temporary variable?Originally posted by LaharlIt's not overwriting, you need to declare a temporary variable, rather than looping through with first. Otherwise, you can't access any nodes before the last the next time you try to do something with the list. Thus, it's effectively overwriting because you can't get to the old insertions.Comment
-
Ok. If you use first, a global variable, the first time you add in a new element. first now points to the second to last element in the list, and since the links in the list only point forward, you only have access to the last two elements of the list. Thus, you use first as a marker that points to the start of the list and use temporary variables to loop through the list.Comment
-
Hey i made the changes as u suggested; and added a temporary node; the;Originally posted by LaharlOk. If you use first, a global variable, the first time you add in a new element. first now points to the second to last element in the list, and since the links in the list only point forward, you only have access to the last two elements of the list. Thus, you use first as a marker that points to the start of the list and use temporary variables to loop through the list.
but its still not working........ ...
[code=c]
//General insertion
void insertion(struc t linklist*first)
{
struct linklist* the=first;
struct linklist* add=NULL;
add= (struct linklist*)mallo c(sizeof(struct linklist));
the= (struct linklist*)mallo c(sizeof(struct linklist));
printf("\nEnter the no. you wish to add in link list\n");
scanf("\n %d",&add->value);
/*first->next=add;
add->next=NULL;
add=add->next;*/
if(first==NULL)
{
first=add;
first->next=NULL;
}
else
{
the=first;
while(the!=NULL )
{
the=the->next;
}
the=add;
the->next=NULL;
add->next=NULL;
}
}
And here is the code for display which now displays only 1. i.e. first element;
//Display of list
void display(struct linklist*first )
{
printf("\nFollo wing are the elements you have added in the list till now\n");
while(first!=NU LL)
{
printf("\n%d\n" ,first->value);
first=first->next;
}
}
[/code]
M fade up of thinking on it.......
Now plz tell me wats wrong with this?Comment
-
OK this is where you have it wrong, stopping at first == NULL and first->next == NULL is NOT one and the same thing.Originally posted by Parul BagadiaNo, i still dont follow you.
ok, i accept i stop my loop at first when its pointing to NULL; but if i stop it 1 iteration before of it; first->next will be equal to NULL.
The unique feature about last node is it's next is pointing to NULL.
In the first instance you are assigning to the local variable (for the reasons I have already given in a previous post) first, this does nothing to alter you list.
In the second instance the stop with first pointing to the last entry, this is global data already in the list so when you change first->next you are changing the list.
I will say this 1 more time, your loop is iterating 1 time too many so you end up with a pointer that only involves local data rather than involving the list held in global data.
Your final sentence
is correct, however if you stop the loop when first==NULL rather than first->next==NULL then you are not envolving a node pointing to NULL in you pointer assignments, you are just involving a local pointer which will be lost when the function exits.But its one and the same thing; main aim is whenever a node is pointing to NULL we need to assign it to new node.Comment
-
This is at least in part because in your function insertion first IS ALREADY a local variable. Taking a copy of it to another local variable has no significant impact on the logic of the code.Originally posted by Parul BagadiaHey i made the changes as u suggested; and added a temporary node; the;
but its still not working........ ...Comment
-
[code=c]
if(first==NULL)
{
first=add;
first->next=NULL;
}
[/code]
This would be the correct thing to do if first was your global head pointer, however at this point in the code first is a LOCAL pointer in the function and altering first does nothing to your list.
[code=c]
the=first;
while(the!=NULL )
{
the=the->next;
}
the=add;
the->next=NULL;
add->next=NULL;
}
[/code]This loop still iterates 1 time too many, you assign add to the which is a local pointer so the assignment makes go change to your global list. You need to stop the loop 1 iteration earlier and assign add to the->next.
You display function is fine, all your problems lie in the insertion function.
You would well advised not to give local variables the same name as your global variables.Comment
-
I have again made changes in my code.
Now i have declared first before main; so it cant be local; as its global.
I havnt passed its value to insert and directly used it there.
But now i have understood one thing;
whenever i assign first=first->next;
it doesnt happen actually. i have inserted two printf statements to check the memory locations and hence to know is it actually getting assigned or not......
And its not happening now.
Am about to fade up now.
[code=c]
#include<stdio. h>
#include<stdlib .h>
struct linklist
{
int value;
struct linklist*next;
}* first;
void firstinsert();
void insertion();
void display(struct linklist*first) ;
//First value insertion
void firstinsert()
{
first=(struct linklist*)mallo c(sizeof(struct linklist));
printf("\nEnter the first value of link list:\n");
scanf("\n%d",&f irst->value);
first->next=NULL;
}
//General insertion
void insertion()
{
//struct linklist*the=NU LL;
struct linklist* add=NULL;
add= (struct linklist*)mallo c(sizeof(struct linklist));
//the= (struct linklist*)mallo c(sizeof(struct linklist));
printf("\nEnter the no. you wish to add in link list\n");
scanf("\n %d",&add->value);
/*first->next=add;
add->next=NULL;
add=add->next;*/
if(first==NULL)
{
first=add;
printf("\n%d",& first);
first->next=NULL;
}
else
{
// the=first;
printf("\n%d",& first);
while(first!=NU LL)
{
first=first->next;
// the=the->next;
printf("\n%d",& first);
}
first=add;
first->next=NULL;
first->next=NULL;
}
}
void display(struct linklist*first )
{
printf("\nFollo wing are the elements you have added in the list till now\n");
while(first!=NU LL)
{
printf("\n%d\n" ,first->value);
first=first->next;
}
}[/code]Last edited by sicarie; Mar 9 '08, 01:55 PM. Reason: Code tags are [code=c] and after your code goes here [/code]. Please use them. They want to be used. They like to be used.Comment
-
-
Comment