实现一个简单的链表,并使用TDD方法对其进行测试。

问题描述

我们要实现一个简单的链表,支持以下操作:

  1. 插入一个节点到链表的头部
  2. 删除链表中的一个节点
  3. 查找链表中的一个节点
步骤 1: 编写测试用例
// test_linked_list.c

#include <assert.h>
#include "linked_list.h"

void test_insert_node() {
    LinkedList *list = create_linked_list();
    insert_node(list, 1);
    assert(list->head->data == 1);
    insert_node(list, 2);
    assert(list->head->data == 2);
    assert(list->head->next->data == 1);
    free_linked_list(list);
}

void test_delete_node() {
    LinkedList *list = create_linked_list();
    insert_node(list, 1);
    insert_node(list, 2);
    delete_node(list, 1);
    assert(list->head->data == 2);
    assert(list->head->next == NULL);
    delete_node(list, 2);
    assert(list->head == NULL);
    free_linked_list(list);
}

void test_find_node() {
    LinkedList *list = create_linked_list();
    insert_node(list, 1);
    insert_node(list, 2);
    Node *node = find_node(list, 1);
    assert(node != NULL);
    assert(node->data == 1);
    node = find_node(list, 3);
    assert(node == NULL);
    free_linked_list(list);
}

int main() {
    test_insert_node();
    test_delete_node();
    test_find_node();
    printf("All tests passed!\n");
    return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
步骤 2: 实现功能

首先,定义链表的结构:

// linked_list.h

#ifndef LINKED_LIST_H
#define LINKED_LIST_H

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

typedef struct {
    Node *head;
} LinkedList;

LinkedList* create_linked_list();
void insert_node(LinkedList *list, int data);
void delete_node(LinkedList *list, int data);
Node* find_node(LinkedList *list, int data);
void free_linked_list(LinkedList *list);

#endif // LINKED_LIST_H
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

接着,实现链表的功能:

// linked_list.c

#include <stdlib.h>
#include "linked_list.h"

LinkedList* create_linked_list() {
    LinkedList *list = (LinkedList *)malloc(sizeof(LinkedList));
    list->head = NULL;
    return list;
}

void insert_node(LinkedList *list, int data) {
    Node *new_node = (Node *)malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = list->head;
    list->head = new_node;
}

void delete_node(LinkedList *list, int data) {
    Node *current = list->head;
    Node *prev = NULL;
    
    while (current != NULL && current->data != data) {
        prev = current;
        current = current->next;
    }
    
    if (current != NULL) {
        if (prev == NULL) {
            list->head = current->next;
        } else {
            prev->next = current->next;
        }
        free(current);
    }
}

Node* find_node(LinkedList *list, int data) {
    Node *current = list->head;
    while (current != NULL) {
        if (current->data == data) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

void free_linked_list(LinkedList *list) {
    Node *current = list->head;
    Node *next;
    
    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }
    
    free(list);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
步骤 3: 运行测试
gcc -o test_linked_list test_linked_list.c linked_list.c
./test_linked_list
  • 1.
  • 2.
解释
  • test_linked_list.c: 这个文件包含了所有的测试用例。我们使用 assert 函数来验证链表操作的输出是否符合预期。
  • linked_list.h: 头文件声明了链表的结构和函数,以便在测试代码和实现代码中都能使用。
  • linked_list.c: 这个文件包含了链表的实现,包括创建链表、插入节点、删除节点、查找节点和释放链表的函数。

通过这种方式,我们可以确保链表的基本功能在实现之前就被定义和测试,从而保证代码的正确性和健壮性。这就是TDD方法的应用。