学习目标

  • 用 C 实现常见数据结构
  • 理解内存布局

3.1 链表

typedef struct Node {
    int data;
    struct Node *next;
} Node;

Node* create_node(int data) {
    Node *node = malloc(sizeof(Node));
    node->data = data;
    node->next = NULL;
    return node;
}

3.2 栈

typedef struct {
    int *data;
    int top;
    int capacity;
} Stack;

void push(Stack *s, int val) {
    s->data[++s->top] = val;
}

int pop(Stack *s) {
    return s->data[s->top--];
}

实践练习

实现链表、栈、队列。

下一步

第 4 章:编译过程详解

更新时间: