why i keep having problem in thhis line: -
-
node *InsertVehicule(node *N, vehicule *V)
-
{
-
node *pre;
-
if(!N)
-
{
-
N = (node*)malloc(sizeof(node));
-
N->data_vehicule = V;
-
}
-
else
-
{
-
pre = N;
-
while(pre->NextVehicule)
-
{
-
if(pre->data_vehicule->distance > V->distance)
-
{
-
V = pre->NextVehicule->data_vehicule;
-
pre->NextVehicule->data_vehicule = V;
-
break;
-
}
-
else
-
pre = pre->NextVehicule;
-
-
if(pre->NextVehicule == NULL) // ****THIS ONE****
-
pre->NextVehicule->data_vehicule = V;
-
}
-
while(N->NextEdge)
-
printf("%s %d", N->data_vehicule->CarId, N->data_vehicule->distance);
-
}
-
return N;
-
}
24 3000
pre = pre->NextVehicule;
if(pre->NextVehicule == NULL) // ****THIS ONE****
You will crash if pre is NULL.
Check pre for NULL before trying to use it in pre->NextVehicule.
Additionally, if pre->nextVehicule is NULL (which your if...statement checks), then there will be no pre->nextVehicule->data_vehicule, so you will crash here as well.
but i need to reach that status in order to insert at the end!
I realy don't know how to overcome that NULL pointer issue please help - node *InsertVehicule(node *N, vehicule *V)
-
{
-
-
node *walker;
-
-
if(!N)
-
-
{
-
-
N = CreatNode();
-
-
N->data_vehicule = V;
-
-
printf("%s", N->data_vehicule->CarId);
-
-
}
-
-
else
-
-
{
-
-
walker = N;
-
-
while(walker->NextVehicule)
-
-
walker = walker->NextVehicule;
-
-
if(!walker->NextVehicule)// ***walker->NextVehicule = NULL***
-
-
walker->NextVehicule->data_vehicule = V;//***hence this line gonna cruch but I need to Insert in here...please help***
-
-
}
-
-
while(N->NextVehicule)
-
-
printf("%s", N->data_vehicule->CarId);
-
-
return N;
-
-
}
-
-
why N is always = NULL - node *InsertVehicule(node *N, vehicule *V)
-
{
-
-
node *walker;
-
-
if(!N)// ***N is always NULL!***
-
-
{
-
-
N = CreatNode();
-
-
N->data_vehicule = V;
-
-
printf("%s", N->data_vehicule->CarId);
-
-
}
-
-
else
-
-
{
-
-
walker = N;
-
-
while(walker->NextVehicule)
-
-
walker = walker->NextVehicule;
-
-
if(!walker->NextVehicule)
-
-
walker->NextVehicule->data_vehicule = V;
-
-
}
-
-
while(N->NextVehicule)
-
-
printf("%s", N->data_vehicule->CarId);
-
-
return N;
-
-
}
-
-
Create a Vehicle*, then set walker->nextVehicle to it, then change the data values you need. This way, you set the pointer to something, then you can access the pointer safely.
This code:
walker = walker->NextVehicule;
if(!walker->NextVehicule)// ***walker->NextVehicule = NULL***
is what I referred to in my Post #2.
walker can be NULL before you get to the if statement. If it is, you will crash. Check you pointers before using the indirection operator (->) on them: -
walker = walker->NextVehicule;
-
if (!walker)
-
{
-
//bail out
-
return;
-
}
-
if(!walker->NextVehicule)// ***walker->NextVehicule = NULL***
-
but I need that pointer to be null in order to inset a t the end....
am lost plz help
If the pointer 'walker' points to NULL, then you get a segmentation fault (segfault) when you try to access walker->NextVehicle because NULL has to link to any Vehicle, or anything else for that matter. You can take a NULL pointer and set it to point to something else, but that is the only thing you can do with it (that I know of) other than check if it points to NULL. Anything else gives you segfaults.
As such, you have to check if walker is null before you can do anything with it, since if it is, it'll go kablooie.
why my linked list is not set!!!! plz help - node *walker = N;
-
-
//printf("%s\n", V->CarId); //all vehicules are ok
-
-
if(!N)
-
-
{
-
-
N = CreatNode();
-
-
N->data_vehicule = V;
-
-
}
-
-
else
-
-
{
-
-
while(walker->NextVehicule)
-
-
walker = walker->NextVehicule;
-
-
if(!walker->NextVehicule)
-
-
{
-
-
walker->NextVehicule = CreatNode();
-
-
walker->NextVehicule->data_vehicule = V;
-
-
printf("%s\n", N->data_vehicule->CarId);//there is only the one of thebegining condition****
-
-
}
-
-
}
-
-
return N;
-
-
}
-
-
sill waiting for a help.... :)
You still haven't made the changes I mentioned in my Post #8.
to tell you the truth i did not get the meaning of "baikl out"
"bail out" means to jump out before the ship sinks. In this case, further use in the function will lead to segfaults, so you need to exit before those can happen by using return; which instantly ends a void function (or any other, but anything else needs a value returned).
why my linked list is not set!!!! plz help - node *walker = N;
-
-
//printf("%s\n", V->CarId); //all vehicules are ok
-
-
if(!N)
-
-
{
-
-
N = CreatNode();
-
-
N->data_vehicule = V;
-
-
}
-
-
else
-
-
{
-
-
while(walker->NextVehicule)
-
-
walker = walker->NextVehicule;
-
-
if(!walker->NextVehicule)
-
-
{
-
-
walker->NextVehicule = CreatNode();
-
-
walker->NextVehicule->data_vehicule = V;
-
-
printf("%s\n", N->data_vehicule->CarId);//there is only the one of thebegining condition****
-
-
}
-
-
}
-
-
return N;
-
-
}
-
-
Well, let review your code again, as long as i know therer are many problems possible happens with this piece of code:
- line 9:
When N is null, you just call to CreateNode() in order to allocate and initialize your N right ?
I do believe that you assume that this CreateNode work properly as what you expected because you are author of this code. I believe that in your CreateNode(), you have allocated memory and initialize the value, but you never think that sometime system does not do it for you it is because of an unexpected bug. so what happen ? You CreateNode will return a pointer which is not yet allocated or system crash.
I would suggest that never trust system when you code something. So here let check for the return value of CreateNode first before assign it to N.
- line 11:
V is actually an input parameter which is a pointer to Vehicle. You assign V to N->data_vehicle automatically because you assume that V is valid, because you are the one who write InsertVehicle() and you are also the one who call this function, so you know exactly that you have allocated a pointer of V already. but what if another guy ( like me ) wanna call your InsertVehicle() and unfortunately i do not input a valid V ( let say Vehicle V not yet malloc ). so what happen with your code. It does not tell me what wrong right ?
So i would suggest that ALWAYS check the inputted parameters before you do your job.
- line 12:
This if statement is called when N is null right ? so you allocate a head of your list, right ? but you forget to assign the N->NextVehicle = NULL. This is also a big problem. In memory, we can not make sure that 100% is free for you to use. You did not assign N->NextVehicle = NULL, it is possible that the next block of memory is allocated and has value. So your list probably never ended.
- line 19:
You have while loop to go to the end of list and assign a new vehicle to the end of list, rigth ?
i do admit that this piece of code work properly for only a NULL list ( have no vehicle ), or a bunch of vehicles in list ( more than 2 ). why ?
because you only check for the walker->NextVehicle. What is about there is only one vehicle in a list. so this condition will false 4ever right ? then you will have only one vehicle in you list until the end of your life. That is why you say there is only the one of the beginning
- line 19:
You always trust what you have done, and what system do for you.
You while loop will end if it reaches the end of your list right ? but who know ? you or me ?......There is no one know that when it ends right ?
OK i give you an example:
you have allocated a single link list and do something with your list. Then you start to loop through all the nodes in list until you reach the NULL you stop there. It is because you think that Memory is only reserved for you application. but it never right ? Memory is a shared resource everybody can write and read from. So imagine you have only allocate a list of 3 nodes. but unexpectedly, your last node points to another area that has values not NULL as what you have assign. So what happen with your while loop, it will run till the end of your machine. That is also a big problem.
So you better limit the maximum loop circles of your while loop. this technic will prevent you from going through the 4ever loops.
/looker
******new version of the function in the post above***** -
node *InsertVehicule(node *head, vehicule *V)
-
-
{
-
-
node *New, *walker;
-
-
node *rear = (node*)malloc(sizeof(node));
-
-
New = CreatNode();
-
-
New->data_vehicule = V;
-
-
if(! head)
-
-
{
-
-
head = New;
-
-
rear = head;
-
-
//printf("%s", rear->data_vehicule->CarId);
-
-
}
-
-
else
-
-
{
-
-
rear->NextVehicule = New;
-
-
rear = New;
-
-
//printf("%s", rear->data_vehicule->CarId);
-
-
}
-
-
walker = head;
-
-
while(walker)
-
-
{
-
-
printf("\n%s", walker->data_vehicule->CarId);
-
-
walker = walker->NextVehicule;
-
-
}
-
-
return head;
-
-
}
-
-
the problem now is when I travers it only the first value of the linked list is printed N times; the N is correct ! hence this suggest that the insertion is ok!
therefore, can't find the problem...
please help
node *InsertVehicule(node *head, vehicule *V)
{
node *New, *walker;
node *rear = (node*)malloc(sizeof(node)); <<<<<<<<<<<<<<<<<<<<
New = CreatNode();
New->data_vehicule = V;
if(! head)
{
head = New;
rear = head; <<<<<<<<<<<<<<<<<<<<<
The rear node is never initialized. I don't even know why you have it. For a single linked list, you walk to the end and append the new node.
whene I walk throught my linked list I get somehow the same problem!!!
raun time error: access violation... -
node *InsertVehicule(node *head, vehicule *V)
-
-
{
-
-
node *walker = CreatNode();
-
-
node *New = CreatNode();
-
-
if(!head)
-
-
{
-
-
New->data_vehicule = V;
-
-
head = New;
-
-
}
-
-
else
-
-
{
-
-
walker = head;
-
-
while(walker)
-
-
walker = walker->NextVehicule;
-
-
walker->NextVehicule = New; // *****here****
-
-
}
-
-
return head;
-
-
}
-
-
this version is semi working..instead having 5 deferente values, am having twice the first one an the others (NULL) - node *InsertVehicule(node *head, vehicule *V)
-
-
{
-
-
node *walker = CreatNode();
-
-
node *New = CreatNode();
-
-
if(!head)
-
-
{
-
-
New->data_vehicule = V;
-
-
head = New;
-
-
printf("%s\n", head->data_vehicule->CarId);
-
-
}
-
-
else
-
-
{
-
-
walker = head;
-
-
while(walker->NextVehicule)
-
-
walker = walker->NextVehicule;
-
-
walker->NextVehicule = New;
-
-
printf("%s\n", walker->data_vehicule->CarId);
-
-
}
-
-
return head;
-
-
}
-
-
now every thing is ok but when i travers there is the first element repeated 5 time!!!! -
node *InsertVehicule(node *head, vehicule *V)
-
-
{
-
-
node *walker = CreatNode();
-
-
node *New = CreatNode();
-
-
New->data_vehicule = V;
-
-
if(!head)
-
-
{
-
-
head = New;
-
-
//printf("%s\n", head->data_vehicule->CarId);
-
-
}
-
-
else
-
-
{
-
-
walker = head;
-
-
while(walker)
-
-
walker = walker->NextVehicule;
-
-
walker = New;
-
-
//printf("%s\n", walker->data_vehicule->CarId);
-
-
}
-
-
walker = head;
-
-
while(walker)
-
-
{
-
-
printf("%s\n", walker->data_vehicule->CarId);
-
-
walker = walker->NextVehicule;
-
-
}
-
-
return head;
-
-
}
-
-
am rely stuck here!!!!!!! almost to give up
Hello mate:
Don't feel so bad about programming, have a break and come back later when your brain is recharged with energy again:)
I sugest you think of dividing your function into smaller parts, say, one for creating the stack(in my case, I would use head and tail sentinels to help me), one for pushing the stack and another one for poping the stack.
Then you can check each of those smaller code fragments to see if they work properly.
I simply cannot see what you are doing in your code beacuse there is little comments:(
If you inserted five elements into the stack but can only retrieve the first one, that means the link after node first is broken.
Most of linked list code errors come from bad logic, not from bad code implementation, since compilers cannot figure out those logic errors for you.
And, if you are as lazy as me, you can google for "c stack tutorial", and head first to check at the sample codes...
For a single linked list, you walk to the end and append the new node.
This was back on Post #18. What I meant to say is that I can see where you put the vehicle in the node but I don't see where you make the next pointer of the previous node point to the new one (appending to end of list) or where the next pointer of the new node is make to point to head (appending to the front of the list - requires ne node to become the head).
And please note that the head argument is a node* and that makes it a copy of the pointer used on the insert call. When you change this inside the insert function you are only changing the copy. If you need to change the pointer used on the call, then you need a node** for head so you can dereference it to change the value in the pointer in the calling function.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: John N. |
last post by:
Hi All,
Here I have a linked list each containing a char and is double linked.
Then I have a pointer to an item in that list which is the current
insertion point.
In this funtion, the user...
|
by: Eugen J. Sobchenko |
last post by:
Hi!
I'm writing function which swaps two arbitrary elements
of double-linked list. References to the next element of list
must be unique or NULL (even during swap procedure), the same condition...
|
by: drewy2k12 |
last post by:
Heres the story, I have to create a doubly linked list for
class, and i have no clue on how to do it, i can barely create a
single linked list. It has to have both a head and a tail pointer,
and...
|
by: bjhecht |
last post by:
http://rafb.net/paste/results/tJoB4z75.html
After executing the following code I receive the errors:
a.c: In function `insert':
a.c:59: error: incompatible types in assignment
I have no...
|
by: william |
last post by:
When implementing Linked list, stack, or trees, we always use pointers
to 'link' the nodes.
And every node is always defined as:
struct node
{
type data; //data this node contains
...
node *...
|
by: Suyash Upadhyay |
last post by:
Hello All,
I am a beginner of C Programming, I am working on linked list now-a-days, i have successfully created a linked list and displayed, but when i tried to insert an element in mid of linked...
|
by: theeverdead |
last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like:
Trevor Johnson
Kevin Smith
Allan Harris
I need to read that file into program and then turn it into a linked...
|
by: APEJMAN |
last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result
I post the code here
I dont wanne bother you, but if any one of you have time to...
|
by: Atos |
last post by:
SINGLE-LINKED LIST
Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
|
by: QiongZ |
last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
|
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...
| |