Last active
September 15, 2022 17:07
-
-
Save canavci2016/9aa49fa395484a26ca25eb03444ce83f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <functional> | |
class Node | |
{ | |
private: | |
int data; | |
Node *next; | |
public: | |
Node() : data(0), next(NULL) {} | |
Node(int data) : data(data), next(NULL) {} | |
friend class LinkedList; | |
}; | |
class LinkedList | |
{ | |
Node *head; | |
private: | |
Node *iterateThrough(std::function<void(Node *)> callback); | |
public: | |
LinkedList() : head(NULL) {} | |
LinkedList &insert(int); | |
void printList(); | |
void deleteNode(u_int16_t index); | |
int getCount(); | |
}; | |
LinkedList &LinkedList::insert(int data) | |
{ | |
Node *node = new Node(data); | |
if (head == NULL) | |
{ | |
head = node; | |
return *this; | |
} | |
Node *temp = iterateThrough([](Node *temp) {}); | |
temp->next = node; | |
return *this; | |
} | |
void LinkedList::printList() | |
{ | |
int length = getCount(); | |
if (length == 0) | |
{ | |
std::cout << "List empty" << std::endl; | |
return; | |
} | |
iterateThrough([](Node *temp) | |
{ std::cout << temp->data << std::endl; }); | |
} | |
void LinkedList::deleteNode(u_int16_t index) | |
{ | |
int length = getCount(); | |
if (length == 0) | |
{ | |
std::cout << " List empty." << std::endl; | |
return; | |
} | |
if (index > length | index < 0) | |
{ | |
std::cout << "out of range" << std::endl; | |
return; | |
} | |
Node *temp = head; | |
Node *prev = NULL; | |
if (index == 0) // if the first node is deleted. we will reassign the head pointer | |
{ | |
if (temp->next != NULL) | |
head = temp->next; | |
delete temp; | |
return; | |
} | |
for (int i = 0; i < length; i++) | |
{ | |
if (i == index) | |
{ | |
prev->next = (temp->next == NULL) ? NULL : temp->next; | |
delete temp; | |
break; | |
} | |
prev = temp; | |
std::cout << "prev value is :" << prev->data << std::endl; | |
temp = temp->next; | |
} | |
} | |
int LinkedList::getCount() | |
{ | |
if (head == NULL) | |
return 0; | |
int length = 0; | |
iterateThrough([&length](Node *temp) mutable | |
{ length++; }); | |
return length; | |
} | |
Node *LinkedList::iterateThrough(std::function<void(Node *)> callback) | |
{ | |
Node *temp = head; | |
while (1) | |
{ | |
callback(temp); | |
if (temp->next == NULL) | |
break; | |
temp = temp->next; | |
} | |
return temp; | |
} | |
int main() | |
{ | |
LinkedList l; | |
l.insert(23).insert(24).insert(25).insert(26).insert(27).insert(28).insert(29).insert(30).insert(31); | |
l.printList(); | |
l.deleteNode(8); | |
std::cout << "--------------------------------" << std::endl; | |
l.printList(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the projects turned into public