Structure-based file-like record handling (in memory) Dry Run in C

Structure-based file-like record handling (in memory) is an interactive C dry run visualizer from the Struct Functions Pointers programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Structure-based file-like record handling (in memory) Program Code

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

struct Record {
    int id;
    char data[100];
    struct Record *next;
};

// File-like operations in memory
struct Record* createRecord(int id, char data[]) {
    struct Record *newRec = (struct Record*)malloc(sizeof(struct Record));
    newRec->id = id;
    strcpy(newRec->data, data);
    newRec->next = NULL;
    return newRec;
}

void insertRecord(struct Record **head, struct Record *rec) {
    rec->next = *head;
    *head = rec;
}

struct Record* searchRecord(struct Record *head, int id) {
    struct Record *current = head;
    while(current != NULL) {
        if(current->id == id)
            return current;
        current = current->next;
    }
    return NULL;
}

int deleteRecord(struct Record **head, int id) {
    struct Record *current = *head;
    struct Record *prev = NULL;
    
    while(current != NULL) {
        if(current->id == id) {
            if(prev == NULL) {
                *head = current->next;
            } else {
                prev->next = current->next;
            }
            free(current);
            return 1;
        }
        prev = current;
        current = current->next;
    }
    return 0;
}

void updateRecord(struct Record *head, int id, char newData[]) {
    struct Record *rec = searchRecord(head, id);
    if(rec != NULL) {
        strcpy(rec->data, newData);
        printf("Record %d updated\n", id);
    } else {
        printf("Record %d not found\n", id);
    }
}

void displayAll(struct Record *head) {
    struct Record *current = head;
    printf("\n--- All Records ---\n");
    while(current != NULL) {
        printf("ID: %d -> %s\n", current->id, current->data);
        current = current->next;
    }
}

void freeAll(struct Record *head) {
    struct Record *current = head;
    while(current != NULL) {
        struct Record *temp = current;
        current = current->next;
        free(temp);
    }
}

int main() {
    struct Record *database = NULL;
    
    // Simulate file operations
    insertRecord(&database, createRecord(1, "First record - User data"));
    insertRecord(&database, createRecord(2, "Second record - Configuration"));
    insertRecord(&database, createRecord(3, "Third record - Log entry"));
    
    displayAll(database);
    
    struct Record *found = searchRecord(database, 2);
    if(found) printf("\nFound: ID %d -> %s\n", found->id, found->data);
    
    updateRecord(database, 2, "Updated second record - New data");
    
    displayAll(database);
    
    deleteRecord(&database, 1);
    printf("\nAfter deleting ID 1:\n");
    displayAll(database);
    
    freeAll(database);
    
    return 0;
}

View the complete Struct Functions Pointers programs page.

Program Console Structure-based file-like record handling (in memory) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.