π Singly & Doubly Linked Lists Visualizer
Interactive C dynamic memory allocation simulator. Perform 10+ node operations on Heap RAM.
Select an operation above and click βΆοΈ Animated to start step-by-step memory visualization.
Ready
No active variables
GCC / C99
π§ Dynamic Memory Allocation: malloc() and free()
Unlike contiguous arrays in Stack memory, Linked Lists use C standard library functions malloc() to reserve node memory blocks dynamically in Heap RAM and free() to release them back to the operating system.
Singly Node Structure
struct Node {
int data; // 4 bytes
struct Node* next; // 8 bytes (64-bit pointer)
}; // Total = 16 bytes
Doubly Node Structure
struct Node {
int data; // 4 bytes
struct Node* prev; // 8 bytes
struct Node* next; // 8 bytes
}; // Total = 24 bytes
π» Essential C Programs & Algorithms Suite
Compilable, production-ready C implementations for standard university exams and technical coding interviews.
π 1. Singly Linked List Standard Operations & Interview Programs
A. Complete Singly Linked List Operations (Insert, Delete, Reverse, Print)
Standard C Implementation
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Insert at Head - O(1)
void insertHead(struct Node** head, int val) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = *head;
*head = newNode;
}
// Insert at Tail - O(N)
void insertTail(struct Node** head, int val) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if (*head == NULL) { *head = newNode; return; }
struct Node* temp = *head;
while (temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
// Reverse Linked List - O(N)
void reverseList(struct Node** head) {
struct Node *prev = NULL, *curr = *head, *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
// Print List
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertHead(&head, 30);
insertHead(&head, 20);
insertHead(&head, 10);
insertTail(&head, 40);
printf("Original List: "); printList(head);
reverseList(&head);
printf("Reversed List: "); printList(head);
return 0;
}
B. Find Middle Node (Floyd's Fast & Slow Pointers - O(N))
Interview Classic
// Finds middle element in single traversal pass
struct Node* findMiddle(struct Node* head) {
if (head == NULL) return NULL;
struct Node* slow = head;
struct Node* fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next; // Moves 1 step
fast = fast->next->next; // Moves 2 steps
}
return slow; // Slow pointer sits on exact middle node!
}
β 2. Doubly Linked List Standard Operations & Interview Programs
A. Complete Doubly Linked List (Insert, Delete, Forward & Backward Print)
Compilable C Code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Insert at Head - O(1)
void insertHeadDLL(struct Node** head, int val) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->prev = NULL;
newNode->next = *head;
if (*head != NULL) (*head)->prev = newNode;
*head = newNode;
}
// Print Forward & Backward
void printDLL(struct Node* head) {
struct Node* temp = head;
struct Node* last = NULL;
printf("Forward Traversal: ");
while (temp != NULL) {
printf("%d β ", temp->data);
last = temp;
temp = temp->next;
}
printf("NULL\n");
printf("Backward Traversal: ");
while (last != NULL) {
printf("%d β ", last->data);
last = last->prev;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertHeadDLL(&head, 50);
insertHeadDLL(&head, 40);
insertHeadDLL(&head, 30);
printDLL(head);
return 0;
}
B. Reverse Doubly Linked List (Swapping prev & next pointers - O(N))
Pointer Swap Algorithm
// Reverses a Doubly Linked List in-place by swapping prev and next pointers
void reverseDLL(struct Node** head) {
struct Node* temp = NULL;
struct Node* curr = *head;
while (curr != NULL) {
// Swap prev and next pointers
temp = curr->prev;
curr->prev = curr->next;
curr->next = temp;
// Move to next node (which is now curr->prev due to swap)
curr = curr->prev;
}
if (temp != NULL) {
*head = temp->prev;
}
}