Linked List in Data Structures

Why Linked List?

Problems with an array
Array is Fixed-size
for example a[10] toh 10 se jyada store nahi ho skta aur agar 2 elements hi hai toh baaki sab jagh waste ho rahi hai
int a[5]; -> array in stack
int *p = new int[5]; -> array in heap
Benefit: arrays are contiguous toh index se access kr skte hai. It’s just like a bench.
Linked List: Jisko bethna ho vo apni chair leke aayga aur jaate vakt leke jayga.

Structure of Linked List

node: data | next

struct Node
{
    int data;
    struct Node *next;
};
How to create a node?
struct Node *P;
P = (struct Node*)malloc(sizeof(struct Node)) //in C
P = new Node; //in C++
How to access members of a Node?
P->data = 10;
P->next = 0; //null
 
What’s next?

Leave a Comment