Bird
0
0
DSA Cprogramming

Why Strings Are a Data Structure Not Just Text in DSA C - Why This Pattern

Choose your learning style9 modes available
Mental Model
A string is more than just letters; it is a sequence of characters stored in order, like a chain of boxes each holding one letter.
Analogy: Imagine a necklace made of beads where each bead is a letter. The order and connection of beads matter, not just the letters themselves.
S -> t -> r -> i -> n -> g -> null
↑
head
Dry Run Walkthrough
Input: string: "cat" stored as characters in sequence
Goal: Show how a string is stored as a sequence of characters linked in order, not just a word
Step 1: Store 'c' as first character in sequence
c -> null
↑ head
Why: We start the string with the first character stored at the beginning
Step 2: Add 'a' after 'c' in sequence
c -> a -> null
↑ head
Why: Each character is stored next to the previous one to keep order
Step 3: Add 't' after 'a' in sequence
c -> a -> t -> null
↑ head
Why: The string ends with the last character pointing to null to mark the end
Result:
c -> a -> t -> null
String stored as sequence of characters, not just text
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure for each character in string
typedef struct CharNode {
    char ch;
    struct CharNode* next;
} CharNode;

// Function to create a new node
CharNode* createNode(char c) {
    CharNode* newNode = (CharNode*)malloc(sizeof(CharNode));
    newNode->ch = c;
    newNode->next = NULL;
    return newNode;
}

// Function to build linked list string from char array
CharNode* buildString(const char* str) {
    if (str[0] == '\0') return NULL; // empty string
    CharNode* head = createNode(str[0]); // create first node
    CharNode* curr = head;
    for (int i = 1; str[i] != '\0'; i++) {
        curr->next = createNode(str[i]); // link next character
        curr = curr->next; // move to next node
    }
    return head;
}

// Function to print the linked list string
void printString(CharNode* head) {
    CharNode* curr = head;
    while (curr != NULL) {
        printf("%c -> ", curr->ch); // print character
        curr = curr->next; // move forward
    }
    printf("null\n");
}

int main() {
    const char* input = "cat";
    CharNode* stringList = buildString(input);
    printString(stringList);
    return 0;
}
CharNode* head = createNode(str[0]); // create first node
start string with first character node
curr->next = createNode(str[i]); // link next character
add next character node linked to previous
curr = curr->next; // move to next node
advance current pointer to newly added node
while (curr != NULL) { printf("%c -> ", curr->ch); curr = curr->next; }
traverse and print each character node in order
OutputSuccess
c -> a -> t -> null
Complexity Analysis
Time: O(n) because we create and print each character node once for n characters
Space: O(n) because we store each character in its own node in memory
vs Alternative: Compared to storing as plain text, linked nodes allow easy insertion or deletion without shifting all characters
Edge Cases
empty string ""
returns NULL pointer, no nodes created
DSA C
if (str[0] == '\0') return NULL; // empty string
single character string "a"
creates one node with 'a' and next as NULL
DSA C
CharNode* head = createNode(str[0]); // create first node
When to Use This Pattern
When you see problems about storing or manipulating text character by character, think of strings as linked sequences to handle changes efficiently.
Common Mistakes
Mistake: Treating strings as just text without structure, ignoring how characters are stored and linked
Fix: Understand strings as sequences of connected characters, each stored in nodes or array positions
Summary
Strings store characters in order as a sequence, not just as text.
Use this when you need to manipulate characters individually or efficiently.
The key is to see strings as connected characters, like beads on a string, not just words.