Linked List in C Language With Examples

Linked List in C Language With Examples

Edited By Nikhil Verma | Updated on Jul 03, 2024 12:35 PM IST | #Neuro Linguistic Programming

Linked lists are linear data structures used to store and organise data in memory. It is an important concept of data structures and helps to utilise memory efficiently by dynamically allocating nodes when needed. C language linked list is a collection of nodes, where each node contains a data element and a reference (called pointer) to the next node in the sequence.

Unlike arrays, which are less flexible and take more time for insertion and deletion operations; linked list programs in C are fast and offer flexibility in size and memory utilisation. If you are interested in gaining further knowledge about this field, we suggest you check Online C Certification Courses.

The basic building block of a linked list in C is a node, and a node 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

It is important to understand the basic structure of a linked list node before diving deep into its implementation. The linked list in C node consists of two primary components: Data Field and Pointer (Reference).

Data Field

The data field of a linked list node holds the actual information 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, the data field would be of type char for a linked list of strings.

Pointer (Reference)

The pointer field in the node structure is important 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 the pointer that allows the creation of a chain of nodes where each node knows the location of the next one.

Syntax to Demonstrate Data Field and Pointer in C Linked List

struct Node {

// Data field where value needs to be stored

int data;

// next is the pointer to the next node

struct Node* next;

};

Note: This linkage of linked lists is what gives the C program its 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

There are many types of linked lists that includes singly, doubly, circular, doubly circular, and header linked lists. But, here, we will discuss in detail only two popular linked lists- singly linked list and doubly linked list.

Singly Linked List

The singly linked list is the most simple form of a linked list. Each node of a linked list contains data and a pointer to the next node that forms a unidirectional sequence. The last node point of a linked list is NULL which marks the end of the list. The following example shows the singly linked list code in C:

// Syntax of a singly linked list node

struct Node {

int data;

struct Node* next;

};

Data

Doubly Linked List

A doubly linked list in C is a type of linked list where each node not only points to the next node but also points to the previous node. This bidirectional connection allows for easier traversal both forward and backward through the list. This feature is particularly useful for operations like reverse traversal or efficient insertion and deletion at both ends of the list.

// 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 computer programmer who aims to master dynamic data structures. A singly linked list is a versatile and memory-efficient data structure. It is important to understand its implementation, as it provides insights into fundamental programming concepts such as pointers and memory management.

Below, we have covered fundamental operations such as creating a node, inserting a node at the beginning, and traversing the singly linked list in C.

Creating a Node

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

Code:

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, first, we create a new node and update the next pointer to point to the current head of the list.

Code:

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 means moving through each node in the list, one by one, starting from the first node (head) and continuing until the last node (NULL). Linked list in C, where nodes are connected through pointers; traversing allows programmers to explore and interact with the elements sequentially.

Unlike arrays, linked lists do not provide direct access to elements based on indices. A systematic traversal from the beginning to the end or vice versa is important to access the data elements in the linked list in C.

Traversing is fundamental for performing operations like printing all elements in the list, searching for a specific value, counting nodes, or performing any operation that requires accessing every node sequentially.

Code:

// 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 important 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.

Output

If we define a structure named `Node` in this C program and add a `main()` function with some values, the output will be:

1719984485694

Related: Popular Programming Courses From Top Providers

Conclusion

A linked list offers a dynamic and efficient way to manage data. Whether it is a singly linked list for one-directional traversal or a doubly linked list for bidirectional traversal, the principle remains the same as the above-provided C linked list example.

Frequently Asked Questions (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

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