对于单链表的介绍部分参考自博文数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现

  1. 单链表介绍

  单向链表(单链表)是链表的一种,它由节点组成,每个节点都包含下一个节点的指针。

   1.1 单链表的示意图

  

  表头为空,表头的后继节点是"节点10"(数据为10的节点),"节点10"的后继节点是"节点20"(数据为10的节点),...

   1.2 单链表添加节点

  

  在"节点10"与"节点20"之间添加"节点15"
  添加之前:"节点10" 的后继节点为"节点20"。
  添加之后:"节点10" 的后继节点为"节点15",而"节点15" 的后继节点为"节点20"。

  需要注意的是在链表头部和其他地方添加结点是不一样的。

  在链表头部添加结点的关键代码为:

 NodePointer ptr = new Node();
ptr->data = val;
ptr->next = head;
head = ptr;

  在其他地方添加结点的关键代码为:

 NodePointer ptr = new Node(), tmpPtr = head;
ptr->data = val;
while(...){...}
ptr->next = tmpPtr->next;
tmpPtr->next = ptr;

   1.3 单链表删除节点

  

  删除"节点30"
  删除之前:"节点20" 的后继节点为"节点30",而"节点30" 的后继节点为"节点40"。
  删除之后:"节点20" 的后继节点为"节点40"。

  需要注意的是在链表首部、尾部和其他地方删除结点是不一样的。

  在链表首部删除结点的关键代码为:

 NodePointer ptr = head, tmpPtr;
...if (pos == ) // 在链表第一个位置
{
head = ptr->next;
delete ptr;
}

  在链表尾部删除结点的关键代码为:

 NodePointer ptr = head, tmpPtr;
while (...){...}
tmpPtr = ptr->next;
ptr->next = NULL;
delete tmpPtr;

  在其他地方删除结点的关键代码为:

 NodePointer ptr = head, tmpPtr;
while(...){...}
tmpPtr = ptr->next;
ptr->next = tmpPtr->next;
delete tmpPtr;

  2. 代码实现

  对于单链表,我定义了一个这样的类LinkedList

 // linkedlist.h
#ifndef LINKEDLIST
#define LINKEDLIST #include <iostream>
#include <cassert> using namespace std; typedef int ElementType; class Node
{
public:
ElementType data;
Node * next;
};
typedef Node * NodePointer; class LinkedList
{
public:
LinkedList();
virtual ~LinkedList();
LinkedList(const LinkedList& origlist);         // 拷贝构造函数
LinkedList& operator=(const LinkedList& origlist);  // 赋值运算符重载
void initList(ElementType * arr, int len);
bool isEmpty();
bool addNode(const int pos, const ElementType val);
bool deleteNode(const int pos);
void displayNodes();
NodePointer getNode(const int pos);
int getLenOfList(); private:
NodePointer head; }; #endif // LINKEDLIST

  实现代码如下:

 // linkedlist.cpp
#include "linkedlist.h" LinkedList::LinkedList()
{
head = NULL;
} LinkedList::~LinkedList()
{
NodePointer ptr = head, tmpPtr;
while (ptr != NULL)
{
tmpPtr = ptr;
ptr = ptr->next;
delete tmpPtr;
}
} LinkedList::LinkedList(const LinkedList& origlist)
{
//head = origlist.head; // 很容易写成这样,这样会造成浅拷贝
NodePointer ptr = origlist.head;
int i = ;
while (ptr != NULL)
{
addNode(i, ptr->data);
ptr = ptr->next;
i++;
}
} LinkedList& LinkedList::operator=(const LinkedList& origlist)
{
//head = origlist.head; // 很容易写成这样,这样会造成浅拷贝
NodePointer ptr = origlist.head;
int i = ;
while (ptr != NULL)
{
addNode(i, ptr->data);
ptr = ptr->next;
i++;
}
return *this;
} void LinkedList::initList(ElementType * arr, int len)
{
for (int i = ; i < len; i++)
{
addNode(i, arr[i]);
}
} bool LinkedList::isEmpty()
{
return head == NULL;
} bool LinkedList::addNode(const int pos, const ElementType val)
{
bool success = true;
int len = getLenOfList();
// assert(0 <= pos <= len);
if (pos < || pos > len)
{
cerr << "The node at position " << pos << " you want to add is less than zero or larger than "
<< "the length of list ." << endl;
success = false;
throw out_of_range("out_of_range");
}
else
{
NodePointer ptr = new Node();
ptr->data = val;
if (pos == ) // 如果添加的元素在第1个
{
ptr->next = head;
head = ptr;
}
else // 其他
{
NodePointer tmpPtr = head;
int count = ;
while (tmpPtr != NULL && count < pos - )
{
tmpPtr = tmpPtr->next;
count++;
}
ptr->next = tmpPtr->next;
tmpPtr->next = ptr;
} } return success;
} bool LinkedList::deleteNode(const int pos)
{
bool success = true;
int len = getLenOfList();
if (len == )
{
cerr << "There is no element in the list." << endl;
success = false;
}
else
{
NodePointer ptr = head, tmpPtr;
int count = ;
// assert(0 <= pos <= len);
if (pos < || pos > len - )
{
cerr << "The node at position " << pos << " you want to delete is less than zero or larger than "
<< "the length of list ." << endl;
success = false;
throw out_of_range("out_of_range");
}
else if (pos == ) // 在链表第一个位置
{
head = ptr->next;
delete ptr;
}
else if (pos == len - ) // 在链表最后一个位置
{
while (ptr != NULL && count < pos - )
{
ptr = ptr->next;
count++;
}
tmpPtr = ptr->next;
ptr->next = NULL;
delete tmpPtr;
}
else // 其他
{
while (ptr != NULL && count < pos - )
{
ptr = ptr->next;
count++;
}
tmpPtr = ptr->next;
ptr->next = tmpPtr->next;
delete tmpPtr;
}
}
return success;
} void LinkedList::displayNodes()
{
int len = getLenOfList();
if (len == )
{
cerr << "There is no element in the list." << endl;
}
else
{
NodePointer ptr = head;
int sequence = ;
while (ptr != NULL)
{
cout << "Seq: " << sequence << "; Data: " << ptr->data << "."<< endl;;
ptr = ptr->next;
sequence++;
}
} } NodePointer LinkedList::getNode(const int pos)
{
int len = getLenOfList();
if (len == )
{
cerr << "There is no element in the list." << endl;
return NULL;
}
else
{
// assert(0 <= pos <= len);
if (pos < || pos > len - )
{
cerr << "The item at position " << pos << " you want to get is less than zero or "
<< "larger than the length of list." << endl;
throw out_of_range("out_of_range");
// return NULL;
}
else
{
NodePointer ptr = head;
int count = ;
while (ptr != NULL && count < pos)
{
ptr = ptr->next;
count++;
}
return ptr;
}
}
} int LinkedList::getLenOfList()
{
int len = ;
NodePointer ptr = head;
while (ptr != NULL)
{
len++;
ptr = ptr->next;
}
return len;
}

linkedlist.cpp

  Boost单元测试代码如下:

 // BoostUnitTest.cpp
#define BOOST_TEST_MODULE LinkedList_Test_Module #include "stdafx.h"
#include "D:\VSProject\Algorithm\List\LinkedList\SingleLinkedList_BasedOnPointer\SingleLinkedList\SingleLinkedList\linkedlist.h" struct LinkedList_Fixture
{
public:
LinkedList_Fixture()
{
testLinkedList = new LinkedList();
}
~LinkedList_Fixture()
{
delete testLinkedList;
} LinkedList * testLinkedList; }; BOOST_FIXTURE_TEST_SUITE(LinkedList_Test_Suite, LinkedList_Fixture) BOOST_AUTO_TEST_CASE( LinkedList_Normal_Test )
{
// isEmpty --------------------------------------------
BOOST_REQUIRE(testLinkedList->isEmpty() == true); // getLenOfList ---------------------------------------
BOOST_REQUIRE(testLinkedList->getLenOfList() == ); // addNode & getNode ---------------------------------
BOOST_REQUIRE(testLinkedList->addNode(, ) == true);
BOOST_REQUIRE((testLinkedList->getNode())->data == );
BOOST_REQUIRE((testLinkedList->getNode())->next == NULL);
BOOST_REQUIRE(testLinkedList->isEmpty() == false);
BOOST_REQUIRE(testLinkedList->getLenOfList() == ); BOOST_REQUIRE(testLinkedList->addNode(, ) == true);
BOOST_REQUIRE((testLinkedList->getNode())->data == );
BOOST_REQUIRE((testLinkedList->getNode())->next == NULL);
BOOST_REQUIRE(testLinkedList->isEmpty() == false);
BOOST_REQUIRE(testLinkedList->getLenOfList() == ); BOOST_REQUIRE(testLinkedList->addNode(, ) == true);
BOOST_REQUIRE((testLinkedList->getNode())->data == );
BOOST_REQUIRE((testLinkedList->getNode())->next != NULL);
BOOST_REQUIRE(testLinkedList->isEmpty() == false);
BOOST_REQUIRE(testLinkedList->getLenOfList() == ); // deleteNode -----------------------------------------
BOOST_REQUIRE(testLinkedList->deleteNode() == true);
BOOST_REQUIRE((testLinkedList->getNode())->data == );
BOOST_REQUIRE(testLinkedList->getLenOfList() == ); BOOST_REQUIRE(testLinkedList->deleteNode() == true);
BOOST_REQUIRE((testLinkedList->getNode())->data == );
BOOST_REQUIRE(testLinkedList->getLenOfList() == ); BOOST_REQUIRE(testLinkedList->deleteNode() == true);
BOOST_REQUIRE(testLinkedList->getLenOfList() == ); // initList -------------------------------------------
int arr[] = { , , };
int len = sizeof(arr) / sizeof(int);
testLinkedList->initList(arr, len);
BOOST_REQUIRE(testLinkedList->getLenOfList() == );
BOOST_REQUIRE((testLinkedList->getNode())->data == );
BOOST_REQUIRE((testLinkedList->getNode())->data == );
BOOST_REQUIRE((testLinkedList->getNode())->data == );
BOOST_REQUIRE((testLinkedList->getNode())->next == NULL); } BOOST_AUTO_TEST_CASE(LinkedList_Abnormal_Test)
{
int arr[] = { , , };
int len = sizeof(arr) / sizeof(int);
testLinkedList->initList(arr, len); // addNode -------------------------------------------
BOOST_REQUIRE_THROW(testLinkedList->addNode(-, ), out_of_range);
BOOST_REQUIRE_THROW(testLinkedList->addNode(, ), out_of_range); // deleteNode ----------------------------------------
BOOST_REQUIRE_THROW(testLinkedList->deleteNode(-), out_of_range);
BOOST_REQUIRE_THROW(testLinkedList->deleteNode(), out_of_range); // getNode --------------------------------------------
BOOST_REQUIRE_THROW(testLinkedList->getNode(-), out_of_range);
BOOST_REQUIRE_THROW(testLinkedList->getNode(), out_of_range); } BOOST_AUTO_TEST_CASE(LinkedList_CopyConstuctor_Test)
{
int arr[] = { , , };
int len = sizeof(arr) / sizeof(int);
testLinkedList->initList(arr, len); //LinkedList * testLinkedList2(testLinkedList); // 特别容易写成这样,这样导致的结果就是testLinkedList2和
// testLinkedList指向同一块内存这样的写法才是正确的
// 该句等同于
// LinkedList * testLinkedList2;
// testLinkedList2 = testLinkedList;
//LinkedList testLinkedList2(*testLinkedList); // 要不就这样子定义,只不过此时testLinkedList2不是一个指针
LinkedList * testLinkedList3 = new LinkedList(*testLinkedList);
BOOST_REQUIRE(testLinkedList3->getLenOfList() == );
BOOST_REQUIRE((testLinkedList3->getNode())->data == );
BOOST_REQUIRE((testLinkedList3->getNode())->data == );
BOOST_REQUIRE((testLinkedList3->getNode())->data == );
BOOST_REQUIRE((testLinkedList3->getNode())->next == NULL);
} BOOST_AUTO_TEST_CASE(LinkedList_EqualOperator_Test)
{
int arr[] = { , , };
int len = sizeof(arr) / sizeof(int);
testLinkedList->initList(arr, len); // LinkedList * testLinkedList2 = testLinkedList; // 错误的写法
LinkedList * testLinkedList2 = new LinkedList();
*testLinkedList2 = *testLinkedList; BOOST_REQUIRE(testLinkedList2->getLenOfList() == );
BOOST_REQUIRE((testLinkedList2->getNode())->data == );
BOOST_REQUIRE((testLinkedList2->getNode())->data == );
BOOST_REQUIRE((testLinkedList2->getNode())->data == );
BOOST_REQUIRE((testLinkedList2->getNode())->next == NULL);
} BOOST_AUTO_TEST_SUITE_END()

BoostUnitTest.cpp

  本篇博文的代码均托管到Taocode : http://code.taobao.org/p/datastructureandalgorithm/src/.

"《算法导论》之‘线性表’":基于指针实现的单链表的更多相关文章

  1. "《算法导论》之‘线性表’":基于数组实现的单链表

    对于单链表,我们大多时候会用指针来实现(可参考基于指针实现的单链表).现在我们就来看看怎么用数组来实现单链表. 1. 定义单链表中结点的数据结构 typedef int ElementType; cl ...

  2. JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)

    前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...

  3. ACM金牌选手算法讲解《线性表》

    哈喽,大家好,我是编程熊,双非逆袭选手,字节跳动.旷视科技前员工,ACM亚洲区域赛金牌,保研985研究生,分享算法与数据结构.计算机学习经验,帮助大家进大厂~ 公众号:『编程熊』 文章首发于: ACM ...

  4. 已知长度为n的线性表采用顺序结构,写一算法删除该线性表中所有值为item的元素

    /** * @author:(LiberHome) * @date:Created in 2019/2/27 23:34 * @description: * @version:$ */ /*已知长度为 ...

  5. 数据结构与算法系列2 线性表 使用java实现动态数组+ArrayList源码详解

    数据结构与算法系列2 线性表 使用java实现动态数组+ArrayList源码详解 对数组有不了解的可以先看看我的另一篇文章,那篇文章对数组有很多详细的解析,而本篇文章则着重讲动态数组,另一篇文章链接 ...

  6. 数据结构(1) 第一天 算法时间复杂度、线性表介绍、动态数组搭建(仿Vector)、单向链表搭建、企业链表思路

    01 数据结构基本概念_大O表示法 无论n是多少都执行三个具体步骤 执行了12步 O(12)=>O(1) O(n) log 2 N = log c N / log c N (相当于两个对数进行了 ...

  7. 数据结构与算法系列2 线性表 链表的分类+使用java实现链表+链表源码详解

    数据结构与算法系列2.2 线性表 什么是链表? 链表是一种物理存储单元上非连续,非顺序的存储结构,数据元素的逻辑顺序是通过链表的链接次序实现的一系列节点组成,节点可以在运行时动态生成,每个节点包括两个 ...

  8. javascript实现数据结构与算法系列:线性表的静态单链表存储结构

    有时可借用一维数组来描述线性链表,这就是线性表的静态单链表存储结构. 在静态链表中,数组的一个分量表示一个结点,同时用游标(cur)代替指针指示结点在数组中的相对位置.数组的第0分量可看成头结点,其指 ...

  9. 数据结构导论 四 线性表的顺序存储VS链式存储

    前几章已经介绍到了顺序存储.链式存储 顺序存储:初始化.插入.删除.定位 链式存储:初始化.插入.删除.定位 顺序存储:初始化 strudt student{ int ID://ID char nam ...

随机推荐

  1. 用Python递归解决阿拉伯数字转为中文财务数字格式的问题(2)--打开思路的一种方法

    几天前自己写了个将阿拉伯数字转为中文财务数字的程序.用的递归,不幸的是它是树形递归. 虽然实际过程中不太可能出现金额数字大到让Python递归栈溢出,但是始终是一块心病,这玩意终究在理论上是受限制的. ...

  2. Nginx - 代理、缓存

    Nginx 标签 : nginx 代理 代理服务可简单的分为正向代理和反向代理: 正向代理: 用于代理内部网络对Internet的连接请求(如VPN/NAT),客户端指定代理服务器,并将本来要直接发送 ...

  3. EJB3基本概念、运行环境、下载安装与运行jboss

    EJB3基本概念 什么是EJB: EJB(EnterpriceJavaBeans)是一个用于分布式业务应用的标准服务端组件模型.采用EJB架构编写的应用是可伸的.事务性的.多用户安全的.采用EJB编写 ...

  4. XML之DOM解析模型

    <?xml version= "1.0" encoding = "UTF-8"> <articles> <article cate ...

  5. VNC 登录上去灰屏,没有shell脚本,鼠标变成X

    CenterOS 1.安装vncserver yum install tigervnc-server -y 2.vncpasswd 设置pwd 3./etc/sysconfig/vncservers ...

  6. everything of people’s life can changed in their twenties

    还记得三年前,独自背着行李,流浪远方,来到曾经只在地理课本上才熟悉的北国,带着好奇,带着期望,带着激动的心情,想感受毛爷爷当年霸气的北国风光,千里冰封的美丽,想知道北方的面条到底有多少种花样,想走进那 ...

  7. 剑指Offer——归并排序思想应用

    剑指Offer--归并排序思想应用 前言 在学习排序算法时,初识归并排序,从其代码量上感觉这个排序怎么这么难啊.其实归并排序的思想很简单:将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列 ...

  8. Android简易实战教程--第九话《短信备份~二》

    这一篇,承接地八话.使用高效的方式备份短信--xml序列化器. 存储短信,要以对象的方式存储.首先创建javabean: package com.itydl.createxml.domain; pub ...

  9. UNIX网络编程——SOCKET API和TCP STATE的对应关系_三次握手_四次挥手及TCP延迟确认

    在socket系统调用中,如何完成三次握手和四次挥手: SOCK_DGRAM即UDP中的connect操作知识在内核中注册对方机器的IP和PORT信息,并没有建立连接的过程,即没有发包,close也不 ...

  10. Oracle dblink详解

     database link概述 database link是定义一个数据库到另一个数据库的路径的对象,database link允许你查询远程表及执行远程程序.在任何分布式环境里,databas ...