Linked List

Linked List

Table of contents

No heading

No headings in the article.

Just like the name implies a link is a ring or loop in a chain. In this article, we will discuss Linked list in c language.

What is a Linked List?

A linked list can be defined as a chain of data structures, that includes a series of connected nodes. In a linked list if you are searching for a particular data, you start from the head[node] of the chain and search through until you find the data you are looking for. Also, by updating the links between the chain, you can add or remove data from the chain without having to rearrange all other data.

Types of linked lists:

  • Singly-linked list: This is a linked list that has a single link pointing to the next node in the list. where the last node in the list has a null link, showing the end of the list.

  • Doubly-linked list: This consists of nodes that have two links. where one is pointing to the next node in the list while the other is pointing to the previous node in the list.

  • Circular-linked list: This is similar to the single-linked list and doubly-linked list, the difference is that the last node is pointing back to the first node, which creates a loop.

  • doubly circular linked list: This joins the features of a doubly linked list and a circular linked list.

Example of linked list in c:

let's take a look at how each node is represented. each node is made up of:

  • Data that stores the actual data of the node.

  • A pointer that stores the address of the next node.

#include <stdio.h>
#include <stdlib.h>

//Define a structure
struct node {
int data;
struct node* next;
};

//function to insert node at the beginning of the list
void creatNode(struct node** head, int data)
{
//Allocating a memory for the new node using malloc
struct node *newi = (struct node*)malloc(sizeof(struct node));
//set data of new node
newi->data = data;
// set pointer of new node to point to head of the list
newi->next = *head;
//set head of the list to point to new node
*head = newi;
}
//Function to print the content of the list
struct node *printList(struct node *head)
{
    struct node* current = head;
    while(current != NULL)
    {
        printf("%d, ", current->data);
        current = current->next;
    }
    return (0);
}

int main() 
{
    struct node* head = NULL;

//inserting nodes at the beginning of the list
    creatNode(&head, 4);
    creatNode(&head, 5);
    creatNode(&head,8);
    creatNode(&head,9);
    creatNode(&head,10);
   printList(head);
    return (0);
}

Output:

10, 9, 8, 5, 4,

Purpose of linked list in c:

  • Dynamic size: linked list allows you to add and remove elements at any time without the need to allocate or deallocate contiguous memory.

  • Insertion and Deletion: you can easily insert or delete an element from a linked list which can be done in constant time, whereas in an array, shifting all the elements may be required.

  • Memory efficiency: linked list has more memory efficient than arrays, especially when the list or element has varying sizes or is sparsely populated.

  • Versatility: in a linked list variety of data structures can be implemented, including stacks, queues, and hash tables.

Conclusion

Linked list is chain of data structure that is commonly used in c language and other programming languages to store and manipulate data. it has serveral advantages over other data structures, such as arrays. linked list includes dynamic size, easy insertion and deletion, memory effciency, and versatility.

Linked list also has disadvantage which includes, increased overhead due to the use of pointers and slow random access time compared to array.

Overall Linked list is a powerful tool use in managing dynamic data structures in C and other programming language, which is a concept that any programmer working with data structures and algorithms needs to understand.