algoritmauzmanı
TREN
1Introduction
Lesson 1.1

What is a data structure?

A program keeps the data it works on in memory in some arrangement. That arrangement, together with the operations done on it, is called a . This lesson introduces the idea, puts two basic structures side by side and shows what the rest of the curriculum holds.

What you will learn
  • The two parts of a data structure: how the data is arranged in memory and the operations on it
  • The basic difference between an array and a linked list, and why no structure is best at everything
  • The order the curriculum follows and what each part covers
6 min

1A data structure: arrangement and operations

Imagine you want to keep a class's grades, a phone book or a city's road network in a program. Writing the data down somewhere is not enough; you also have to decide how you will reach it. A data structure is the description of how data is laid out in memory together with the you can do on it.

Typical operations are inserting an element, deleting an element, searching for a value and visiting every element in order. When the same data is kept in different structures, these operations cost different amounts. In one, searching is fast and inserting is slow; in another it may be the other way round.

A data structure is not a feature of a programming language. The same structure can be written in C, Java and Python; only the syntax changes. Every lesson on this site gives the code in all three.

2Why one structure is not enough

Let us compare two basic structures. An keeps its elements one after another in . So the place of element 3 can be calculated, and access by takes a single step. On the other hand, to insert at the front of an array you have to shift every element along by one slot.

120x100070x100430x100890x100c50x1010
Array: five integers side by side in memory. Each slot is 4 bytes; the addresses go up by 4.

A linked list keeps each element in its own . The nodes are scattered in memory; each one stores the address of the next in its field. Inserting at the front takes a few assignments, but to reach element 3 you have to start at the beginning and walk node by node.

NULL12?0x1a407?0x2c103?0x1b20head
Linked list: the same first three values, but the addresses are not consecutive. The next links set the order.
OperationArrayLinked list
Access by indexOne stepWalk from the start
Insert at the frontEvery element shiftsA few assignments
MemoryOne contiguous blockScattered nodes, each with an extra address

There is no single answer to which one is better. The answer depends on which operation the program does most often. How to put these differences into numbers is covered in A short look at Big-O, and the idea of an address in Memory, addresses and pointers.

3A map of the curriculum

The Data Structures field has nine parts. Each one builds on the one before:

  1. Introduction: concepts, memory and complexity. You are here.
  2. Arrays: contiguous memory and access by index. Starts with What is an array?.
  3. Linked Lists: lists built from nodes and pointers; singly linked, doubly linked and circular. Starts with Introduction to linked lists.
  4. Stack: last in, first out. The undo button and function calls work this way.
  5. Queue: first in, first out. Print queues and task queues work this way.
  6. Trees: hierarchical data, like folders and subfolders.
  7. Binary Trees: trees where each node has at most two children, and the binary search tree for fast lookups.
  8. Graphs: vertices and edges. Relationships such as road maps and friendship networks.
  9. Hashing: from key to address. The way to find a value in one step on average.

After data structures comes the Algorithms field: complexity analysis, recursion, searching, sorting, divide and conquer, greedy methods, dynamic programming and graph algorithms. Algorithms run on top of these structures; knowing the structures makes them easier to understand.

4Try it

Which two things define a data structure?

Show the answer

The answer is B. The same structure can be written in any language, and the number of elements can change. What defines a is how the data is laid out and which operations it supports at what cost.

A program keeps inserting at the front of a list and rarely reaches elements by index. Which structure fits better?

Show the answer

The answer is B. In an array, inserting at the front shifts every element. In a linked list the new node is linked in at the front and no other node is touched. This is drawn step by step in Inserting at the head.

Lessons you mark show up in the curriculum. This is kept on this device only.