Dynamic Representation Of Single Linked list

In C, a single linked list is typically represented using a struct that contains a pointer to the head node of the list. Each node in the list also contains a data field and a pointer to the next node in the list.

Here's an example of how to define a single linked list in C:

```
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node in the list
struct Node {
    int data;
    struct Node* next;
};

// Define the structure for the list
struct LinkedList {
    struct Node* head;
};

// Create a new node with the given data
struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Add a node to the front of the list
void push(struct LinkedList* list, int data) {
    struct Node* newNode = createNode(data);
    newNode->next = list->head;
    list->head = newNode;
}

// Print the contents of the list
void printList(struct LinkedList* list) {
    struct Node* currNode = list->head;
    while (currNode != NULL) {
        printf("%d ", currNode->data);
        currNode = currNode->next;
    }
    printf("\n");
}

// Main function to test the list
int main() {
    struct LinkedList list;
    list.head = NULL;

    push(&list, 3);
    push(&list, 2);
    push(&list, 1);

    printList(&list);
    return 0;
}
```


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext