This module examines two useful
class
implementations of dynamic structures:
- a "safe array" that performs its own bounds checking and
- a singly linked list.
In addition, you will take a look at an efficient object disposal routine for large aggregate types. You will learn how to:
- Implement a bounds-checking array using classes
- Group classes in a hasa relationship
- Implement a singly linked list using classes
- Use reference counting for efficient object disposal
Creating a singly linked list in C++ involves defining a `Node` structure and a `LinkedList` class that manipulates these nodes. The `Node` structure typically contains the data you want to store and a pointer to the next node. The `LinkedList` class manages the operations such as adding, removing, and searching for elements within the list.
Below is an example implementation of a singly linked list in C++ with basic operations such as adding to the end, printing the list, and a constructor/destructor for proper memory management.
#include <iostream>
// Definition of the Node structure
struct Node {
int data; // The data held by the Node
Node* next; // Pointer to the next node in the list
// Constructor to initialize a Node with data and optionally set the next node
Node(int data, Node* next = nullptr) : data(data), next(next) {}
};
// Definition of the LinkedList class
class LinkedList {
public:
Node* head; // The head pointer points to the first element in the list
// Constructor to initialize an empty list
LinkedList() : head(nullptr) {}
// Destructor to free the memory used by the list
~LinkedList() {
Node* current = head;
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}
head = nullptr;
}
// Function to add a new element to the end of the list
void append(int data) {
if (head == nullptr) {
head = new Node(data);
} else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = new Node(data);
}
}
// Function to print all the elements in the list
void print() const {
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
};
int main() {
LinkedList list;
list.append(1);
list.append(2);
list.append(3);
list.print(); // Should print: 1 2 3
return 0;
}
Explanation:
- Node Structure: Represents each element in the list. It contains the data (`int data`) and a pointer to the next node (`Node* next`).
- LinkedList Class: Manages the list. It only contains a single member `Node* head`, which points to the first element of the list.
- LinkedList Constructor: Initializes the `head` pointer to `nullptr`, indicating that the list is initially empty.
- LinkedList Destructor: Iterates through the list, deallocating memory used by each node to prevent memory leaks.
- `append` Function: Adds a new node to the end of the list. If the list is empty (`head == nullptr`), it creates a new node and assigns it to `head`. Otherwise, it traverses the list until it finds the last node and attaches a new node to its `next` pointer.
- `print` Function: Iterates over each element in the list, starting from `head`, and prints the `data` of each node.
This code provides a basic structure for a singly linked list, and you can extend it with more functionalities such as deleting nodes, finding elements, or inserting elements at a given position.