jeudi 2 octobre 2014

c++ adding to the end of a doubly linked list


Vote count:

0




I"m new to this so I'm barely learning how to implement a linked list. My program crashes when i input a number. I'm suppose to add the node to the back of the list.


This is the node



struct node
{
int data;
node *next;
node *prev;
};


this is the add function



void Add(node* &head, int newdata)
{
//create a new node to hold the data with a terminal (NULL) next pointer
node *tmp = new node;
tmp->data = newdata;
tmp->next;
node *current = head;
//check whether head has been initialized (is NULL)
// if not, make the new node head and set prev
if ( head != NULL)
{
tmp = head;
tmp->prev = NULL;
}

//if head has been initialized
//find the end of the chain with a pointer
else
{
while (current->next != NULL )
{
current = current->next;
}
}

//add the new node on to the last node in the list
//set pointers both forward and backwards
tmp = current;
tmp->prev = current->prev->next;
tmp->next = current->next;



}


asked 2 mins ago







c++ adding to the end of a doubly linked list

Aucun commentaire:

Enregistrer un commentaire