Linked List in C



Structure and Linked List with C Programming in Tamil


Linked List in C

  • A linked list is formed when many such nodes are linked together to form a chain.
  • Each node points to the next node present in the order.
  • The first node is always used as a reference to traverse the list and is called HEAD. The last node points to NULL.
 Linked List

Linked List

Node

  • A node is a collection of two sub-elements or parts. A data part that stores the element and a next part that stores the link to the next node.
 Node

Node

Declaring a Linked List

Example

struct LinkedList 
{ 
	int data; 
	struct LinkedList *next; 
};

Read Also

Linked List
  • The above definition is used to create every node in the list.
  • The data field stores the element and the next is a pointer to store the address of the next node.
  • In place of a data type, struct LinkedList is written before next.
  • That's because its a self-referencing pointer. It means a pointer that points to whatever it is a part of.
  • Here next is a part of a node and it will point to the next node.
 Linked List

Declaring Linked List

Creating a Node

  • typedef is used to define a data type in C.
  • malloc() is used to dynamically allocate a single block of memory in C, it is available in the header file stdlib.h.
  • sizeof() is used to determine size in bytes of an element in C. Here it is used to determine size of each node and sent as a parameter to malloc.
  • The below code will create a node with data as value and next pointing to NULL.
 Linked List

Creating a node

Example

typedef struct LinkedList *node;    //Define node as pointer of data type struct LinkedList 
node createNode()
{ 
	node temp;                         // declare a node 
	temp = (node)malloc(sizeof(struct LinkedList)); //allocate memory using                         								malloc() 
	temp->next = NULL;          // make next point to NULL 
	return temp;                    //return the new node 
}

Add a Node to the Linked List

  • Here the new node will always be added after the last node. This is known as inserting a node at the rear end.
  • The last node is checked by the condition :  p->next = NULL;
  • Here -> is used to access next sub element of node p. NULL denotes no node exists after the current node, i.e. its the end of the list.

Example

node addNode(node head, int value)
{ 
  node temp,p;              // declare two nodes temp and p 
  temp = createNode(); //createNode will return a new node with data = value and next  pointing to NULL. 
  temp->data = value;   // add element's value to data part of node 
  if(head == NULL)
  { 
        head = temp;       //when linked list is empty 
   }
}
else
{  
    p = head;             //assign head to p 
    while(p->next != NULL)
    {  
        p = p->next;    //traverse the list until p is the last node. The last node always points to NULL. 
    }  
    p->next = temp; //Point the previous last node to the new node created. 
} 
    return head; 
}

Traversing a Linked List

  • The linked list can be traversed in a while loop by using the head node as a starting reference: node p;
 p = head; 
while(p != NULL) 
{  
    p = p->next; 
}
 Linked List Code

Learn C - C tutorial - Linked List Code - C examples - C programs

Sample Code

#include <stdio.h>
#include <stdlib.h>
struct LinkedList 
{
    int data;                           //Data of the node
    struct LinkedList *next;   //Address of the next node
} *p;
void createNodeList(int n); // function to create the list
void displayList();              // function to display the list
int main()
{
    int n;
    printf("\n\n Linked List : To create and display Singly Linked List :\n");
    printf("-------------------------------------------------------------\n");	
    printf(" Input the number of nodes : ");
    scanf("%d", &n);
    createNodeList(n);
    printf("\n Data entered in the list : \n");
    displayList();
    return 0;
}
void createNodeList(int n)
{
    struct LinkedList *fnNode, *temp;
    int data, i;
    p = (struct LinkedList *)malloc(sizeof(struct LinkedList));
    if(p == NULL)    //check whether the fnnode is NULL and if so 		no memory allocation
    {
        printf("Memory can not be allocated.");
    }
    else
    {
        printf("Input data for node 1 : ");
        scanf("%d", &data);
        p->data = data;      
        p->next = NULL; // links the address field to NULL
        temp = p;
        for(i=2; i<=n; i++)
        {
            fnNode = (struct LinkedList *)malloc(sizeof(struct LinkedList));
            if(fnNode == NULL)
            {
                printf(" Memory can not be allocated.");
                break;
            }            
            else
            {
                printf("Input data for node %d : ", i);
                scanf("%d", &data);
                fnNode->data = data; fnNode->next = NULL; 
                temp->next = fnNode; 
                temp = temp->next; 
            }
        }
    }
}

Output:

Linked List : To create and display Singly Linked List :
---------------------------------------------------------------
Input the number of nodes : 3
Input data for node 1 : 10
Input data for node 2 : 20
Input data for node 3 : 30

Data entered in the list :
10---> 20---> 30--->


View More Quick Examples


Related Searches to Linked List C