Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   linked list keep getting extra node (http://www.programmingforums.org/showthread.php?t=12242)

kruptof Dec 22nd, 2006 1:50 PM

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:

Employee* createList()
{
Employee* head = NULL;
Employee* temp = NULL;

temp = new Employee;
head = temp;

int i=0;
do
{
temp->age = i;
temp->next = new Employee;
temp = temp->next;
temp->next = NULL;
i++;
}while(i < 2);

return head;
}

mark Dec 22nd, 2006 2:19 PM

isn't the loop going to create two structs, and you created one before the loop or am i missing something

kruptof Dec 22nd, 2006 2:29 PM

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?

andro Dec 22nd, 2006 2:50 PM

I think something like this will work...

:

Employee *head = NULL;

for (int i = 0; i < 2; i++)
{
        Employee *temp = new Employee();
        temp->age = i;
        temp->next = head;
        head = temp;
}

return head;


kruptof Dec 22nd, 2006 3:09 PM

Quote:

Originally Posted by andro (Post 121635)
I think something like this will work...

:

Employee *head = NULL;

for (int i = 0; i < 2; i++)
{
        Employee *temp = new Employee();
        temp->age = i;
        temp->next = head;
        head = temp;
}

return head;


but when i is equal to 1 head wouldn't point to the beginning of the list it will point to the second struct(because you have ovewritten it).

andro Dec 22nd, 2006 4:39 PM

So you want to append nodes at the tail-end is what you're saying?

grumpy Dec 22nd, 2006 4:47 PM

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;
  for (int i = 0; i < number_of_nodes; ++i)
  {
      Employee *new_node = new Employee();
      new_node->age = i;
      new_node->next = NULL;
        if (!head)
        {
            last = head = new_node;
        }
        else
        {
            last = last->next = new_node;
        }
  }

or (in a while loop)
:

  Employee *head = NULL, *last;
  int i = 0;
  do
  {
        // same body of loop
  } while (++i < number_of_nodes);


kruptof Dec 22nd, 2006 9:37 PM

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()
{
Employee* head = NULL;
Employee *new_node = NULL;

for (int i = 0; i < 2; ++i)
  {
      new_node = new Employee();
      new_node->age = i;
      new_node->next = NULL;
         
        if (i==0)
        {
            head = new_node;
        }
                new_node = new_node->next;

  }
return head;
}


Destroying the list:
:

void destroyList(Employee* head)
{
        Employee* currentEmployee = head;
        Employee* temp = NULL;
       
        while(currentEmployee!=NULL)
        {
                temp = currentEmployee;
                cout<<currentEmployee->age<<endl;
                currentEmployee = currentEmployee->next;
                delete temp;
        }
        cout<<currentEmployee->age<<endl;//error recieved on this line
        delete temp;       
}


And the calling of the functions:
:

void main(void)
{
        Employee* head = createList();
        destroyList(head);
        cout<<"\nlist destroyed"<<endl;
       
}


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.

DaWei Dec 22nd, 2006 9:42 PM

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).

kruptof Dec 22nd, 2006 10:02 PM

Quote:

Originally Posted by DaWei (Post 121650)
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).

ah yeah. But what has happened to my other element doesn't createlist() funtion create two elements in the list?

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