C++ Singly Linked List Simulation Implementation
In many programming tasks, especially in technical interviews or system-level programming, you may be required to implement data structures from scratch. This helps deepen your understanding of how they work under the hood. In this article, we will simulate a singly linked list in C++ without using the STL list
class.
We’ll build a basic singly linked list class that supports the following operations:
- Adding a node to the end
- Deleting a node by value
- Displaying the list
- Searching for a value
Singly Linked List: Basic Concept
A singly linked list is a collection of nodes where each node contains:
- Data
- Pointer to the next node
Visually, it looks like this:
[Data|Next] -> [Data|Next] -> [Data|Next] -> nullptr
Implementation
Here’s the complete code to simulate a basic singly linked list:
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class SinglyLinkedList {
private:
Node* head;
public:
SinglyLinkedList() : head(nullptr) {}
~SinglyLinkedList() {
Node* current = head;
while (current != nullptr) {
Node* toDelete = current;
current = current->next;
delete toDelete;
}
}
void append(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* current = head;
while (current->next) {
current = current->next;
}
current->next = newNode;
}
void remove(int val) {
if (!head) return;
if (head->data == val) {
Node* toDelete = head;
head = head->next;
delete toDelete;
return;
}
Node* current = head;
while (current->next && current->next->data != val) {
current = current->next;
}
if (current->next) {
Node* toDelete = current->next;
current->next = current->next->next;
delete toDelete;
}
}
bool search(int val) {
Node* current = head;
while (current) {
if (current->data == val)
return true;
current = current->next;
}
return false;
}
void display() {
Node* current = head;
while (current) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "nullptr" << std::endl;
}
};
Example Usage
Let’s put our SinglyLinkedList
class to use:
int main() {
SinglyLinkedList list;
list.append(10);
list.append(20);
list.append(30);
std::cout << "List after adding elements: ";
list.display();
std::cout << "Searching for 20: " << (list.search(20) ? "Found" : "Not Found") << std::endl;
std::cout << "Searching for 40: " << (list.search(40) ? "Found" : "Not Found") << std::endl;
list.remove(20);
std::cout << "List after removing 20: ";
list.display();
return 0;
}
Output:
List after adding elements: 10 -> 20 -> 30 -> nullptr
Searching for 20: Found
Searching for 40: Not Found
List after removing 20: 10 -> 30 -> nullptr
Summary
This implementation of a singly linked list demonstrates how a basic dynamic data structure can be built in C++ without relying on STL containers. By managing memory manually and understanding pointers, you gain insight into how more complex systems like STL’s std::list
work internally.
You can extend this simulation further to support:
- Insertion at a specific position
- Reverse traversal (requires doubly linked list)
- Sorting the list
Mastering such fundamentals is essential for systems programming, embedded development, and interviews.