#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the linked list
struct Node {
int data;
struct Node* next;
};
// Function to add a new element to the linked list
void addElement(struct Node head, int newData) {
// Allocate memory for a new node
struct Node newNode = (struct Node)malloc(sizeof(struct Node));
// Check if memory allocation is successful
if (newNode == NULL) {
printf("Memory allocation failed");
return;
}
// Set the data for the new node
newNode->data = newData;
// Make the new node point to the current head
newNode->next = *head;
// Update the head to the new node
*head = newNode;
}
// Function to print the elements of the linked list
void printList(struct Node* node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
// Main function
int main() {
// Initialize an empty linked list
struct Node* head = NULL;
// Add elements to the linked list
addElement(&head, 3);
addElement(&head, 7);
addElement(&head, 12);
// Print the elements of the linked list
printf("Linked List: ");
printList(head);
return 0;
}