C Code Implementation:
A linked list in C can be created using either a class or a struct, defining the attributes and functions of the linked list. This class or struct will include a pointer pointing to the initial element of the list, referred to as the head, along with a variable to keep track of the total number of elements in the list. Below is a detailed illustration of a comprehensive linked list implementation in C:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct LinkedList {
struct Node* head;
int size;
};
void init(struct LinkedList* list) {
list->head = NULL;
list->size = 0;
}
void insert(struct LinkedList* list, int value) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = list->head;
list->head = newNode;
list->size++;
}
void deleteNode(struct LinkedList* list, int pos) {
if (pos < 0 || pos >= list->size) {
printf("Invalid position!\n");
return;
}
if (pos == 0) {
struct Node* temp = list->head;
list->head = list->head->next;
free(temp);
} else {
struct Node* current = list->head;
for (int i = 0; i < pos-1; i++) {
current = current->next;
}
struct Node* temp = current->next;
current->next = temp->next;
free(temp);
}
list->size--;
}
void printList(struct LinkedList* list) {
struct Node* current = list->head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int getSize(struct LinkedList* list) {
return list->size;
}
int main() {
struct LinkedList list;
init(&list);
insert(&list, 5);
insert(&list, 10);
insert(&list, 15);
printList(&list); // Output: 15 10 5
deleteNode(&list, 1);
printList(&list); // Output: 15 5
printf("Number of elements: %d\n", getSize(&list)); // Output: 2
return 0;
}
Output
15 10 5
15 5
Number of elements: 2
Add Node to Linked List in C
Here is an illustration of a basic code snippet in C showcasing the process of appending a new node to a linked list along with a main function:
C Code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct LinkedList {
struct Node* head;
};
void addNode(struct LinkedList* list, int value) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (list->head == NULL) {
list->head = newNode;
return;
}
struct Node* current = list->head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
int main() {
struct LinkedList list;
list.head = NULL;
addNode(&list, 5);
addNode(&list, 10);
addNode(&list, 15);
struct Node* current = list.head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
return 0;
}
Output
5 10 15
Explanation:
In this code snippet, the addNode function accepts a linked list pointer and an integer value as parameters. The integer value is then assigned to the data field of a newly created node. Initially, the next field of the node is set to NULL after the value assignment.
Linked lists excel in managing dynamic data structures and performing insertions and deletions efficiently. On the other hand, arrays shine in enabling fast random access and exhibiting superior cache locality. Nonetheless, the choice between the two data structures is contingent on the specific use case and implementation nuances.
Next, it verifies whether the beginning of the list is NULL. If this condition is met, the new node is assigned to the head. Otherwise, it proceeds to traverse the list in order to locate the final node and append the new node at the list's end. Within the main function, a LinkedList object is instantiated, the head is set to NULL, the addNode function is invoked multiple times to insert various values, and finally, the list is traversed to display all the values.