I'm trying to figure out double linked lists. I'm looking for any type of comments you might have for my code. Anything that will make it better.
#include <iostream>
using std::cout;
using std::endl;
using std::clog;
using std::cerr;
class List {
private:
struct node{
node* n_next;
node* n_prev;
int n_data;
node(int data = 0)
:n_next(NULL)
,n_prev(NULL)
,n_data(data){}
};
node* m_head;
node* m_tail;
public:
List() :m_head(NULL) ,m_tail(NULL) {}
List(const List& listObj) {
m_head = m_tail = NULL;
for(node* tmp = listObj.m_head; tmp!=NULL; tmp=tmp->n_next) {
addToTail(tmp->n_data);
}
}
List& operator=(List listObj){
m_head = m_tail = NULL;
for(node* tmp = listObj.m_head; tmp!=NULL; tmp=tmp->n_next) {
addToTail(tmp->n_data);
}
}
~List() {
node* tmp = m_head;
while(tmp) {
m_head = tmp;
tmp = tmp->n_next;
delete m_head;
}
}
void addToHead(int data = 0) {
if(m_head) {
node* tmp = new node();
tmp->n_data = data;
tmp->n_next = m_head;
m_head->n_prev = tmp;
m_head = tmp;
//clog << "+m_head @ " << m_head <<endl;
} else {
node* tmp = new node();
tmp->n_data = data;
m_head = tmp;
m_tail = tmp;
//clog << "+m_head @ " << m_head <<endl;
}
}
void addToTail(int data = 0) {
if(m_head) {
node* tmp = new node();
tmp->n_data = data;
tmp->n_prev = m_tail;
m_tail->n_next = tmp;
m_tail = tmp;
//clog << "+m_tail @ " << m_tail <<endl;
} else {
node* tmp = new node();
tmp->n_data = data;
m_head = tmp;
m_tail = tmp;
//clog << "+m_head @ " << m_head <<endl;
}
}
void addAfterN(int pos = 0, int data = 0) {
if( pos == 0 ) {addToHead(data); return;}
int i = 0;
for(node* tmp = m_head ; tmp!=NULL; tmp=tmp->n_next)
{i++;}
if( i < pos ) {
cerr << "no such node" << endl;
} else {
node* tmp = m_head;
for(int i=0; i<pos-1; i++){
tmp=tmp->n_next;
}
node* newNode = new node();
newNode->n_data = data;
newNode->n_next = tmp->n_next;
newNode->n_prev = tmp;
tmp->n_next->n_prev = newNode;
tmp->n_next = newNode;
}
}
void deleteHead(){
node* tmp = m_head;
m_head = m_head->n_next;
m_head->n_prev = NULL;
delete tmp;
}
void deleteTail(){
node* tmp = m_tail;
m_tail = m_tail->n_prev;
m_tail->n_next = NULL;
delete tmp;
}
void deleteN(int pos = 0, bool fromHead = true) {
if( pos==0 && fromHead ) {deleteHead(); return;}
if( pos==0 && !fromHead ) {deleteTail(); return;}
int sum = 0;
for(node* tmp = m_head; tmp!=NULL; tmp=tmp->n_next) {sum++;}
if( sum<=pos ) { cerr << "no such node" << endl; return; }
else {
if(fromHead) {
node* tmp = m_head;
for(int i=0; i<pos; i++){
tmp = tmp->n_next;
}
if(tmp->n_prev)tmp->n_prev->n_next = tmp->n_next;
if(tmp->n_next)tmp->n_next->n_prev = tmp->n_prev;
delete tmp;
} else {
node* tmp = m_tail;
for(int i=0; i<pos; i++){
tmp = tmp->n_prev;
}
if(tmp->n_prev)tmp->n_prev->n_next = tmp->n_next;
if(tmp->n_next)tmp->n_next->n_prev = tmp->n_prev;
delete tmp;
}
}
}
void printList(bool ascending = true) const {
if( ascending ){
for(node* tmp = m_head; tmp!=NULL; tmp=tmp->n_next) {
cout << tmp->n_data <<endl;
}
} else {
for(node* tmp = m_tail; tmp!=NULL; tmp=tmp->n_prev) {
cout << tmp->n_data <<endl;
}
}
}
int getN(int pos = 0, bool fromHead = true) const {
int sum = 0;
for(node* tmp = m_head; tmp!=NULL; tmp=tmp->n_next)
{sum++;}
if( sum<=pos ) {
cerr << "no such node" << endl;
return 0;
} else {
if(fromHead) {
node* tmp = m_head;
for(int i=0; i<pos; i++){
tmp=tmp->n_next;
}
return tmp->n_data;
} else {
node* tmp = m_tail;
for(int i=0; i<pos; i++){
tmp=tmp->n_prev;
}
return tmp->n_data;
}
}
}
};
int main(void) {
List foo;
foo.addToTail(1);
foo.addToTail(2);
foo.addToTail(3);
foo.printList();
return 0;
}