Hi,
I am doing a program using Doubly-Linked List. I am just studying C++, and still new in this field. My question are:
- How can I make a method which inserts a new node before the currently selected node, then makes the new node the current node?
- How can I make a method which inserts a new node after the currently selected node, then makes the new node the current node?
I came up with some ideas, please check it out. Your help is greatly appreciated.
doublyLinkedList.h
#ifndef _DOUBLYLINKEDLIST_H
#define _DOUBLYLINKEDLIST_H
template <class TYPE>
class doublyLinkedList
{
private:
struct Node
{
TYPE data;
Node *next;
Node *prev;
Node(TYPE t,Node* p, Node* n): data(t),prev(p),next(n){}
};
Node *head;
Node *tail;
public:
doublyLinkedList();
doublyLinkedList(TYPE emptyValue);
~doublyLinkedList();
void InsertBefore(TYPE data);
void InsertAfter(TYPE data);
TYPE GetValue();
TYPE NextValue();
TYPE PreviousValue();
bool empty() const {return(!head || !tail );}
operator bool() const{return !empty();}
};
#endif // _DOUBLYLINKEDLIST_H
I already created a method for insert before the currently selected node, but I am not sure it is right or not. And I need some ideas for insertAfter method.
template <class TYPE>
void doublyLinkedList<TYPE>::insertBefore(TYPE data)
{
if(empty())
{
head = new Node(data, NULL, NULL);
head->prev = head->next = head;
}
else
{
head = new Node(data,head->prev,head->next);
head->prev->next = head->next->prev = head
}
}