C语言实现链表之双向链表(十五)测试用例

本文详细介绍了如何使用C语言编写双向链表的测试用例,包括链表的插入、删除、遍历等操作的测试,确保链表功能的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C语言实现链表之双向链表(十五)测试用例


    上一篇文章给出了最后的两个函数,即链表打印和排序,这篇文章将给出所有函数的测试用例,即ListTestTop.c文件。

/*
*****************************************************************************************
*                                       UART Block
*
*                                      (c) Copyright
*                                   All Rights Reserved
*
* Filename :    MyList_Bidirection.c
*
* Function :    双向非循环链表功能及函数测试主程序
*
* History  :    1. wangyi  2015-4-19  17:47  Version 1.0  creat	
*
*****************************************************************************************
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "MyList_Bidirection.h"

int main()
{
    // 变量定义
    MyListData   ListData;
    MyListNode*  pHeadNode   = NULL;
    MyListNode*  pFindNode   = NULL;
    MyListNode** ppHeadNode  = &pHeadNode;   
    C_Bool       ImpleStatus = TRUE;
    char         cFindName[20];
    int          iAfterBefore = 0;
    int          iLocation    = 0;
    int          iListLen     = 0;
    C_Bool (*pf_compare)(MyListData, MyListData) = compare;

    // 为头结点分配内存并初始化
    pHeadNode = MallocMemInitNode(pHeadNode);
    if(pHeadNode == NULL)
    {
        fprintf(stderr, "The process of MallocMemInitNode occurs error.\n");
        exit(2);
    }

    // 为头结点输入数据
    fflush(stdin);      // 清空输入缓冲区
    printf("Please input the first node's name and age:");
    scanf("%s %d", pHeadNode->sNodeData.cName, &(pHeadNode->sNodeData.iAge));

    // 创建链表,直到输入的年龄为负数为止
    pHeadNode = CreatMyList(pHeadNode);
    if(pHeadNode == NULL)
    {
        fprintf(stderr, "The list is created defeatly.\n");
        exit(0);
    }

    // 打印创建完链表后的所有数据元素
    PrintfListDataNode(pHeadNode);

    /*
    // 输入新的头结点数据
    fflush(stdin);      // 清空输入缓冲区
    printf("Please input the new first node's name and age:");
    scanf("%s %d", ListData.cName, &(ListData.iAge));

    // 插入头结点
    pHeadNode = InsertFirstNode(pHeadNode, ListData);

    // 打印插入新的头结点后当前链表中的所有数据元素
    PrintfListDataNode(pHeadNode);

    // 删除头结点
    DeletFirstNode(ppHeadNode);

    // 打印删除头结点后的当前链表中的所有数据元素
    PrintfListDataNode(pHeadNode);
    */

    /*
    // 输入新的尾结点数据
    fflush(stdin);      // 清空输入缓冲区
    printf("Please input the new tail node's name and age:");
    scanf("%s %d", ListData.cName, &(ListData.iAge));

    // 插入尾结点
    pHeadNode = InsertTailNode(pHeadNode, ListData);

    // 打印插入新的尾结点后当前链表中的所有数据元素
    PrintfListDataNode(pHeadNode);

    // 删除尾结点
    DeletTailNode(ppHeadNode);

    // 打印删除尾结点后的当前链表中的所有数据元素
    PrintfListDataNode(pHeadNode);
    */

    /*
    // 输入新的结点数据
    fflush(stdin);      // 清空输入缓冲区
    printf("Please input the new node's name and age:");
    scanf("%s %d", ListData.cName, &(ListData.iAge));

    // 输入要插入的位置
    fflush(stdin);      // 清空输入缓冲区
    printf("Please input the name that you want to insert:");
    scanf("%s", cFindName);
    fflush(stdin);      // 清空输入缓冲区
    printf("If you want to insert after the name, please input 0, otherwise please input 1.\n");
    scanf("%d", &iAfterBefore);

    // 插入结点
    pHeadNode = InsertOtherNode(pHeadNode, ListData, cFindName, iAfterBefore);

    // 插入失败,没有找到结点
    if(pHeadNode == NULL)
    {
        fprintf(stderr, "Insert failed, now will exit.\n");
        exit(1);
    }

    // 打印插入新的结点后当前链表中的所有数据元素
    PrintfListDataNode(pHeadNode);

    // 输入要删除结点的位置
    fflush(stdin);      // 清空输入缓冲区
    printf("Please input the name that you want to delete:");
    scanf("%s", cFindName);

    // 删除结点
    ImpleStatus = DeletOtherNode(ppHeadNode, cFindName);

    // 没有找到要删除的结点
    if(ImpleStatus == FALSE)
    {
        fprintf(stderr, "Delete failed, now will exit.\n");
        exit(2);
    }

    // 打印删除结点后的当前链表中的所有数据元素
    PrintfListDataNode(pHeadNode);
    */

    /*
    // 设置数据元素
    strcpy(ListData.cName, "ziyou");
    ListData.iAge = 28;
    iLocation = 2;

    ImpleStatus = SetCurrentNodeData(pHeadNode, iLocation, ListData);
    if(ImpleStatus == FALSE)
    {
        fprintf(stderr, "The location is error, now will exit.\n");
        exit(3);
    }

    // 打印设置数据元素后链表中的所有数据元素
    PrintfListDataNode(pHeadNode);
    */
    
    /*
    // 获取数据元素
    iLocation = 2;
    ImpleStatus = GetCurrentNodeData(pHeadNode, iLocation, &ListData);
    if(ImpleStatus == FALSE)
    {
        fprintf(stderr, "The location is error, now will exit.\n");
        exit(3);
    }

    // 打印获取的数据元素
    printf("The name is %s, the age is %d.\n", ListData.cName, ListData.iAge);
    */
    
    /*
    // 测试链表是否为空
    ImpleStatus = CheckMyListEmpty(pHeadNode);
    if(ImpleStatus == TRUE)
    {
        printf("The list is empty.\n");
    }
    else
    {
        printf("The list is no empty.\n");
    }

    // 获取链表长度
    iListLen = GetMyListLen(pHeadNode);
    printf("The length of this list is %d.\n", iListLen);
    */

    // 寻找关键字对应的结点
    /*
    strcpy(ListData.cName, "wangyi");
    ListData.iAge = 21;
    pFindNode = FindListDataNode(pHeadNode, ListData, pf_compare);
    printf("The name is %s, the age is %d.\n", pFindNode->sNodeData.cName, pFindNode->sNodeData.iAge);
    */
    
    // 执行结束,释放内存
    ClearMyList(ppHeadNode);
    ppHeadNode  = NULL;
    return 0;
}

    这个测试用例对大部分功能都进行了测试,仅供参考,大家可以根据自己的需要进行更改测试。

    至此,“C语言实现链表之双向链表”这个系列就全部结束了,希望大家可以给予指正,谢谢。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值