Last active
May 4, 2021 20:24
-
-
Save benapie/0e7d1cebe2801d387609e20ec594646a to your computer and use it in GitHub Desktop.
Simple singly linked list implementation in C++ with find, delete, and insert functions.
This file contains 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
/** | |
* Simple node data structure for a singly linked list. | |
*/ | |
class Node { | |
public: | |
Node *next = nullptr; | |
int data; | |
}; | |
/** | |
* Simple data structure for a singly linked list which utilises the node data structure. | |
*/ | |
class LinkedList { | |
public: | |
Node *head = nullptr; | |
int size = 0; | |
LinkedList() = default; | |
/** | |
* Insert a new node with specified data into the head of the linked list. | |
* @param data the data to insert into the list. | |
*/ | |
void Insert(int data) { | |
Node *temp = new Node(); | |
temp->data = data; | |
temp->next = head; | |
head = temp; | |
size++; | |
} | |
/** | |
* Finds the node with specified data in the list. | |
* @param data the data to find in the list. | |
* @return the index of the node with the data or -1 if not found. | |
*/ | |
int Find(int data) { | |
Node *temp = head; | |
for (int i = 0; i < size; ++i) { | |
if (temp->data == data) { | |
return i; | |
} | |
temp = temp->next; | |
} | |
return -1; | |
} | |
/** | |
* Delete the node with specified data in the list. | |
* Will delete the first node that it finds and then terminate. | |
* @param data the data to delete from the list. | |
*/ | |
void Delete(int data) { | |
if (head->data == data) { | |
head = head->next; | |
size--; | |
} else { | |
Node *node = head->next; | |
Node *prev = head->next; | |
for (int i = 1; i < size; ++i) { | |
if (node->data == data) { | |
prev->next = node->next; | |
size--; | |
return; | |
} | |
prev = node; | |
node = node->next; | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment