I am currently in between semesters at school and am using that time to improve on my knowledge of C++ classes by making a node class and a linked list class that inherits from the node class. There is a list of areas in need of improvement at the start of the code, but I was also hoping to get some external input on this as well. Once these two classes are more sounds I'm going to implement a stack and queue class as well. I am building this code using Visual Studio 2019 on a Windows 10 operating system, and I do not believe it is optimized for cross-OS use.
If you believe that the int main()
function I use to test the member functions is necessary to giving accurate feedback, I would be happy to comply.
//LinkedList.h
#include <stdio.h>
#pragma once
/*PLACES TO START AND IMPROVE ON CODE
Proper ctor and dtor implementation,
Replace int data value with template <typename T>,
Const Correctness,
keep default constructor in protected,
ostream operator
set_tail() and get_tail() function utilization
return node* from back_list() and push_back()
*/
const int UnkD = 999;//Variable for node() default constructor init
//NODE class code from http://cplusplus.com/forum/beginner/194591
class node {
protected:
int data;
node* prev;
node* next;
public:
node() : data(UnkD), prev(nullptr), next(nullptr) { printf("default node() ctor called... \n"); } //Default Constructor
node(const int & d,node* p,node* n): data(d), prev(p), next(n) { printf("node() ctor with args called... \n"); } //Constructor With Args
node(const node& rhs) {
printf("node() copy ctor called... \n");
data = rhs.data;
prev = rhs.prev;
next = rhs.next;
}; //Copy Constructor
node & operator = (const node& rhs) { //Copy Operator
printf("Copy Operator");
if (this != &rhs) {
data = rhs.data;
prev = rhs.prev;
next = rhs.next;
}
return *this;
}
void set_data(int x) { data = x; }
void set_prev(node* node) { prev = node; }
void set_next(node* node) { next = node; }
int get_data() { return this->data; }
node* get_prev() { return this->prev; }
node* get_next() { return this->next; }
~node() { printf("~node() dtor called...\n"); delete[] this; };
};
class LL: public node {
private:
node* head;
node* tail;
public:
//Class Ctor Functions
LL() : head(nullptr), tail(nullptr) { printf("LL deault constructor \n"); }
LL(node* head, node* tail) : head(nullptr), tail(nullptr) { printf("LL constructor with assignments \n"); }
void set_head(node* node) { head = node; }
void set_tail(node* node) { tail = node; }
node* get_head() { return this->head; }
node* get_tail() { return this->tail; }
//Create and Destroy Fucntions
node* create_node(int d);
void delete_list(node* list);
void delete_selected(node* list, int selection);
//Add and Remove Node Functions
node* push_front(node* list, node* node);
node* push_back(node* list, node* node);
node* pop_front(node* list);
void pop_back(node* list);
void append_before(node* list, node* node, int selection);
void append_after(node* list, node* node, int selection);
//View List Functions
void front_list(node* list);
void back_list(node* list);
void print_fwd(node* list);
void print_bwd(node* list);
void list_selected(node* list, int selection);
~LL() {
printf("LL destructor \n");
while (head)
{
node* tmp = head->get_next();
delete[] tmp;
}
}
};
//create and destroy functions
node* LL::create_node(int d) { return new node{ d, nullptr, nullptr }; }
void LL::delete_list(node* list) {
while (list)
{
node* tmp = list;
list = list->get_next();
delete[] tmp;
}
printf("List Deleted. \n");
}
void LL::delete_selected(node* list, int selection) {
int count = 0;
while (count != (selection - 1)) {
list = list->get_next();
count++;
}
node* tmp = list;
list = list->get_prev();
list->set_next(tmp->get_next());
list = tmp->get_next();
list->set_prev(tmp->get_prev());
//delete[] tmp;
}
//add and remove node functions
node* LL::push_front(node* list, node* node) {
if (!list) { return node; }
node->set_next(list);
list->set_prev(node);
list = node;
return list;
}
node* LL::push_back(node* list, node* node) {
if (list->get_next() == nullptr) { list->set_next(node); node->set_prev(list); }
else {
while (list->get_next()) { list = list->get_next(); }
list->set_next(node); node->set_prev(list);
}
return node;
}
node* LL::pop_front(node* list) {
//node* tmp = list;
list = list->get_next();
list->set_prev(nullptr);
//delete[] tmp;
return list;
}
void LL::pop_back(node* list) {
node* tmp;
if (list->get_next() == nullptr) {
//tmp = list;
list = list->get_prev();
list->set_next(nullptr);
}
else {
while (list->get_next()) { list = list->get_next(); }
//tmp = list;
list = list->get_prev();
list->set_next(nullptr);
}
//delete[] tmp;
}
void LL::append_before(node* list, node* n, int selection) {
int count = 0;
while (count != (selection - 1)) {
list = list->get_next();
count++;
}
n->set_next(list);
n->set_prev(list->get_prev());
node* tmp = list->get_prev();
tmp->set_next(n);
list->set_prev(n);
}
void LL::append_after(node* list, node* n, int selection) {
int count = 0;
while (count != (selection - 1)) {
list = list->get_next();
count++;
}
n->set_next(list->get_next());
node* tmp = list->get_next();
tmp->set_prev(n);
n->set_prev(list);
list->set_next(n);
}
//View list functions
void LL::front_list(node* list) {
if (list->get_prev() == nullptr) { printf("The data at the front of the list is %d \n", list->get_data()); }
else {
while (list->get_prev()) { list = list->get_prev(); }
printf("The data at the front of the list is %d \n", list->get_data());
}
}
void LL::back_list(node* list) {
if (list->get_next() == nullptr) { printf("The data at the back of the list is %d \n", list->get_data()); }
else {
while (list->get_next()) { list = list->get_next(); }
printf("The data at the back of the list is %d \n", list->get_data());
}
}
void LL::print_fwd(node* list) {
int count = 0;
while (list)
{
printf("count [%d] : %d \n", count, list->get_data());
list = list->get_next();
count++;
}
}
void LL::print_bwd(node* list) {
int count = 0;
while (list->get_next()) { list = list->get_next(); count++; }
while (list) {
printf("count [%d] : %d \n", count, list->get_data());
list = list->get_prev();
count--;
}
}
void LL::list_selected(node* list, int selection) {
int count = 0;
while (count != (selection - 1)) {
list = list->get_next();
count++;
}
printf("node %d: [%d] %d \n", selection, (selection - 1), list->get_data());
}
this
) object.delete[] this;
does not belong in a destructor. Correct these bugs, then edit your question to include the fixed code. \$\endgroup\$