Program to Implement Singly Linked List in C++ Using Class
Last Updated :
11 Jan, 2025
A singly linked list is a linear data structure where each element (node) points to the next element in the sequence. It consists of nodes, with each node having two components: a data part to store the value and a next pointer part to store the address of the next node.
Traditionally, we represent the linked list node as struct or POD class, where it only contains the data field and next pointer. However, C++ classes allow you to encapsulate all the related functionality into a single type providing better modularity, data abstraction, and the ability to maintain and extend code more effectively.
Representation of Singly Linked List as a Class
To represent a singly linked list as a class, we need to first define a Node type that represents a single node.
class Node {
int data;
Node* next;
}
We then define the class that contains the pointer to the head of the linked list as data member and all basic operation functions. For now, we will only include the insertAtHead() and print() function for simplicity.
class LinkedList {
Node* head;
public:
void insertAtHead(){ ... }
void print() { ... }
}
We can add more functions such as accessing element at particular position, search and delete operations, etc. according to our requirements.
To know how to insert element at head and print linked list, refer to the following articles:
Implementing data structures like linked lists is fundamental for developing efficient applications.
Implementation
C++
// C++ program to implement singly linked list using a class
#include <iostream>
using namespace std;
// Node class to represent a node of the linked list.
class Node {
public:
int data;
Node *next;
// Default constructor
Node() {
data = 0;
next = NULL;
}
// Parameterised Constructor
Node(int data) {
this->data = data;
this->next = NULL;
}
};
// Linked list class to implement a singly linked list
class Linkedlist {
Node *head;
public:
// Default constructor
Linkedlist() {
head = NULL;
}
// Function to insert a node at the start of the
// linked list
void insertAtHead(int data) {
// Create the new Node
Node *newNode = new Node(data);
// Assign to head of the list is empty
if (head == NULL) {
head = newNode;
return;
}
// Insert the newly created linked list at the head
newNode->next = this->head;
this->head = newNode;
}
// Function to print the linked list.
void print() {
Node *temp = head;
// Check for empty list
if (head == NULL) {
cout << "List empty" << endl;
return;
}
// Traverse the list
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}
};
int main() {
// Creating a LinkedList object
Linkedlist list;
// Inserting nodes
list.insertAtHead(4);
list.insertAtHead(3);
list.insertAtHead(2);
list.insertAtHead(1);
cout << "Elements of the list are: ";
// Print the list
list.print();
cout << endl;
return 0;
}
OutputElements of the list are: 1 2 3 4
Time Complexity: O(N)
Auxiliary Space: O(N)
Conclusion
A linked list that is implemented as a class have following advantages:
- Encapsulation hides internal details, protecting the list's structure and ensuring only controlled access.
- Organizes all related operations (insertion, deletion, display) within the class, making the code more readable and maintainable.
- Supports extending functionality through derived classes, promoting code reuse.
In C++, STL contains are all implemented using classes.
Similar Reads
C++ Program to Implement Queue using Linked List Queue is the fundamental data structure that follows the First In, First Out (FIFO) principle where the elements are added at the one end, called the rear and removed from other end called the front. In this article, we will learn how to implement queue in C++ using a linked list. Queue Using Linked
5 min read
Implementing Iterator pattern of a single Linked List STL is one of the pillars of C++. It makes life a lot easier, especially when your focus is on problem-solving and you donât want to spend time implementing something that is already available which guarantees a robust solution. One of the key aspects of Software Engineering is to avoid reinventing
10 min read
Different Ways to Initialize a List in C++ STL Initializing a list means assigning some initial values to the list elements. In this article, we will learn different methods to initialize the list in C++. Letâs start from the easiest method:The easiest way to initialize a list is by passing the initial values inside an initializer list to its co
3 min read
Priority Queue of Lists in C++ with Examples Priority Queue Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}). Functio
5 min read
Circular Linked List in C++ A circular linked list is a linear data structure similar to a singly linked list where each node consists of two data members data and a pointer next, that stores the address of the next node in the sequence but in a circular linked list, the last node of the list points to the first node instead o
10 min read
How to create a List with Constructor in C++ STL Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, list has slow traversal, but once a position has been found, insertion and deletion are quick. Normally, when we say a List, we talk about doubly linked list. For implementing a singly linked list, we u
2 min read