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 For Insertion Sort In A Singly Linked List We have discussed Insertion Sort for arrays. In this article we are going to discuss Insertion Sort for linked list. Below is a simple insertion sort algorithm for a linked list. 1) Create an empty sorted (or result) list. 2) Traverse the given list, do following for every node. ......a) Insert curr
5 min read
C++ Program For Writing A Function To Delete A Linked List Algorithm For C++:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C++ // C++ program to delete a linked list #include <bits/stdc++.h> using namespace std
2 min read
Linked List C/C++ Programs The Linked Lists are linear data structures where the data is not stored at contiguous memory locations so we can only access the elements of the linked list in a sequential manner. Linked Lists are used to overcome the shortcoming of arrays in operations such as deletion, insertion, etc. In this ar
2 min read
C++ Program For Writing A Function To Get Nth Node In A Linked List Write a C++ Program to GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before m
4 min read
C++ Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes
5 min read
How to Print Linked List in C++? A Linked List is a linear data structure that consists of nodes having a data member and a pointer to the next node. The linked list allows the users to store data of the same type in non-contiguous memory locations through dynamic memory allocation. In this article, we will learn how to print a lin
2 min read
C++ Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g
4 min read
C++ Program For Moving Last Element To Front Of A Given Linked List Write a function that moves the last element to the front in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4. Algorithm: Traverse the list till the last node. Use two pointers: one t
3 min read
C++ Program for Deleting a Node in a Linked List Write a C++ program to delete a node from the given link list.ExamplesInput: Linked List: 10 -> 20 -> 30 -> 40 -> 50, Position to delete: 3Output: 10 -> 20 -> 40 -> 50Explanation: The node at position 3 is removed. The list then connects node 20 directly to node 40.Input: Linked
6 min read
How to Implement Iterator for a Doubly Linked List in C++? In C++, iterators are used to access elements of a container sequentially without exposing the underlying representation. When working with data structures like a doubly linked list, implementing an iterator can greatly enhance the usability and efficiency of the list as it allows users to traverse
4 min read