A girl and a robot thinking together

Think together · Start with a story

Books, queue numbers, and routes on a map are all data, but the robot cannot organize them all in the same way.

Break down the process

Follow the numbered steps, then explain them in your own words
  1. Find a bookStore it by location
  2. Wait in lineServe people in order
  3. MapRecord connected routes

A data structure describes how data is organized, accessed, and changed.

01What happens when there is more data?

When a program has many values to manage, keeping them in a useful order makes it easier to find and update them.

02What is a data structure?

A data structure is a way to organize data so that particular operations are convenient.

Array

Elements are accessed by index.

Stack

The most recently added item comes out first.

Queue

The earliest arrival is handled first.

Tree / graph

Represent hierarchy and connections.

03Why not use arrays for every problem?

Arrays are important, but different problems need different operations.

For example:

  • A browser's “back” action is like a stack.
  • Print jobs handled in order are like a queue.
  • Folder hierarchy is like a tree.
  • Road connections between cities are like a graph.

04Can a data structure affect program speed?

Yes.

Finding one value may take very different amounts of work in different data structures.

Well suited organization

Access, insertion, and deletion feel natural.

Poorly suited organization

It may require lots of moving or repeated searching.

05How are data structures related to algorithms?

An algorithm tells a program “how to solve the problem.” A data structure tells it “how the data is organized.”

Data→Data structure→Algorithm processing

They are often designed together.

06Should you memorize code first when learning data structures?

No.

The most important thing in this topic is to first build a clear mental model:

  • How data is stored
  • Where it enters
  • Where it leaves
  • How the elements relate to each other

What you have learned

  • A data structure is a way to organize and manage data.
  • Different structures suit different operations and problems.
  • A data structure can affect program clarity and efficiency.
  • Data structures and algorithms are often designed together.
  • When learning, understand the model before learning the code interface.

Next: Stacks: Like a Pile of Plates

Your turn to try

If a school cafeteria calls out numbers, is it more like choosing a random person or serving people in order?

Ready? Open to see the explanation

Usually people are served in line order, which fits a queue model. The rule tells us to preserve the arrival order.