Programming Data Structure Implementation of singly linked list | Cpp Code Example

Implementation of singly linked list | Cpp Code Example


We have already discussed what linked list is, and why we need to use it. In this article we will talk about implementation of singly linked list.

Linked list is a dynamic memory storage that can store unlimited amount of items. We use linked list in case we need continuous inserting or deleting elements in memory. For example, browser history, or previous, next button on a web browser.

There are two types of linked list. Singly linked list – where every element store the address of next element. Doubly linked list – where every element in the list store the addresses of both previous and next element. Here we will see the cpp code example of implementation of singly linked list.

An element of linked list is called node. a node has two field, data, and next. The data field stores specific data in that node. The data can be single item or multiple items. And the next field stores the address of the next node.

No matter how many nodes present in linked list. The very first node is the head node, and the last node is the tail. If there is only one node exist in linked list then the single node is called both head and tail.

Implementation of singly linked list | Cpp Code Example

As linked list consists of node, therefore we will declare a structure that define a single node.

Struct node {
int data;
node *next;

The above structure has one variable name data for data section, and a pointer name next for storing address of next node.

Now we will create a class that will contain functions to handle the node:

Class list {
   node *head, *tail;
          head = NULL;
          tail = NULL;

Now, we will write a function in the class list that will define a node.

void newnode(int value) {
   node *temp = new node;
   temp->data = value;
   temp->next = NULL;
   if (head == NULL) {
    tail = temp;
    temp = NULL;
   } else {
     tail->next = temp;
     tail = temp;

Here temp is a pointer type, which we already has defined in list class. Then we inserted value in its data field using parameter. Here, next the address field is declared NULL, because it would be the last node.

Then we inserted a condition in the function, because we want to know what would happen, if the linked list is empty? We know that head points to the first node of linked list. Therefore, if head is equal to NULL, then we can say that the linked list is empty (here temp=NULL; means list is empty). Otherwise new node will be created at the end of linked list.

In this case, passing the address of new node to the next pointer of the tail node. The tail pointer should always point to the last node. So we will make our tail pointer equal to new node.

At this point we already have a working linked list. Now below function will display nodes in linked list. 

Display function

void display () {
node *temp = new node;
temp = head;

while (temp != NULL) {
std::cout<< temp->data<<“\t”;

read details about implementation of singly linked list, insertion, and deletion here >>

learn more data structure here >>


Please enter your comment!
Please enter your name here

Latest Articles

Property Decorator | Getters Setters and Deleters in Python

In this article, we will talk about the Property Decorator in Python. It enables the class functionality...

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap comparing to other languages. It stores data as a key-value...

Hash Table | Indexing | Hashing Algorithm | Python Implementation

This article will talk about a high-level view of the Hash Table. As a programmer, this technique...

Eigenvector Eigenvalue | Linear Algebra Fundamentals

Eigenvector ($bar{v}$) in linear algebra is a non-zero vector (matrix) that doesn't change its direction during linear...

Pivot Table | Microsoft Excel | Create Data Insight Easily

Pivot table in microsoft Excel is an useful function that gives us a way to create insight...

Macro Function in Microsoft Excel | Automate Repetitive Task

This article we will talk about the Macro. It is a function in microsoft excel which basically...

Must read

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap...

You might also likeRELATED
Recommended to you