A linked list is a data structure in which size can be modified dynamically during the runtime. In the linked list, each element is referred to as a node. Each node in the linked list stores data and a pointer to the next node present in the list. In this article, we will learn how to create a linked list in C.
Example
Input:
Elements for Linked list = {10,20,30,40,50}
Output:
Linked List: 10 -> 20 -> 30 -> 40 -> 50 -> NULL
Create a Linked List in C
Linked List in C can be created by defining a structure that represents the nodes i.e. having a data field and next pointer.
- We can then create the instances of this structure and join them using the next pointers.
- We will keep the pointer to the first element as the head of the linked list.
- We can traverse or refer to the linked list using the pointer to the head.
C Program Create a Linked List
C
// C Program to create a Linked List #include <stdio.h> #include <stdlib.h> // Define the structure of Node struct Node {
int data;
struct Node* next;
}; // Function to Insert Node in the Beginning void insertAtBeginning( struct Node** head, int data)
{ // create a new Node using dynamic memory allocation
struct Node* newNode
= ( struct Node*) malloc ( sizeof ( struct Node));
// store the data in the new Node
newNode->data = data;
// the next pointer of new Node will be on current head
newNode->next = *head;
// the current head will the new Node
*head = newNode;
} // Function to Insert the Node in the end void insertAtEnd( struct Node** head, int data)
{ struct Node* newNode
= ( struct Node*) malloc ( sizeof ( struct Node));
// store the data in the new Node
newNode->data = data;
// Since the node will be last its next will be NULL
newNode->next = NULL;
// in case this is the first node make the newNode as
// the head of the LinkedList
if (*head == NULL) {
*head = newNode;
return ;
}
// Create a pointer to iterate till the last node
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
// make the next of the tail to the new Node
current->next = newNode;
} // Function to Print the Linked List void printList( struct Node* head)
{ struct Node* current = head;
while (current != NULL) {
printf ( "%d -> " , current->data);
current = current->next;
}
printf ( "NULL\n" );
} int main()
{ // Create the Linked List
struct Node* head = NULL;
// pass head by reference so that it can be modified
insertAtBeginning(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
insertAtEnd(&head, 50);
// Print the Linked List
printf ( "Linked List: " );
printList(head);
return 0;
} |
Output
Linked List: 10 -> 20 -> 30 -> 40 -> 50 -> NULL
Time Complexity: O(N) where N is the total number of nodes in the linked list.
Auxiliary Space: O(N)