1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
#include <iostream> using namespace std; // Node structure for the linked list struct Node { int data; Node* next; Node(int value) : data(value), next(nullptr) {} }; // Linked list class class LinkedList { private: Node* head; public: LinkedList() : head(nullptr) {} ~LinkedList() { Node* current = head; while (current != nullptr) { Node* nextNode = current->next; delete current; current = nextNode; } } // Add a node to the end of the list void append(int value) { Node* newNode = new Node(value); if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } // Print all nodes in the list void printList() const { Node* current = head; while (current != nullptr) { cout << current->data << " -> "; current = current->next; } cout << "nullptr" << endl; } // Delete a node with a specific value void deleteValue(int value) { Node* current = head; Node* prev = nullptr; // Check if the node to be deleted is the head if (current != nullptr && current->data == value) { head = current->next; delete current; return; } // Search for the node to be deleted while (current != nullptr && current->data != value) { prev = current; current = current->next; } // If the value was not found if (current == nullptr) { cout << "Value not found in the list." << endl; return; } // Unlink the node from the linked list prev->next = current->next; delete current; } }; int main() { LinkedList list; // Append values to the list list.append(10); list.append(20); list.append(30); list.append(40); // Print the list cout << "Linked List: "; list.printList(); // Delete a node and print the list again list.deleteValue(20); cout << "After deleting 20: "; list.printList(); return 0; } |
Explanation
- Node Structure: Defines the building block of the linked list.
data
: Holds the value of the node.next
: Points to the next node in the list.
- LinkedList Class: Manages the linked list operations.
- Constructor: Initializes
head
tonullptr
, indicating an empty list. - Destructor: Deletes all nodes to free up memory when the list is destroyed.
append
Method: Adds a new node with a given value to the end of the list.printList
Method: Prints all nodes in the list, showing their values and the end of the list asnullptr
.deleteValue
Method: Deletes the first node with a specified value from the list. Handles cases where the node to delete is the head or somewhere else in the list.
- Constructor: Initializes
- Main Function:
- Creates an instance of the
LinkedList
class. - Appends several values to the list.
- Prints the list to show its current state.
- Deletes a specific value from the list and prints the updated list.
- Creates an instance of the