Creating a linked list in C involves understanding the basic concepts of pointers and dynamic memory allocation. A linked list is a collection of nodes where each node contains a data part and a pointer to the next node in the sequence. Here’s how you can create a simple linked list in C.
Creating a Simple Linked List in C
1. Define the Node Structure
First, we need to define the structure of a node in the linked list. Each node will contain an integer data field and a pointer to the next node.
#include <stdio.h>
#include <stdlib.h>
// Define the node structure
struct Node {
int data;
struct Node* next;
};
2. Function to Create a New Node
Next, we need a function to create a new node. This function will allocate memory for a new node, set its data, and initialize its next pointer to NULL
.
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
3. Insert a Node at the Beginning
We can add a function to insert a new node at the beginning of the linked list. This function will update the head of the list to point to the new node.
// Function to insert a node at the beginning of the linked list
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}
4. Traverse the Linked List
To print the elements of the linked list, we need a function to traverse it.
// Function to traverse and print the linked list
void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
5. Main Function to Demonstrate Linked List Operations
Finally, we can write the main function to demonstrate the creation and traversal of the linked list.
int main() {
struct Node* head = NULL; // Initialize the head of the list
// Insert elements at the beginning
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);
// Traverse and print the linked list
traverseList(head);
return 0;
}
Complete Code
Here’s the complete code for creating and traversing a simple linked list in C:
#include <stdio.h>
#include <stdlib.h>
// Define the node structure
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the beginning of the linked list
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}
// Function to traverse and print the linked list
void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL; // Initialize the head of the list
// Insert elements at the beginning
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);
// Traverse and print the linked list
traverseList(head);
return 0;
}
Explanation
- Node Structure: Defines a
Node
with an integerdata
and a pointer to the next node. - Create Node: Allocates memory for a new node, initializes its data and next pointer.
- Insert at Beginning: Adds a new node at the beginning of the list and updates the head.
- Traverse List: Prints each node’s data, following the
next
pointers until reaching the end. - Main Function: Demonstrates the creation of a linked list by inserting nodes and then traversing and printing the list.
By understanding and practicing this code, you’ll gain a good foundation in linked list operations in C. Happy coding!