473,398 Members | 2,212 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,398 software developers and data experts.

linked list (Insert)

momotaro
357 100+
why i keep having problem in thhis line:

Expand|Select|Wrap|Line Numbers
  1.  
  2. node *InsertVehicule(node *N, vehicule *V)
  3. {
  4.    node *pre;
  5.    if(!N)
  6.    {
  7.       N = (node*)malloc(sizeof(node));
  8.       N->data_vehicule = V;
  9.    }
  10.    else
  11.    {
  12.       pre = N;
  13.       while(pre->NextVehicule)
  14.       {
  15.          if(pre->data_vehicule->distance > V->distance)
  16.          {
  17.             V = pre->NextVehicule->data_vehicule;
  18.             pre->NextVehicule->data_vehicule = V;
  19.             break;
  20.          }
  21.          else
  22.             pre = pre->NextVehicule;
  23.  
  24.          if(pre->NextVehicule == NULL) // ****THIS ONE****
  25.             pre->NextVehicule->data_vehicule = V;
  26.       }
  27.       while(N->NextEdge)
  28.          printf("%s %d", N->data_vehicule->CarId, N->data_vehicule->distance);
  29.    }
  30.    return N;
  31. }
Dec 1 '07 #1
24 3000
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 1 '07 #2
Ganon11
3,652 Expert 2GB
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.
Dec 1 '07 #3
momotaro
357 100+
but i need to reach that status in order to insert at the end!
Dec 1 '07 #4
momotaro
357 100+
I realy don't know how to overcome that NULL pointer issue please help


Expand|Select|Wrap|Line Numbers
  1.  node *InsertVehicule(node *N, vehicule *V) 
  2. {
  3.  
  4. node *walker;
  5.  
  6. if(!N)
  7.  
  8. {
  9.  
  10. N = CreatNode();
  11.  
  12. N->data_vehicule = V;
  13.  
  14. printf("%s", N->data_vehicule->CarId);
  15.  
  16. }
  17.  
  18. else
  19.  
  20. {
  21.  
  22. walker = N;
  23.  
  24. while(walker->NextVehicule)
  25.  
  26. walker = walker->NextVehicule;
  27.  
  28. if(!walker->NextVehicule)// ***walker->NextVehicule = NULL***
  29.  
  30. walker->NextVehicule->data_vehicule = V;//***hence this line gonna cruch but I need to Insert in here...please  help***
  31.  
  32. }
  33.  
  34. while(N->NextVehicule)
  35.  
  36. printf("%s", N->data_vehicule->CarId);
  37.  
  38. return N;
  39.  
  40. }
  41.  
  42.  
Dec 1 '07 #5
momotaro
357 100+
why N is always = NULL

Expand|Select|Wrap|Line Numbers
  1.  node *InsertVehicule(node *N, vehicule *V) 
  2. {
  3.  
  4. node *walker;
  5.  
  6. if(!N)// ***N is always NULL!***
  7.  
  8. {
  9.  
  10. N = CreatNode();
  11.  
  12. N->data_vehicule = V;
  13.  
  14. printf("%s", N->data_vehicule->CarId);
  15.  
  16. }
  17.  
  18. else
  19.  
  20. {
  21.  
  22. walker = N;
  23.  
  24. while(walker->NextVehicule)
  25.  
  26. walker = walker->NextVehicule;
  27.  
  28. if(!walker->NextVehicule)
  29.  
  30. walker->NextVehicule->data_vehicule = V;
  31.  
  32. }
  33.  
  34. while(N->NextVehicule)
  35.  
  36. printf("%s", N->data_vehicule->CarId);
  37.  
  38. return N;
  39.  
  40. }
  41.  
  42.  
Dec 3 '07 #6
Laharl
849 Expert 512MB
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.
Dec 3 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. walker = walker->NextVehicule;
  2. if (!walker)
  3. {
  4.      //bail out
  5.      return;
  6. }
  7. if(!walker->NextVehicule)// ***walker->NextVehicule = NULL***
  8.  
Dec 3 '07 #8
momotaro
357 100+
but I need that pointer to be null in order to inset a t the end....
am lost plz help
Dec 3 '07 #9
Laharl
849 Expert 512MB
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.
Dec 4 '07 #10
momotaro
357 100+
why my linked list is not set!!!! plz help


Expand|Select|Wrap|Line Numbers
  1.  node *walker = N; 
  2.  
  3. //printf("%s\n", V->CarId); //all vehicules are ok
  4.  
  5. if(!N)
  6.  
  7. {
  8.  
  9. N = CreatNode();
  10.  
  11. N->data_vehicule = V;
  12.  
  13. }
  14.  
  15. else
  16.  
  17. {
  18.  
  19. while(walker->NextVehicule)
  20.  
  21. walker = walker->NextVehicule;
  22.  
  23. if(!walker->NextVehicule)
  24.  
  25. {
  26.  
  27. walker->NextVehicule = CreatNode();
  28.  
  29. walker->NextVehicule->data_vehicule = V;
  30.  
  31. printf("%s\n", N->data_vehicule->CarId);//there is only the one of thebegining condition****
  32.  
  33. }
  34.  
  35. }
  36.  
  37. return N;
  38.  
  39. }
  40.  
  41.  
Dec 4 '07 #11
momotaro
357 100+
sill waiting for a help.... :)
Dec 4 '07 #12
weaknessforcats
9,208 Expert Mod 8TB
You still haven't made the changes I mentioned in my Post #8.
Dec 4 '07 #13
momotaro
357 100+
to tell you the truth i did not get the meaning of "baikl out"
Dec 4 '07 #14
Laharl
849 Expert 512MB
"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).
Dec 5 '07 #15
looker
18
why my linked list is not set!!!! plz help


Expand|Select|Wrap|Line Numbers
  1.  node *walker = N; 
  2.  
  3. //printf("%s\n", V->CarId); //all vehicules are ok
  4.  
  5. if(!N)
  6.  
  7. {
  8.  
  9. N = CreatNode();
  10.  
  11. N->data_vehicule = V;
  12.  
  13. }
  14.  
  15. else
  16.  
  17. {
  18.  
  19. while(walker->NextVehicule)
  20.  
  21. walker = walker->NextVehicule;
  22.  
  23. if(!walker->NextVehicule)
  24.  
  25. {
  26.  
  27. walker->NextVehicule = CreatNode();
  28.  
  29. walker->NextVehicule->data_vehicule = V;
  30.  
  31. printf("%s\n", N->data_vehicule->CarId);//there is only the one of thebegining condition****
  32.  
  33. }
  34.  
  35. }
  36.  
  37. return N;
  38.  
  39. }
  40.  
  41.  
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
Dec 5 '07 #16
momotaro
357 100+
******new version of the function in the post above*****
Expand|Select|Wrap|Line Numbers
  1. node *InsertVehicule(node *head, vehicule *V)
  2.  
  3. {
  4.  
  5. node *New, *walker;
  6.  
  7. node *rear = (node*)malloc(sizeof(node));
  8.  
  9. New = CreatNode();
  10.  
  11. New->data_vehicule = V;
  12.  
  13. if(! head)
  14.  
  15. {
  16.  
  17. head = New;
  18.  
  19. rear = head;
  20.  
  21. //printf("%s", rear->data_vehicule->CarId);
  22.  
  23. }
  24.  
  25. else
  26.  
  27. {
  28.  
  29. rear->NextVehicule = New;
  30.  
  31. rear = New;
  32.  
  33. //printf("%s", rear->data_vehicule->CarId);
  34.  
  35. }
  36.  
  37. walker = head;
  38.  
  39. while(walker)
  40.  
  41. {
  42.  
  43. printf("\n%s", walker->data_vehicule->CarId);
  44.  
  45. walker = walker->NextVehicule;
  46.  
  47. }
  48.  
  49. return head;
  50.  
  51. }
  52.  
  53.  
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
Dec 21 '07 #17
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 21 '07 #18
momotaro
357 100+
whene I walk throught my linked list I get somehow the same problem!!!
Dec 21 '07 #19
momotaro
357 100+
raun time error: access violation...
Expand|Select|Wrap|Line Numbers
  1. node *InsertVehicule(node *head, vehicule *V)
  2.  
  3. {
  4.  
  5. node *walker = CreatNode();
  6.  
  7. node *New = CreatNode();
  8.  
  9. if(!head)
  10.  
  11. {
  12.  
  13. New->data_vehicule = V;
  14.  
  15. head = New;
  16.  
  17. }
  18.  
  19. else
  20.  
  21. {
  22.  
  23. walker = head;
  24.  
  25. while(walker)
  26.  
  27. walker = walker->NextVehicule;
  28.  
  29. walker->NextVehicule = New; // *****here****
  30.  
  31. }
  32.  
  33. return head;
  34.  
  35. }
  36.  
  37.  
Dec 21 '07 #20
momotaro
357 100+
this version is semi working..instead having 5 deferente values, am having twice the first one an the others (NULL)

Expand|Select|Wrap|Line Numbers
  1.  node *InsertVehicule(node *head, vehicule *V) 
  2.  
  3. {
  4.  
  5. node *walker = CreatNode();
  6.  
  7. node *New = CreatNode();
  8.  
  9. if(!head)
  10.  
  11. {
  12.  
  13. New->data_vehicule = V;
  14.  
  15. head = New;
  16.  
  17. printf("%s\n", head->data_vehicule->CarId);
  18.  
  19. }
  20.  
  21. else
  22.  
  23. {
  24.  
  25. walker = head;
  26.  
  27. while(walker->NextVehicule)
  28.  
  29. walker = walker->NextVehicule;
  30.  
  31. walker->NextVehicule = New;
  32.  
  33. printf("%s\n", walker->data_vehicule->CarId);
  34.  
  35. }
  36.  
  37. return head;
  38.  
  39. }
  40.  
  41.  
Dec 21 '07 #21
momotaro
357 100+
now every thing is ok but when i travers there is the first element repeated 5 time!!!!


Expand|Select|Wrap|Line Numbers
  1. node *InsertVehicule(node *head, vehicule *V)
  2.  
  3. {
  4.  
  5. node *walker = CreatNode();
  6.  
  7. node *New = CreatNode();
  8.  
  9. New->data_vehicule = V;
  10.  
  11. if(!head)
  12.  
  13. {
  14.  
  15. head = New;
  16.  
  17. //printf("%s\n", head->data_vehicule->CarId);
  18.  
  19. }
  20.  
  21. else
  22.  
  23. {
  24.  
  25. walker = head;
  26.  
  27. while(walker)
  28.  
  29. walker = walker->NextVehicule;
  30.  
  31. walker = New;
  32.  
  33. //printf("%s\n", walker->data_vehicule->CarId);
  34.  
  35. }
  36.  
  37. walker = head;
  38.  
  39. while(walker)
  40.  
  41. {
  42.  
  43. printf("%s\n", walker->data_vehicule->CarId);
  44.  
  45. walker = walker->NextVehicule;
  46.  
  47. }
  48.  
  49. return head;
  50.  
  51. }
  52.  
  53.  
Dec 21 '07 #22
momotaro
357 100+
am rely stuck here!!!!!!! almost to give up
Dec 21 '07 #23
mattmao
121 100+
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...
Dec 22 '07 #24
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 22 '07 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
12
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...
1
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...
3
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...
9
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 *...
3
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...
1
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...
6
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...
0
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
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,...
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
tracyyun
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...
0
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,...
0
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...

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.