Careers360 Logo
Linked List in C Language With Examples

Linked List in C Language With Examples

Edited By Team Careers360 | Updated on Feb 13, 2024 12:45 PM IST | #Neuro Linguistic Programming

In data structures, linked list in C stand out as a dynamic and versatile option for organising data efficiently. C language linked list is a collection of nodes, where each node contains a data element and a reference to the next node in the sequence. Moreover, the dynamic nature of linked lists facilitates efficient memory utilisation by dynamically allocating nodes as needed.

Unlike arrays, linked list program in C offer flexibility in size and memory utilisation, making them an essential tool in various applications. Consider learning these Online C Certification Courses if you are interested in gaining further knowledge about this field.

What Is Linked List In C?

A linked list algorithm in C is a dynamic data structure that consists of a sequence of elements, known as nodes, where each node contains a data element and a reference (or pointer) to the next node in the sequence. Unlike arrays, which are static and have a fixed size, linked list in C can dynamically grow or shrink during program execution.

The basic building block of a linked list in C is a node, and a node typically contains two fields: a data field to store the actual information and a reference or pointer field to link it to the next node in the sequence. The last node in the list usually points to a null value, indicating the end of the list.

The Anatomy of a Linked List Node in C

Understanding the basic structure of a linked list node is crucial before delving into implementation. Linked list in C node typically consists of two primary components:

  • Data Field

The data field of a linked list node holds the actual information or payload that the node is intended to store. This can be any data type, depending on the requirements of the specific linked list. For example, if the linked list is meant to store integers, the data field would be of type int. Similarly, for a linked list of strings, the data field would be of type char*.

struct Node {

// Data field where value needs to be stored

int data;

// next is the pointer to the subsequent node

struct Node* next;

};

  • Pointer (Reference) to the Next Node

The pointer field in the node structure is crucial for maintaining the linkage between nodes in the linked list. This pointer, often named next or link, points to the memory location of the next node in the sequence. It is of type "pointer to the same node structure," allowing the creation of a chain of nodes where each node knows the location of the next one.

struct Node {

int data;

// Pointer to the next node

struct Node* next;

};

This linkage is what gives C program for linked lists their dynamic nature, as nodes can be easily added or removed by updating these pointers.

Also read:

Exploring Two Popular Types of Linked Lists in C

Exploring these popular types of linked lists in C sheds light on their structures and operations, understanding on how each caters to specific programming needs. From the unidirectional connections of singly linked lists to the bidirectional relationships of doubly linked lists, these equip computer programmers with the knowledge to make informed decisions in designing and implementing efficient data structures tailored to their applications.

1. Singly Linked List

The singly linked list is the most straightforward form of a linked list. Each node contains data and a pointer to the next node, forming a unidirectional sequence. The last node points to NULL, signifying the end of the list. The following example shows the singly linked list code in C:

// Example of a singly linked list node

struct Node {

int data;

struct Node* next;

};

2. Doubly Linked List

Taking a step further, the doubly linked list in C introduces bidirectional traversal by including a pointer to the previous node in each node. This bidirectional connectivity facilitates operations like reverse traversal.

// Example of a doubly linked list node

struct Node {

int data;

struct Node* next;

struct Node* prev; // Pointer to the previous node

};

Also read:

Implementing a Singly Linked List in C: A Step-by-Step Guide

Implementing a singly linked list in C is a foundational skill for any programmer aiming to master dynamic data structures. A singly linked list is a versatile and memory-efficient data structure, and understanding its implementation provides insights into fundamental programming concepts such as pointers and memory management.

Let us dive into the implementation of a singly linked list in C, covering fundamental operations such as creating a node, inserting a node at the beginning, and traversing the list.

Creating a Node

To create a new node, we define a function that allocates memory for the node and initialises its data and next pointer.

struct Node* createNode(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

return newNode;

}

Inserting a Node at the Beginning

To insert a node at the beginning of the list, we create a new node and update the next pointer to point to the current head of the list.

struct Node* insertAtBeginning(struct Node* head, int value) {

struct Node* newNode = createNode(value);

newNode->next = head;

return newNode;

}

Traversing the Linked List

Traversing a linked list is a fundamental operation that involves systematically navigating through individual nodes of the list to access or manipulate their data. Linked list in C, where nodes are connected through pointers, traversing allows programmers to explore and interact with the elements in a sequential manner. Unlike arrays, linked lists do not provide direct access to elements based on indices, necessitating a systematic traversal from the beginning to the end or vice versa.

This process involves following the pointers from one node to the next until the desired node is reached. Traversing the linked list involves moving through each node in the sequence and performing actions, such as printing the data.

// The head is the starting point of the linked list.
void traverseList(struct Node* head) {

// Declare a pointer 'current' and initialise it with the head of the linked list.
struct Node* current = head;

while (current != NULL)
// Move the 'current' pointer to the next node in the linked list.

// This step is crucial for iterating through the entire list.
{

printf("%d -> ", current->data);

current = current->next;

}

printf("NULL\n");

}

// Print "NULL" to indicate the end of the linked list after the while loop.

Related: Popular Programming Courses From Top Providers

Conclusion

C program to create a linked list offers a dynamic and efficient way to manage data, and understanding their implementation is crucial for any programmer. Whether it is a singly linked list for simplicity or a doubly linked list for bidirectional traversal, the principles remain the same. With the provided C linked list example and explanations, you should now have a solid foundation for working with linked lists in C, empowering you to handle dynamic data structures effectively.

Frequently Asked Question (FAQs)

1. What is a linked list in C?

A linked list in C is a data structure consisting of nodes, where each node contains data and a reference (or link) to the next node in the sequence. This structure allows for dynamic memory allocation and efficient data manipulation.

2. What are the two popular types of linked lists in C?

The two primary types are Singly Linked List in which each node contains data and a pointer to the next node in the sequence and Doubly Linked List in which Nodes have data, a pointer to the next node, and a pointer to the previous node, enabling bidirectional traversal.

3. How do I create a node in a linked list?

To create a node in a linked list, you allocate memory for the node, set its data, and initialise the next (and prev for doubly linked lists) pointer. This creates a new element ready to be added to the linked list.

4. How do I traverse and print a linked list in C?

To traverse a linked list in C, iterate through each node, and perform actions like printing data. This involves moving from one node to the next (or previous for doubly linked lists) until reaching the end of the list.

5. How can I insert a node at the beginning of a linked list?

Inserting a node at the beginning involves creating a new node, setting its next (and prev for doubly linked lists) pointer to the current head, and updating the head to the new node.

Articles

Upcoming Exams

Application Date:20 October,2023 - 14 May,2024

Application Date:06 December,2023 - 20 May,2024

Application Date:06 February,2024 - 15 May,2024

Application Date:14 February,2024 - 15 May,2024

Have a question related to Neuro Linguistic Programming ?
Udemy 12 courses offered
Vskills 2 courses offered
Back to top