![]() |
linked list keep getting extra node
The function below is supposed to create a chain of the Employee struct (on the heap), but the function below creates 1 more employee struct than i need, i really need 2 employee structs (i know should of used for loop) but i can't seem to code the function without creating the extra employee struct. Is it possible to write the function without having this extra struct being created?
Quote:
|
isn't the loop going to create two structs, and you created one before the loop or am i missing something
|
that will solve the extra node problem, but then how do i know where the head of the list is because before i created the head first then the rest so i knew where the head was.
EDIT: ah one way would be to assign temp to head while i (the incrementer) is still 0(first run), but is there an other way to do that? |
I think something like this will work...
:
Employee *head = NULL; |
Quote:
|
So you want to append nodes at the tail-end is what you're saying?
|
The issue is that you need to keep track of the head, but are trying also to keep track of the last node in the list so you can add quickly to the end. So keep track of both the head and the last element.
:
Employee *head = NULL, *last;:
Employee *head = NULL, *last; |
okay finnaly got rid of the extra node but know every time i try to print or get some of the data from the last element i get an access violation error:
Creating the List: :
Employee* createList()Destroying the list: :
void destroyList(Employee* head)And the calling of the functions: :
void main(void)I am not understanding why i am getting this error from the last element, as you can see from the createList() function on the last element there should be the number 1 in the last element, any help along with explanaition would be helpful. |
The while doesn't exit until currentEmployee is NULL. Now that it's NULL, and you shouldn't dereference it, that's the first thing you do (currentEmployee->age).
|
Quote:
EDIT: The more i think about this i think the only way would be to have a dummy node (the extra node i have been running from). Is there anotherway to get at the data that you put into the last node without having to an extra node |
| All times are GMT -5. The time now is 1:29 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC