Double link list

When you create the 1st node I see front and back = the new node. When you add the 2nd node front gets modified, along with back, because they both pointed to the same address, but when you add a 3rd node how is front getting the 3rd node because back = n (address of n was passed to back) so how is front getting the 3rd node.

[code]
//An example of a simple double linked list using OOP techniques
#include <iostream>
using namespace std;

struct Node
{
double value;
Node *N,*P;
Node(double y)
{
value = y;
N = P = NULL;
}
};

class doubleLinkedList
{
Node *front;
Node *back;
public:
doubleLinkedList()
{ front = NULL; back = NULL; }
~doubleLinkedList(){ destroyList();}
void appendNodeBack(double x);
void dispNodesForward();
void dispNodesReverse();
void destroyList();
};

}
void doubleLinkedList::appendNodeBack(double x)
{
Node *n = new Node(x);
if( back == NULL)
{
front = n;
back = n;
}
else
{
back->N = n;
n->P = back;
back = n;
}

}

void doubleLinkedList::dispNodesForward()
{
Node *temp = front;
cout << "\n\nNodes in forward order:" << endl;
while(temp != NULL)
{
cout << temp->value << " " ;
temp = temp->N;
}
}
void doubleLinkedList::dispNodesReverse()
{
Node *temp = back;
cout << "\n\nNodes in reverse order :" << endl;
while(temp != NULL)
{
cout << temp->value << " " ;
temp = temp->P;
}
}
void doubleLinkedList::destroyList()
{
Node *T = back;
while(T != NULL)
{
Node *T2 = T;
T = T->P;
delete T2;
}
front = NULL;
back = NULL;
}
int main()
{
doubleLinkedList *list = new doubleLinkedList();

//append nodes to back of the list
for( int i = 1 ; i < 4 ; i++)
list->appendNodeBack(11.0 - (1.1 * i));
cout << endl << endl;
list->dispNodesForward();
list->dispNodesReverse();

cout << endl << endl;
delete list;
return 0;
}

I don't see how front->next get an assignment. So when you print front to back how does temp = temp->next no what the next address is. Is it every time you add a node to back in memory you are adding the same node to front even though front is not being modified in the code. So when you print front to back, front is the head and front->n is the next node.

Is my understanding correct?
Last edited on
I think I under stand it. Once back->N gests an address Front->N gets the same address because in the beginning they are at the same address. So that starts the order. So when you go Front->N->N it knows to go to the next spot on memory.

If this is not correct please tell me.
Topic archived. No new replies allowed.