I've tried to create my best at creating a custom Linked List implementation in C++. This is my code, would love to get feedback on it!
DATA_TYPE is a macro defined elsewhere.
struct Node
{
DATA_TYPE data;
Node *next;
};
void addEnd(DATA_TYPE value, Node *&head, Node *&tail)
{
Node *newNode = new Node;
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) head = tail = newNode;
else {
tail->next = newNode;
tail = newNode;
}
}
void addBeginning(DATA_TYPE value, Node *&head, Node *&tail)
{
Node *newNode = new Node;
newNode->data = value;
if (head == nullptr) {
newNode->next = nullptr;
head = tail = newNode;
} else {
newNode->next = head;
head = newNode;
}
}
void addSpecific(DATA_TYPE value, DATA_TYPE location, Node *&head, Node *&tail)
{
Node *newNode = new Node;
newNode->data = value;
if (head == nullptr) {
newNode->next = nullptr;
head = tail = newNode;
} else {
Node *temp = head;
while (temp->data != location) {
if (temp->next == nullptr) {
std::cout << std::endl << std::endl;
std::cout << "\t" << "LL-> Node (" << location << ") Not Found In The List.";
std::cout << std::endl << std::endl;
return;
}
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
if (temp->next == nullptr) tail = newNode;
}
}
void deleteBeginning(Node *&head)
{
if (head == nullptr) {
std::cout << std::endl << std::endl;
std::cout << "\t" << "LL-> The List Is Empty. Deletion Not Possible.";
std::cout << std::endl << std::endl;
return;
}
Node *temp = head;
if (temp->next == nullptr) {
head = nullptr;
delete temp;
} else {
head = head->next;
delete temp;
}
}
void deleteEnd(Node *&head, Node *&tail)
{
if (head == nullptr) {
std::cout << std::endl << std::endl;
std::cout << "\t" << "LL-> The List Is Empty. Deletion Not Possible.";
std::cout << std::endl << std::endl;
return;
}
Node *temp = head;
if (temp->next == nullptr) {
head = nullptr;
delete temp;
} else {
while (temp->next != tail) temp = temp->next;
tail = temp;
temp = temp->next;
tail->next = nullptr;
delete temp;
}
}
void deleteSpecific(DATA_TYPE node_location, Node *&head, Node *&tail)
{
if (head == nullptr) {
std::cout << std::endl << std::endl;
std::cout << "\t" << "LL-> The List Is Empty. Deletion Not Possible.";
std::cout << std::endl << std::endl;
return;
}
Node *temp1 = head, *temp2;
while (temp1->data != node_location) {
if (temp1->next == nullptr) {
std::cout << std::endl << std::endl;
std::cout << "\t" << "LL-> Given Node Not Found. Deletion Not Possible.";
std::cout << std::endl << std::endl;
return;
}
temp2 = temp1;
temp1 = temp1->next;
}
if (temp1->data == node_location) {
if (head == temp1 && tail == temp1) {
head = nullptr;
delete temp1;
} else if (temp1 == head) {
head = head->next;
delete temp1;
} else if (temp1 == tail) {
temp2->next = nullptr;
tail = temp2;
delete temp1;
} else {
temp2->next = temp1->next;
delete temp1;
}
}
}
void display(Node *head)
{
if (head == nullptr) {
std::cout << std::endl << std::endl;
std::cout << "\t" << "LL-> The List Is Empty.";
std::cout << std::endl << std::endl;
return;
}
Node *temp = head;
std::cout << std::endl << std::endl << "\t" << "| ";
while (temp != nullptr) {
std::cout << temp->data << " | ";
temp = temp->next;
}
std::cout << std::endl << std::endl;
}
DATA_TYPEis a macro defined elsewhere!" Why not using a template class instead? All in all that more looks like c-code, than c++. \$\endgroup\$