C Code for Linked List: A Comprehensive Guide
Understanding and implementing a linked list in C can be a rewarding experience for any programmer. A linked list is a linear data structure where each element is a separate object called a node. Each node contains a data field and a reference (or link) to the next node in the sequence. This guide will walk you through the process of creating a linked list in C, covering the basics, operations, and some advanced topics.
Understanding the Node Structure
A node in a linked list typically consists of two parts: the data part and the link part. The data part holds the actual data, while the link part holds the address of the next node in the list. Here’s a simple structure for a node in a singly linked list:
struct Node { int data; struct Node next;};
This structure defines a node with an integer data type and a pointer to the next node. In a doubly linked list, each node would also have a pointer to the previous node, making it bidirectional.
Creating a Linked List
Creating a linked list involves initializing the head pointer to NULL and then adding nodes to the list. Here’s a basic 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;}
Once you have a function to create nodes, you can add them to the list. Here’s a simple function to add a node at the end of the list:
void appendNode(struct Node head, int data) { struct Node newNode = createNode(data); if (head == NULL) { head = newNode; return; } struct Node current = head; while (current->next != NULL) { current = current->next; } current->next = newNode;}
Traversing a Linked List
Traversing a linked list means going through each node in the list to perform some operation. Here’s a simple function to print the data of each node in the list:
void traverseList(struct Node head) { struct Node current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("");}
Deleting a Node
Deleting a node from a linked list involves finding the node and then adjusting the pointers to exclude it from the list. Here’s a function to delete a node with a given key:
void deleteNode(struct Node head, int key) { struct Node temp = head, prev = NULL; if (temp != NULL && temp->data == key) { head = temp->next; free(temp); return; } while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if (temp == NULL) return; prev->next = temp->next; free(temp);}
Sorting a Linked List
Sorting a linked list can be done using various algorithms, such as bubble sort, insertion sort, or merge sort. Here’s an example of how to sort a linked list using bubble sort:
void bubbleSort(struct Node head) { int swapped; struct Node ptr1; struct Node lptr = NULL; if (head == NULL) return; do { swapped = 0; ptr1 = head; while (ptr1->next != lptr) { if (ptr1->data > ptr1->next->data) { int temp = ptr1->data; ptr1->data = ptr1->next->data; ptr1->next->data = temp; swapped = 1; } ptr1 = ptr1->next; } lptr = ptr1; } while (swapped);}
Table of Operations
Here’s a table summarizing the operations we’ve discussed:
Operation | Description |
---|---|
|