您的位置:首页 > 编程语言 > C语言/C++

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

2015-11-08 21:54 537 查看

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

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

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

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "MyList_Single.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语言实现链表之单向链表”这个系列就全部结束了,希望大家可以给予指正,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息