Facebook IconTwitter IconReddit IconLinkedIn IconPinterest Icon

What is Data Structure?

If you're learning programming, you've probably heard the term "data structure" thrown around. But what exactly is it? Why is it so important? And how does a computer store and manage data efficiently?

In this blog, we’ll break down the concept of data structures in simple terms, using real-world analogies and TypeScript examples. By the end, you’ll understand:

  1. What is Data? (How computers see and interpret data)
  2. What is a Data Structure? (How we organize data efficiently)
  3. Types of Data & Data Structures (Primitive vs. Non-Primitive)
  4. How Data Structures are Stored in Memory (RAM, Stack, Heap)

What is Data?

Imagine you have a box of LEGO bricks. Individually, each brick is simple:

  • A red 2x4 brick → Represents the number 5.
  • A blue 1x2 brick → Represents the letter 'A'.
  • A yellow round brick → Represents true or false.

But when you combine them? You can build anything—a spaceship, a house, or even a robot.

Similarly:

  • Raw data (like 5, 'A', true) is like individual LEGO pieces.
  • Data structures (like arrays, objects) are how you assemble them into meaningful structures.

How Computers See LEGO (Binary)

Computers don’t see "red" or "blue" bricks. They see:

  • 0101 (the number 5)
  • 01000001 (the letter 'A')
  • 1 (the boolean true)

Every piece of data is just a combination of 0s and 1s.

Another way to think data is like ingredients in a kitchen:

  • Flour, eggs, sugar → Basic data types (numbers, strings, booleans).
  • A recipe → A data structure (how you organize ingredients to make a cake).

Example: Baking a Cake (Storing Data)

// Ingredients (Primitive Data)
let flour: number = 2; // cups  
let sugar: number = 1; // cup  
let hasEggs: boolean = true;  

// Recipe (Data Structure - Object)
let cakeRecipe = {  
  flour: 2,  
  sugar: 1,  
  hasEggs: true,  
  steps: ["Mix", "Bake", "Serve"] // Array (another data structure)  
};

  • Without organization, ingredients are just random items in your kitchen.
  • With a recipe (data structure), they turn into something useful (a cake).

What is a Data Structure?

A data structure is a way to organize and store data so that it can be accessed and modified efficiently.

Imagine you're building something amazing—maybe a LEGO spaceship. Data structures are the ways you organize your pieces (data) to make building (programming) faster and easier.

A. Loose LEGO Blocks (Unstructured Data)

If you dump all your LEGO pieces into one big bin, finding the right block takes forever.

  • Problem: Need a red 2x4 brick? Good luck digging!
  • In Code: Variables scattered everywhere.
let block1 = "red 2x4";
let block2 = "blue 1x2";
let block3 = "yellow round";
// Hard to manage!

B. Sorted LEGO Trays (Arrays, Objects, Stacks, Queues)

Instead, you organize blocks into trays (data structures) for quick access.

1. Arrays → Numbered LEGO Trays (Ordered Lists)

  • Each slot has an index (0, 1, 2...).
  • Great for lists where order matters.
    • Pros: Fast access by position.
    • Cons: Adding/removing blocks in the middle is slow.
let legoTray: string[] = ["red 2x4", "blue 1x2", "yellow round"];
console.log(legoTray[0]); // "red 2x4" (fast access!)

2. Objects → Labeled LEGO Compartments (Key-Value Pairs)

  • Store blocks by name (key) instead of position.
    • Pros: Instant lookup by label.
    • Cons: No guaranteed order.
let legoKit = {
  wheels: "black round 2x2",
  windshield: "transparent 4x2",
  engine: "red 2x4"
};
console.log(legoKit.wheels); // "black round 2x2"

3. Stack → LEGO Tower (Last-In-First-Out / LIFO)

  • You can only add/remove from the top.
  • Like stacking LEGO pieces: last one placed is first one removed.
    • Real-world use: Undo/Redo operations (Ctrl+Z).
let legoStack: string[] = [];
legoStack.push("base"); // Add to top
legoStack.push("middle");
legoStack.push("top");
console.log(legoStack.pop()); // "top" (removed!)

4. Queue → LEGO Conveyor Belt (First-In-First-Out / FIFO)

  • Like a line for LEGO pieces: first in, first out.
    • Real-world use: Printer job scheduling.
let legoQueue: string[] = [];
legoQueue.push("piece1"); // Enqueue
legoQueue.push("piece2");
console.log(legoQueue.shift()); // "piece1" (first out!)

Why Do We Need Data Structures?

  • Speed: Some structures allow faster searches (e.g., Hash Tables).
  • Memory Efficiency: Some structures use memory better (e.g., Linked Lists vs. Arrays).
  • Real-World Problem Solving:
    • Arrays → Lists (shopping list)
    • Stacks → Undo/Redo operations (Ctrl+Z)
    • Queues → Printer job scheduling (First-In-First-Out)

Types of Data & Data Structures

At its core, data is just information. Data can be divided into two major data types:

  1. Primitive Data Types
  2. Non-primitive Data Types

Examples of data types:

# Primitive Data Types
const userId: number = 1001;
const nullValue: null = null;
const userName: string = "JohnDoe";
const isActiveUser: boolean = true;
const bigNumber: bigint = 9007199254740991n;
const undefinedValue: undefined = undefined;
const uniqueSymbol: symbol = Symbol("uniqueIdentifier");

# Non-primitive Data Types
const currentDate: Date = new Date();
const userRoles: Array = ["admin", "editor", "subscriber"];
const uniqueTags: Set = new Set(["typescript", "javascript", "webdev"]);

const userPreferences: object = {
  theme: "dark",
  language: "en",
  notifications: true,
};

const userSession = new Map([
  ["sessionId", "abc123xyz"],
  ["expiresAt", new Date("2023-12-31")],
]);

function greetUser(name: string): string {
  return `Hello, ${name}!`;
}

Data structures are ways to organize and store data so that it can be accessed and modified easily and efficiently. It can be divided into following categories:

Linear Data Structure:

  • In linear data structures, elements are arranged in a sequential order.
  • Each element is connected to its previous and next element, making traversal straightforward.

Non-Linear Data Structures:

  • In non-linear data structures, elements are not arranged sequentially.
  • They are connected in a hierarchical or network-like structure.

How Data is stored in Computers Memory?

Computers use RAM (Random Access Memory) to store data. When computer executes a program or code it creates two types of memory:

  • Stack Memory:
    • Fast access, fixed size.
    • Stores primitive data types and function calls.
    • Also stores reference or memory address for Non-primitive data types.
  • Heap Memory:
    • Slower access, flexible size.
    • Stores dynamic data like non-primitive data types.

Conclusion

  • Data is raw information (numbers, text, etc.).
  • Data Structures organize data for efficient access & modification.
  • Primitive types (numbers, strings) are stored in Stack.
  • Non-primitive structures (arrays, objects) are stored in Heap.
  • Choosing the right data structure optimizes performance.