I implemented a Double linked list in c++ to practice. I reviewed the code myself and I would like others perspective about the improvements that could be made. I have also noticed that I don't take fully advantage of many c++ features nor use them in my code. Any comments about significant performance improvements are also appreciated!
Header File (List.h)
#pragma once
class List {
private:
struct Node {
int x;
Node* next;
Node* prev;
};
Node * head;
Node * tail;
public:
List();
List(const int x);
void printList();
void insert(const int x);
unsigned short length();
bool deleteList();
bool isEmpty();
bool isSorted();
void reversePrint();
void sortedInsert(const int x);
List operator+(List const &obj1);
void operator+(const int x);
};
Source File (Source.cpp)
#include <iostream>
#include "List.h"
using std::cout;
List::List() {
head = nullptr;
}
List::List(const int x) {
List();
insert(x);
}
List List::operator+(List const &obj1) {
List newList;
Node* tmp = head;
Node* tmp2 = obj1.head;
while (tmp != nullptr) {
newList.insert(tmp->x);
tmp = tmp->next;
}
while (tmp2 != nullptr) {
newList.insert(tmp2->x);
tmp2 = tmp2->next;
}
return newList;
}
void List::operator+(const int x) {
insert(x);
}
void List::printList() {
if (head == nullptr) {
return;
}
Node* tmp = head;
while (tmp != nullptr) {
cout << tmp->x << " ";
tmp = tmp->next;
}
cout << '\n';
}
void List::insert(const int x) {
// If list is empty
if (head == nullptr) {
head = new Node;
head->x = x;
head->next = nullptr;
head->prev = nullptr;
return;
}
// Inserting at the end
Node* copy = head;
while (copy->next != nullptr) {
copy = copy->next;
}
// Inserting at the end
Node* tmp2 = new Node;
tmp2->x = x;
tmp2->next = nullptr;
copy->next = tmp2;
tmp2->prev = copy;
// Save the last node (tail)
tail = tmp2;
}
unsigned short List::length() {
short c = 0;
Node* copy = head;
while (copy != nullptr) {
c++;
copy = copy->next;
}
return c;
}
void List::reversePrint() {
Node* tmp = tail;
while (tmp->prev != nullptr) {
cout << tmp->x << ' ';
tmp = tmp->prev;
}
cout << tmp->x << '\n';
}
bool List::deleteList() {
if (head == nullptr)
return false;
Node* tmp = head;
Node* tmp2 = head;
head = nullptr;
while (tmp != nullptr) {
tmp = tmp->next;
delete tmp2;
tmp2 = tmp;
}
return true;
}
bool List::isEmpty() {
if (length() == 0) {
return true;
}
else {
return false;
}
}
void List::sortedInsert(const int x) {
if (isEmpty()) {
insert(x);
return;
}
Node* copy = head;
while (copy != nullptr) {
if (copy->x > x) {
//Insertar antes
Node* tmp = new Node;
tmp->x = x;
head = tmp;
tmp->next = copy;
tmp->prev = nullptr;
copy->prev = tmp;
break;
}
if (copy->next == nullptr) {
insert(x);
break;
}
if ((x >= copy->x) && (x < copy->next->x)) {
// insertar Despues
Node* tmp = new Node;
tmp->x = x;
tmp->prev = copy;
tmp->next = copy->next;
copy->next->prev = tmp;
copy->next = tmp; // -> next next
break;
}
copy = copy->next;
}
}
bool List::isSorted() {
Node* tmp = head;
while (tmp->next != nullptr) {
if (tmp->x > tmp->next->x) {
return false;
}
tmp = tmp->next;
}
return true;
}
Additional notes: The sortedInsert(x) function would only work properly if it is used with a new list. It wouldn't work if first insert(x) is used and then sortedInsert(x)