Here's an example of a singly linked list program in C:
#include
#include
// Define the structure of a singly linked list node
struct Node {
int data;
struct Node* next;
};
// Function to print all elements of the linked list
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
// Create three nodes
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
// Allocate memory for each node
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
// Assign data values and pointers for each node
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
// Print the linked list
printList(head);
return 0;
}
This program creates a linked list with three nodes, assigns data values and pointers for each node, and then prints the linked list. You can modify this program to add, delete or modify nodes as per your requirements.