🔴 Advanced  ·  Lesson 38

Linked List

Linked List

Linked List

A linked list is a dynamic data structure made of nodes. Each node stores data and the address of the next node. It can grow or shrink at run time.

Array vs Linked List

Array uses contiguous memory. Linked list uses nodes connected by pointers.

Node Structure

C Language
struct Node {
    int data;
    struct Node *next;
};

Insert Node

C Language
#include <stdio.h>
#include <stdlib.h>

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

struct Node* insertEnd(struct Node *head, int value) {
    struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;

    if(head == NULL)
        return newNode;

    struct Node *temp = head;
    while(temp->next != NULL)
        temp = temp->next;

    temp->next = newNode;
    return head;
}

Display List

C Language
void display(struct Node *head) {
    struct Node *temp = head;
    while(temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL");
}

int main() {
    struct Node *head = NULL;
    head = insertEnd(head, 10);
    head = insertEnd(head, 20);
    head = insertEnd(head, 30);
    display(head);
    return 0;
}
10 -> 20 -> 30 -> NULL

Summary

  • Linked list is made of nodes
  • Each node has data and next pointer
  • Memory is allocated dynamically
  • Insertion and deletion are easier than array in many cases
  • Traversal starts from head and continues until NULL

Linked List

Linked list nodes से बना dynamic data structure है। हर node में data और next node का address होता है।

Node Structure

C Language
struct Node {
    int data;
    struct Node *next;
};

Node Insert

C Language
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

Display

C Language
while(temp != NULL) {
    printf("%d -> ", temp->data);
    temp = temp->next;
}
printf("NULL");

सारांश

  • Linked list nodes से बनती है
  • हर node data और next pointer रखती है
  • Memory dynamically allocate होती है
  • Head first node को point करता है
  • Traversal NULL तक चलता है
← Back to C Tutorial