VOOZH about

URL: https://www.geeksforgeeks.org/cpp/program-to-implement-singly-linked-list-in-c-using-class/

⇱ Program to Implement Singly Linked List in C++ Using Class - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to Implement Singly Linked List in C++ Using Class

Last Updated : 28 May, 2026

A singly linked list is a linear data structure where each node stores data and a pointer to the next node in the sequence. It allows dynamic memory usage and efficient insertion or deletion of elements.

  • Each node contains two parts: a data field to store the value and a next pointer to store the address of the next node.
  • In C++, classes can encapsulate both data and related operations, improving modularity, abstraction, and maintainability.

Example: 10 -> 20 -> 30 -> NULL

Here, each node points to the next node, and the last node points to NULL.

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.

Example: Program to implement singly linked list using a class


Output
Elements of the list are: 1 2 3 4 
  • Time Complexity: O(N)
  • Auxiliary Space: O(N)

Working

The program creates a singly linked list using two classes: Node and LinkedList.

  • The Node class stores the data and the address of the next node.
  • The LinkedList class manages the linked list using a head pointer.

Working Process

  • A new node is created using the Node class.
  • insertAtHead() inserts the node at the beginning of the list.
  • The head pointer is updated to point to the new node.
  • print() traverses the linked list from head to end and prints all elements.

Example: If we insert 1, 2, 3, and 4 using insertAtHead(), the linked list becomes: 1 -> 2 -> 3 -> 4

The print() function visits each node one by one and displays the data.

Search and Delete Operations

A singly linked list can also perform operations like searching and deleting nodes.

Search Operation

The search operation traverses the linked list from the head node until the required value is found.

  • Start from the head node
  • Compare each node’s data with the target value
  • Stop when the value is found or the list ends

Example: To search for 3 in: 1 -> 2 -> 3 -> 4

The traversal checks nodes one by one until node 3 is found.

Delete Operation

The delete operation removes a node from the linked list by changing the links between nodes.

  • Find the node to delete
  • Update the previous node’s next pointer
  • Remove the target node from memory

Example: Deleting 3 from: 1 -> 2 -> 3 -> 4

Results in: 1 -> 2 -> 4

Comment
Article Tags:
Article Tags: