Appending a node to a circular doubly linked list

The problem is in the appendNode function. If I do a doubly linked list only it works fine,but when I connect the head and tail nodes the program crashes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
template<typename T>
class Dlist
{
	private:
		struct Node
		{
			T data;
			Node *previous=NULL;
			Node *next=NULL;
		};
		Node *head;
		Node *tail;
	public:
		Dlist();
		~Dlist();
		void appendNode(T);
		void insertNode(T);
		void traverse();
		void deleteNode(T);
};


int main()
{
	Dlist<int>List;
	List.appendNode(5);
	List.appendNode(4);	
}

  template<typename T>
void Dlist<T>::appendNode(T num)
{
	Node *newNode=new Node;
	newNode->data=num;
	
	if(!head)
	head=newNode;
	
	else
	{
	Node *nodePtr=head;
	while(nodePtr->next)
		nodePtr=nodePtr->next;
	
	tail=newNode;
	nodePtr->next=tail;

	tail->previous=nodePtr;
	tail->next=head;
        head->previous=tail;
    }
}
nevermind.
Topic archived. No new replies allowed.