配列(Array)
ド定番のデータ構造。データを並べて管理。
forで順番に処理する時とか。
Python
arr = [10, 20, 30]
print(arr[1]) # 20
C
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
printf("%d\n", arr[1]); // 20
return 0;
}
連想配列 / 辞書とかとか
キーと値のペアでデータを管理する。こいつも定番。
APIのレスポンスや設定ファイルで多いよね。
Python
d = {"apple": 1, "banana": 2}
print(d["banana"]) # 2
リスト(Linked List)
要素が「次の要素」へのポインタを持つ構造。アルゴリズムを少し勉強してたら絶対通る道。
挿入・削除が頻繁なとき。あとは面接問題の定番。
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node a = {1, NULL};
struct Node b = {2, NULL};
a.next = &b;
printf("%d\n", a.next->data); // 2
return 0;
}
スタック(Stack)
後入れ先出し。初心者が詰まる。
Undoとか。
Python
stack = []
stack.append(1)
stack.append(2)
print(stack.pop()) # 2
キュー(Queue)
先入れ先出し。これも詰まる。
順番待ち。
Python
from collections import deque
queue = deque()
queue.append(1)
queue.append(2)
print(queue.popleft()) # 1
ヒープ(Heap)
優先度付きキュー。最小値や最大値を高速に取り出せる。
流石に普段使いしてる人は限られそう。
Dijkstra法など。
Python
import heapq
heap = []
heapq.heappush(heap, 3)
heapq.heappush(heap, 1)
print(heapq.heappop(heap)) # 1
木構造
階層構造。割と触れるよね。コンピュータサイエンスでは。
ファイル構造、UIコンポーネント、探索とか。
グラフ(Graph)
点と線で構成。まあWeb開発とかでは触れないと思う。
地図とか。
Pythonでは隣接リストで表すことが多い
graph = {
'A': ['B', 'C'],
'B': ['D'],
'C': ['D'],
'D': []
}