Difference Between Linked List and Array in C

1. Introduction

In C programming, arrays and linked lists are two different ways of storing collections of data. An array is a fixed-size collection of elements of the same data type stored in contiguous memory locations. A linked list, on the other hand, is a collection of nodes, where each node contains data and a pointer to the next node in the sequence.

2. Key Points

1. Arrays have fixed sizes determined at compile time.

2. Linked lists are dynamic in size and can grow or shrink at runtime.

3. Elements in an array are stored in contiguous memory locations.

4. Linked list elements (nodes) can be stored anywhere in the memory, linked together by pointers.

3. Differences

Array Linked List
Fixed size. Dynamic size.
Efficient in terms of memory when the size is known. Requires extra memory for storing pointers.
Fast access to elements by index. Sequential access required to reach an element.
Insertion and deletion are expensive operations. Easier insertion and deletion of elements.

4. Example

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

// Array example
void arrayExample() {
    int arr[3] = {1, 2, 3};
    for (int i = 0; i < 3; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

// Linked list example
typedef struct Node {
    int data;
    struct Node *next;
} Node;

void linkedListExample() {
    Node *head = malloc(sizeof(Node));
    head->data = 1;
    Node *second = malloc(sizeof(Node));
    second->data = 2;
    head->next = second;
    second->next = NULL;

    Node *current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    free(second);
    free(head);
}

int main() {
    arrayExample();
    linkedListExample();
    return 0;
}

Output:

1 2 3
1 2

Explanation:

1. In the array example, the elements are stored in contiguous memory locations and accessed by index.

2. In the linked list example, nodes are dynamically allocated in memory, and each node points to the next, forming a chain.

5. When to use?

- Use arrays when the size of the collection is known and fixed, and you need fast access to elements.

- Use linked lists for dynamic size collections where frequent insertions and deletions are required.

Difference between malloc() and calloc()?

Difference between Local Variable and Global Variable in C

Difference between Global and Static Variables in C

Difference Between Call by Value and Call by Reference in C

Difference Between getch() and getche() in C

Difference between printf() and sprintf() in C

Difference between Arrays and Pointers in C

Difference between Structure and Union in C

Difference Between Stack and Heap Memory Allocation in C

Difference Between Macro and Function in C

Difference between = and == in C

Difference Between for loop and while loop in C

Difference Between Linked List and Array in C

Difference between fgets() and gets() in C

Difference between ++i and i++ in C

Difference between struct and typedef struct in C

Difference between int main() and void main() in C

Difference between Character Array and String in C

Difference between break and continue in C

Difference between exit() and return in C

Comments