×

What is Linked List in C

Linked list

Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory is used for data, and second memory is used to store the addresses of subsequent components.

Therefore, each struct node in a linked list has two memory fields: a data field and an address field.

The address field contains the address of the next element. The address section is also known as Pointer to the Next Node.

After array, we use linked list as our second priority for the data structure.

The linked list's size is not defined, and data items may be inserted along the list.

Linked list Vs. Array:

The fundamental distinction between an array and a linked list is the amount of contiguous memory that an array takes up as opposed to the dispersed memory of a linked list.

Random access is made feasible by the array's contiguous memory. Any element can be directly accessed using its index. The below table explains more about the differences between Linked List and Array:

S.no.Linked ListArray
1A linked list is a linear data structure which consist of a series of nodes. We can store data and also address of next node in every node.     An array is also a linear type of data structure which consists of similar data types elements in contiguous memory location.    
2Memory is dispersed in Linked List. In physical memory, the nodes are organized in random order.  An array takes up contiguous memory. This indicates that the data is kept in physical memory's subsequent memory locations.  
3Access to the sequence is supported by linked lists. It implies that we must read nodes progressively starting at the beginning of the list to access any random node. The reason for this is that physical memory nodes are scattered at random. Nodes, unlike arrays, do not have a known address.  Random access is possible with arrays. The array's elements can be accessed directly by their indexes. Since the data is kept in a single, continuous memory space, we can quickly access any element of an array using its index.  
4Each node in a linked list has two memory fields: one for data and one for the address of the next node. This means that Linked List takes up more memory than an array.  The array merely stores the data at each index. Nothing additional is kept in storage.
5It's easy to insert or delete. Nodes aren't moved around or rearranged as a result of it.  Rearranging all of the array's members may be necessary to insert or remove data from it.  
6Memory is efficiently used by Linked List. Only when data is added does it take up memory.  Even without any data being inserted, an array uses memory.  
7The memory for the linked list is allocated at runtime.An array may have memory allocated for it both during compilation and during runtime.  
8Linked List is dynamic. Its size is freely expandable and contractible.  The array that has allocated memory at compile time is a static array that has been declared, and its size is fixed.  
9On a linked list, we cannot conduct a binary search.  On the array, we can conduct a binary search.    

Types of linked lists:

  1. Singly Linked List
  2. Doubly Linked List

Singly Linked List:

The most prevalent kind of linked list among all other linked lists is the singly linked list. We can only navigate in one direction in a singly linked list. It is an assortment of sets of elements in an orderly fashion. In single linked list, each node in a linked list has two memory fields: one for data and one for the address of the next node.

A singly linked list can be created in essentially two different methods, which are: 

  • We can add any future new entries to the linked list's end.
  • The alternative is to insert items into particular locations in the list in ascending order of sorted items.

Each item in a linked list must have both the data and an address of the next item in the list. The items contained in a linked list typically consist of structures.

What is Linked List in C

Syntax:

struct node
{
Int data;
struct node * after;
};

In the above syntax, struct nodes are used to hold data of the integer type and struct nodes *, which hold the address of the next node.

In singly linked lists, there is a single node connected just next to another node, so in the above code, we have used a struct to describe a node. The node will contain its integer value and the pointer of the same node type to the next.

Example: Let's create a singly linked list of node size 4 where the node order would be 4->1->2->3.

C code:

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


struct node{
    int data;
    struct node* after_node;
};


int main(){
    struct node* one=NULL;
    struct node* two=NULL;
    struct node* three=NULL;
    struct node* four=NULL;
    
    one=(struct node*)malloc(sizeof(struct node));
    two=(struct node*)malloc(sizeof(struct node));
    three=(struct node*)malloc(sizeof(struct node));
    four=(struct node*)malloc(sizeof(struct node));
    
    one->data=4;
    one->after_node=two;
    
    two->data=1;
    two->after_node=three;
    
    three->data=2;
    three->after_node=four;
    
    four->data=3;
    four->after_node=NULL;
    
    struct node* temp = one;
    while(temp!=NULL){
        printf("%d ",temp->data);
        temp=temp->after_node;
    }
}

Program Output:

What is Linked List in C

Explanation:

In the above c code, we created four variables of type struct and initialized them as NULL. Now in the first node, we put the value, and we initialize its next node as a pointer that points to the next node. Now in the second node, we put the value again, and the next pointer would point to the third node. Now the third node will contain a value, and it will point to the fourth node as its next pointer since the fourth pointer is the last pointer so that we will put the value into it, and its next pointer will point to NULL.

Time complexity:

Search: O(N)

insertion: O(1)

deletion: O(1)

Doubly Linked list:

A data portion and two pointers—one for the previous node and the other for the next node—are contained in each node in a doubly-linked list.

In a doubly linked list, there are two possible directions of movement: forward and backward.

Traversal is possible in both directions using a doubly linked list. The next node's and the previous node's addresses are available. The choice to turn right or left will be available at any node.

Data, a pointer to the subsequent node, and a pointer to the preceding node make up a node.

With NULL as the previous node refers that the head node is empty. See the below image:

What is Linked List in C

Example: We will be having an example of a doubly linked list:

NULL <-> 4 <-> 6 <-> 9 <-> 5 <-> 3 <-> NULL

C code:

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


struct node{
    int data;
    struct node* next;
    struct node* prev;
};


void addFront( struct node** headRef,int data){
    struct node* newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    
    newNode->next=(*headRef);
    newNode->prev=NULL;
    
    if((*headRef)!=NULL)
    (*headRef)->prev=newNode;
    
    (*headRef) = newNode;
}


void addAfter(struct node* currNode,int data){
    if(currNode==NULL){
        printf("can't be null node");
        return;
    }
    struct node* newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    
    newNode->next=currNode->next;
    currNode->next=newNode;
    newNode->prev=currNode;
    if(newNode->next!=NULL){
        newNode->next->prev=newNode;
    }
}


void addEnd(struct node** headRef,int data){
    struct node* newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    
    newNode->next=NULL;
    
    struct node* temp=(*headRef);
    if((*headRef)==NULL){
        newNode->prev=NULL;
        (*headRef)=newNode;
        return;
    }
    
    while(temp->next!=NULL){
        temp=temp->next;
    }
    temp->next=newNode;
    newNode->prev=temp;
    
}


int main(){
    struct node* head=NULL;
    
    addFront(&head,9);
    addFront(&head,6);
    addFront(&head,4);
    addEnd(&head,3);
    addAfter(head->next->next,5);
    
    struct node* temp = head;
    struct node* last=NULL;
    printf(" left to right traversal is :");
    while(temp!=NULL){
        last=temp;
        printf("%d ",temp->data);
        temp=temp->next;
    }
    
     printf(" \nright to left traversal is :");
    while(last!=NULL){
        printf("%d ",last->data);
        last=last->prev;
    }
}

Program output:

What is Linked List in C

Explanation:

In the above code, we have a node variable of type struct where three members are present. One is the node's data, and the other two are pointers to the node before it and the node after it.

We implemented three functions that add the new node at different positions.

One function will add the new node at the start of the list. We will be given the pointer to the head and the integer data. We will create the node with data value and add it to the previous head node, making this new node the head node.

The second function will add the node after any given node. We will receive an integer value and a reference to the target node. We will create the node with its values, and then we will traverse it to the target node. The target node's next node will be the new node, and vice versa. We will add the new node as the target node's next node. Also, we will set the previous nodes also.

The final node will be added to the linked list in the third function. The list will be repeated several times, and when it is finished, the previous node of the new node will serve as the last node, with the next node of the last node added as a new node.


Related Topics

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.