"《算法导论》之‘线性表’":基于静态分配的数组的顺序表
2014-10-18 18:46
573 查看
首先,我们来搞明白几个概念吧(参考自网站数据结构及百度百科)。
顺序表和链表的比较如下:
BoostUnitTest.cpp
我在单元测试中偏向于使用REQUIRE型的判断,这样方便我检测出问题出现在哪。另外,我在单元测试中偏向于按“正常测试”和“不正常测试”两类进行测试。
本篇博文的代码均托管到Taocode : http://code.taobao.org/p/datastructureandalgorithm/src/.
线性表
线性表是最基本、最简单、也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。线性表的逻辑结构简单,便于实现和操作。在实现线性表数据元素的存储方面,一般可用顺序存储结构和链式存储结构两种方法。顺序表
用顺序存储方法存储的线性表简称为顺序表(Sequential List)。顺序表的存储方法是把线性表的结点按逻辑次序依次存放在一组地址连续的存储单元里。链表
链接方式存储的线性表简称为链表(Linked List)。顺序表和链表的比较如下:
// BoostUnitTest.cpp #define BOOST_TEST_MODULE ArrayList_Test_Module #include "stdafx.h" #include "D:\VSProject\Algorithm\List\SeqList\SeqList\SeqList\seqlist.h" struct ArrayList_Fixture { ArrayList_Fixture() { BOOST_TEST_MESSAGE("Setup fixture"); testArrayList = new SeqList(); } ~ArrayList_Fixture() { BOOST_TEST_MESSAGE("Teardown fixture"); delete testArrayList; } SeqList * testArrayList; }; // BOOST_AUTO_TEST_SUITE(ArrayList_Test_Suite) BOOST_FIXTURE_TEST_SUITE(ArrayList_Test_Suite, ArrayList_Fixture) BOOST_AUTO_TEST_CASE(ArrayList_Abnormal_Test) { // Set values to the array list int testArray[] = { 1, 2, 3, 4, 5 }; // 5 个元素 int testLenOfList = sizeof(testArray) / sizeof(int); testArrayList->setSeqList(testArray, testLenOfList); // BOOST_REQUIRE_THROW(testArrayList->setArrayList(testArray, testLenOfList), out_of_range); // Method getItem----------------------------------------------- // If the position of the item you want to get is less than zero BOOST_REQUIRE_THROW(testArrayList->getItem(-1), out_of_range); // If the position of the item you want to get is larger than the length of the list BOOST_REQUIRE_THROW(testArrayList->getItem(10), out_of_range); // Method insert------------------------------------------------- // If the inserting position is less than zero BOOST_REQUIRE_THROW(testArrayList->insert(-1, 10), out_of_range); BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList); // If the inserting position is larger than the length of the list BOOST_REQUIRE_THROW(testArrayList->insert(10, 10), out_of_range); BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList); // Method erase------------------------------------------------- // If the erasing position is less than zero BOOST_REQUIRE_THROW(testArrayList->erase(-1), out_of_range); BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList); // If the erasing position is larger than the length of the list BOOST_REQUIRE_THROW(testArrayList->erase(10), out_of_range); BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList); } BOOST_AUTO_TEST_CASE(ArrayList_Normal_Test) { bool expected; bool actual; // Method empty------------------------------------------------- expected = true; actual = testArrayList->empty(); BOOST_REQUIRE(expected == actual); // Set values to the array list int testArray[] = { 1, 2, 3, 4, 5 }; // 5 个元素 int testLenOfList = sizeof(testArray) / sizeof(int); testArrayList->setSeqList(testArray, testLenOfList); // BOOST_REQUIRE_THROW(testArrayList->setArrayList(testArray, testLenOfList), out_of_range); // Method getItem----------------------------------------------- BOOST_REQUIRE(testArrayList->getItem(1) == testArray[1]); // Method empty------------------------------------------------- expected = false; actual = testArrayList->empty(); BOOST_REQUIRE(expected == actual); // Method insert------------------------------------------------- expected = true; actual = testArrayList->insert(1, 10); BOOST_REQUIRE(expected == actual); BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList + 1); BOOST_REQUIRE(testArrayList->getItem(1) == 10); // Method erase------------------------------------------------- expected = true; actual = testArrayList->erase(1); BOOST_REQUIRE(expected, actual); BOOST_REQUIRE(testArrayList->getLenOfList() == testLenOfList); BOOST_REQUIRE(testArrayList->getItem(1) == testArray[1]); } BOOST_AUTO_TEST_SUITE_END();
BoostUnitTest.cpp
我在单元测试中偏向于使用REQUIRE型的判断,这样方便我检测出问题出现在哪。另外,我在单元测试中偏向于按“正常测试”和“不正常测试”两类进行测试。
本篇博文的代码均托管到Taocode : http://code.taobao.org/p/datastructureandalgorithm/src/.
相关文章推荐
- "《算法导论》之‘线性表’":基于动态分配的数组的顺序表
- "《算法导论》之‘线性表’":基于数组实现的单链表
- "《算法导论》之‘栈’":栈的三种实现(静态数组、动态数组及指针)
- "《算法导论》之‘队列’":队列的三种实现(静态数组、动态数组及指针)
- "《算法导论》之‘线性表’":双向循环链表
- 实现基于静态数组的顺序表
- 实现基于静态数组的顺序表的以下基本操作(进阶部分)
- 基于静态数组的顺序表的一些基本操作(2)
- 基于数组实现顺序存储的线性表
- 实现基于静态数组的顺序表的以下基本操作:
- 基于静态数组的顺序表的基本操作
- "《算法导论》之‘线性表’":基于指针实现的单链表
- 实现基于静态数组的顺序表的以下基本操作:
- !-- 基于静态数组的顺序表相关操作C语言实现 --!
- 实现基于静态数组的顺序表的以下基本操作:
- 实现基于静态数组的顺序表
- 基于静态数组的顺序表的一些基本操作(1)
- linux下使用vim实现基于静态数组的顺序表的一些基本操作
- 顺序表是线性表基于数组的存储表示
- homework-02 "最大子数组之和"的问题进阶