Static Representation of Single Linked List in Data Structure using C

In C, a static representation of a linked list can be achieved using an array. The elements of the array represent the nodes in the linked list, and each element contains the data of the node and a pointer to the next node in the list. Here is an example of how to create and manipulate a statically represented linked list:

```
#include <stdio.h>
#define MAX_SIZE 100

typedef struct Node {
    int data;
    int next;
} Node;

int main() {
    Node arr[MAX_SIZE]; // Initialize an array to represent the linked list
    int head = -1; // Initialize the head of the list to -1 (empty list)
    int numNodes = 0; // Initialize the number of nodes in the list to 0

    // Insert nodes into the list
    arr[0].data = 1;
    arr[0].next = -1;
    head = 0;
    numNodes++;

    arr[1].data = 2;
    arr[1].next = -1;
    arr[0].next = 1;
    numNodes++;

    arr[2].data = 3;
    arr[2].next = -1;
    arr[1].next = 2;
    numNodes++;

    // Print the list
    int node = head;
    while (node != -1) {
    printf("%d ", arr[node].data);
    node = arr[node].next;
    }

    return 0;
}
```

In this example, we define a `Node` struct that contains an integer `data` and an integer `next`. We also define an array `arr` with a maximum size of `MAX_SIZE` elements, and initialize the head of the list to -1 (indicating an empty list) and the number of nodes in the list to 0.

We then insert three nodes into the list by setting the `data` and `next` fields of each element of the array, and updating the `next` field of the previous node to point to the current node. We also update the `head` variable to point to the first element of the array, and increment the `numNodes` variable to reflect the number of nodes in the list.

Finally, we print the list by iterating through the array using the `head` and `next` fields of each element, and outputting the `data` field of each element.

Note that this is a simple example of a statically represented linked list, and there are many other operations that can be performed on a linked list, such as deleting nodes and finding the length of the list.



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