SinglyLinkedListFunctions
SinglyLinkedListFunctions
using namespace std; // Allows us to use standard library names like cout without
needing the std:: prefix
// Definition of the Node structure (used for creating nodes in a linked list)
struct Node {
// Data part of the node
int data;
// Traverse the linked list until the end (when current becomes nullptr)
while (current != nullptr) { // Loop continues as long as 'current' is not
nullptr
// If the key is not found after traversing the list, return false
return false;
}
// Traverse the linked list until the end (when current becomes nullptr)
while (current != nullptr) { // Loop continues as long as 'current' is not
nullptr
// Point the new node's 'next' to the current head of the linked list
newNode->next = head;
// Return the new node, which now becomes the new head of the list
//VIMP - you need to save this in the head now
return newNode;
}
// If the list is empty (head is nullptr), the new node becomes the head
if (head == nullptr) {
return newNode; // Return the new node as the new head
}
// Iterate through the list to find the node with the specified key
while (current != nullptr) {
if (current->data == key) {
break; // Break the loop if the node with the key is found
// this ennsures that current has the details of the key node
}
current = current->next; // Move to the next node
}
// Insert the new node after the node with the specified key
newNode->next = current->next; // Point the new node to the next node after
'current'
current->next = newNode; // Link 'current' to the new node
// Special case: if the key is in the head node, insert before the head
if (head->data == key) {
newNode->next = head; // Point the new node to the current head
return newNode; // Return the new node as the new head
}
// Traverse the list to find the node with the specified key
while (current != nullptr) {
if (current->data == key) {
break; // Key found, exit the loop
}
prev = current; // Store the previous node
current = current->next; // Move to the next node
}
// Traverse the list to find the node just before the desired position
while (current != nullptr) {
// Function to delete the node that comes after the given node with the specified
key
Node* deleteAfterGivenNode(Node* head, int key) {
// Initialize 'current' to start at the head of the linked list
Node* current = head;
// Traverse the linked list to find the node with the specified key
while (current != nullptr) {
// If the current node's data matches the key
if (current->data == key) {
// following if, Checks if the current node has a next node to delete
if (current->next != nullptr) { // if it has then
Node* temp = current->next; // Store the node to be deleted
current->next = temp->next; // Bypass the node to be deleted
delete temp; // Free the memory of the deleted
node
}
return head; // Return the head of the list (unchanged)
}
current = current->next; // Move to the next node in the list
}
// Special case: If the node before the key is the head itself
if (head->next->data == key) {
Node* temp = head;
head = head->next; // Move head to the next node
delete temp; // Delete the old head
return head; // Return new head
}
// Traverse the list to find the node before the node with the given key
while (current->next != nullptr && current->next->next != nullptr) {
/* The condition while (current->next != nullptr && current->next->next !=
nullptr)
is used to safely traverse the linked list while ensuring that you don't
accidentally dereference a nullptr, which would lead to runtime errors or
crashes. */
// If the node two steps ahead has the key, delete the node before it
if (current->next->next->data == key) {
Node* temp = current->next; // Store the node to be deleted
current->next = temp->next; // Bypass the node to be deleted
delete temp; // Free the memory of the deleted
node
return head; // Return the head after deletion
}
current = current->next; // Move to the next node
}
// If key is not found, return the unchanged head
return head;
}
// Traverse the list to find the node just before the node to be deleted
Node* current = head;
for (int i = 1; i < position - 1 && current != nullptr; i++) {
current = current->next; // Move to the next node
}
// Update the current node's next pointer to skip over the node to be deleted
current->next = current->next->next;
// Store next
next = curr->next;
cout << "Length of list is: " << lengthOfList(head) << endl;
head = insertAtBeginning(head,12);
head = insertAtBeginning(head,13);
head = insertAtBeginning(head,900);
head = insertAtBeginning(head,23);
head = insertAtBeginning(head,339);
head = insertAtBeginning(head,450);
head = insertAtEnd(head,5000);
head = insertAfterGivenNode(head,2,12345);
head = insertBeforeGivenNode(head,1,555);
head = insertBeforeGivenNode(head,200,999);
head = insertBeforeGivenNode(head,22,999);
head = insertAtPosition(head,3,333);
head = insertAtPosition(head,1,444);
head = insertAfterGivenNode(head,2,222);
traverseList(head);
head = deleteFirstNode(head);
head = deleteLastNode(head);
head = deleteAfterGivenNode(head,2);
head = deleteBeforeGivenNode(head,2);
head = deleteAtPosition(head,9);
traverseList(head);
reverseList(head);
return 0;
}