Linked List in C/C++ Fascinating Tactics that will improvise your skills

A linked list in C/C++ is basically a linear data structure based on the concept of dynamic memory allocation. It is implemented with the help of pointers. The linked list in C and C++ tutorial is specially designed for the beginners, who are not aware of the importance of linked lists. By the end of this tutorial, you can easily implement the linked list.

 Why Wait? Explore Now!

Linked List in C and Cpp

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

1. What is a Linked List in C/C++?

As the name itself suggests, the linked list is a linear collection of similar data types in CA linked list is nothing but a linear data structure with elements pointing to the next elements in a sequence, formally referred to as nodes. Just like arrays support the feature of static allocation of memory, linked list support the feature of dynamic memory allocation.

Key takeaway: Linked lists do not store their data elements contiguously, unlike arrays.

We saw that every linked list is associated with a node. Every node in a linked list is divided into two parts:

  • The first part contains the information of the data element.
  • The second part contains the memory address of the next node in the form of a pointer, called the link.

This is how a node looks like:

Node in linked List in C

Here is a diagrammatic representation of how a linked list looks like:

Data Stored in Linked list

From the above diagrams, it is evident that the first node is called the head.

Before we start the example, you must know about Arrays in C/C++

2. Real Life Example of Linked List

Let us understand the meaning of a linked list through a simple scenario:

A student coordinator is given the responsibility to manage 20 students taken to a hill station on a school trip. Each student has his own allocated room. Their rooms are on the same floor adjacent to each other.

Now, the student coordinator is supposed to distribute the tour plan to each student. The only thing the student coordinator needs to remember is the floor and room number of the first student. The room numbers of the next 19 students can easily be found out as the rooms are adjacent to each other.

Now, the next day, due to some disparency, the hotel manager has to shuffle the rooms of the 20 students. This time they have been given random rooms that are not sequentially arranged. This time the student coordinator is supposed to distribute carnival tickets to each student but has no idea how to find all the students as they are randomly scattered in different rooms.

The student coordinator finds a solution to this problem. He finds out the room number of the first student and asks him to remember the room number of the next student, who then asks to remember the room number of the student next to him. This process goes on until each student knows the room number of the student next to him except the last student.

Employing this method helps the student coordinator to solve this problem.

In the above-stated situation, the first case is clearly analogous to that of arrays, where there is a contiguous arrangement of students. Here, the data item is the student and their memory addresses are their room numbers. By finding out the memory address of one data element, the rest of the data elements can easily be accessed.

In contrast to the first case, the second case is analogous to a linked list. Here, the first data element stores the address of its next data element to access it.

Enhance Your Fundamental Skills with Operators in C and C++

Here is a diagrammatic representation of how the above-stated situations look like:

Array anology

Linked List Anology

3. What is the Requirement of Implementing a Linked List in C/C++?

We already know how to work with arrays in C/C++, which is also a linear data structure. Now, the question arises: What is the requirement of learning how to implement a linked list in C and C++ when the same purpose is served?

Here are some of the reasons why:

3.1 DMA

DMA stands for Dynamic Memory Allocation. The linked list supports this concept which proves to be quite helpful when the number of elements and their sizes are not predetermined. The memory can be allocated during processing as and when required.

3.2 Easy modifications

Since the linked list supports the concept of dynamic memory allocation and data modifications such as insertion and deletion prove to quite easy to implement.

4. C/C++ Linked List Implementation

We can implement a linked list with the help of structures in C.

In other programming languages, like Java, classes are generally used to represent linked lists as these languages support the feature of object-oriented programming.

4.1 Declaration

Before we learn how to implement a linked list in C/C++, let us see how it to declare it. Its declaration is as follows:

struct node
{
int data_element;
struct node *next;
};

4.2 Creating a node

This is how we create a linked list in C/C++ consisting of two data elements:

struct node *first = NULL; 
struct node *second = NULL;

/* Dynamic memory allocation */

first = (struct node*)malloc(sizeof(struct node)); 
second = (struct node*)malloc(sizeof(struct node)); 

first->data_element = 10; // Assigning data to the first node 
first->next = second; // Linking the first node with the second node

second->data_element = 20; // Assigning data to the second node 
second->next = NULL; // Since third is the last node, it does not point to any other data element.
// NULL denotes the termination of the linked list

Before we move ahead, you should know how functions in C work?

4.3 Displaying

We have created the following function to display the linked list in C:

void display(struct node *n) // Function to display the linked list
{ 
while (n != NULL) 
{ 
printf("%d\n", n->data_element); 
n = n->next; 
} 
}

We have created the following function to display the linked list in C++:

void display(struct node *n) // Function to display the linked list
{ 
while (n != NULL) 
{ 
cout<< n->data_element << endl; 
n = n->next; 
} 
}

5. Example of Linked List in C

Here is a program in C that illustrates the use of a linked list:

#include<stdio.h> 
#include<stdlib.h> 
struct node 
{ 
int data_element; 
struct node *next; 
}; 
void display(struct node *n) // Function to display the linked list
{ 
while (n != NULL) 
{ 
printf("%d\n", n->data_element); 
n = n->next; 
} 
}
int main() 
{

printf("Welcome to DatafLair tutorials!\n\n");

/* Linked list with 3 pointers: first, second, and third */

struct node *first = NULL; 
struct node *second = NULL; 
struct node *third = NULL; 

/* Dynamic memory allocation */ 
first = (struct node*)malloc(sizeof(struct node)); 
second = (struct node*)malloc(sizeof(struct node)); 
third = (struct node*)malloc(sizeof(struct node)); 

first->data_element = 10; // Assigning data to the first node 
first->next = second; // Linking the first node with the second node

second->data_element = 20; // Assigning data to the second node 
second->next = third; // Linking the second node with the third node 

third->data_element = 30; // Assigning data to the third node
third->next = NULL; // Since third is the last node, it does not point to any other data element.
// NULL denotes the termination of the linked list 
display(first); 
return 0; 
}

Code- 

Example of Linked list in C

Output-

Output

6. Example of Linked List in C++

Here is a program in C++ that illustrates the use of linked lists:

#include <iostream>
using namespace std;

struct node 
{ 
int data_element; 
struct node *next; 
}; 
void display(struct node *n) // Function to display the linked list
{ 
while (n != NULL) 
{ 
cout<< n->data_element <<endl; 
n = n->next; 
} 
}
int main() 
{

cout<<"Welcome to DatafLair tutorials!" << endl;

/* Linked list with 3 pointers: first, second, and third */

struct node *first = NULL; 
struct node *second = NULL; 
struct node *third = NULL; 

/* Dynamic memory allocation */ 
first = (struct node*)malloc(sizeof(struct node)); 
second = (struct node*)malloc(sizeof(struct node)); 
third = (struct node*)malloc(sizeof(struct node)); 

first->data_element = 10; // Assigning data to the first node 
first->next = second; // Linking the first node with the second node

second->data_element = 20; // Assigning data to the second node 
second->next = third; // Linking the second node with the third node 

third->data_element = 30; // Assigning data to the third node
third->next = NULL; // Since third is the last node, it does not point to any other data element.
// NULL denotes the termination of the linked list 
display(first); 
return 0; 
}

Code-

C++ Linked List Example

Output-

Linked List in C++ output

7. Types of Linked List in C/C++

There are basically three types of linked lists in C/C++, namely:

  1. Singly linked list- In this discussion, we will mainly focus on singly-linked lists. A singly linked list has only one reference, that is, ‘next’ that points to the memory address of the next element in the linked list.
  2. Doubly linked list- Just as a singly linked list has one reference, that is, ‘next’ that points to the memory location of the next element, a doubly linked list has two references, called ‘next’ and ‘previous’ which refers to the next node and previous node respectively.
  3. Circular linked list- A circular linked list is different from singly and doubly linked lists with respect to the memory occupied by the pointer in the last node. In a circular linked list, the last node does not contain the NULL pointer. It rather contains the memory address of the first node and hence called a circular linked list.

8. Limitations of Linked List

With positive aspects, comes negative ones as well. Here are some of the drawbacks of C/C++ linked lists:

8.1 No random access to elements

As the name “linked list” itself suggests, every data item in the list is linked to its next data item. Therefore, it is not possible to randomly access any element inside the linked list. Only the sequential access of elements is permitted in a linked list.

Arrays support the easy implementation of binary search to find an element but it is not the case with a linked list. Binary search in a linked list is quite complicated.

It’s time to uncover the concept of Multi-dimensional array in C/C++

8.2 Use of pointers

The implementation of pointers is considered to be relatively difficult, which most programmers struggle with, as it proves to be quite dangerous if left uninitialized. Moreover, pointers occupy extra space in computer memory.

9. Summary

Linked lists in C/C++ are the 2nd most used data structure after arrays. A linked list is a dynamic data structure that came into existence to overcome the limitations of Arrays. The concept of linked lists is a must for every C/C++ programmer. Now, you are acquainted with the concept of a linked list.

We have much more amazing and exciting tutorials for C Programming, that you can’t afford to miss.

Don’t forget to provide us your precious reviews by commenting below.

2 Responses

  1. Roshan singh says:

    Awesome explanation , my all concept are clear . This tutorial has very much simple language which is very easy to understand.
    THANK YOU .

    • DataFlair Team says:

      Thanks for the appreciation. If you liked the tutorial on linked list in C/C++, spread the word on social media with your friends and colleagues.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.