文章目录

  • 1. 用C++实现链表
  • 2. 代码

1. 用C++实现链表

本文用到C++中的类模板,用类模板的好处就是方便建立任何类型的链表。但类模板这里有个坑就是无法分离编译,具体原因可以百度搜索类模板无法分离编译。最后废话不多说,直接上代码。

2. 代码

linklist.h文件实现顺序表的声明和定义

#ifndef LINKLIST_H
#define LINKLIST_H
#include <iostream>
using namespace std;
template <typename T>
class Node {public:/* 用于链表节点的定义 */T data; // 表示数据域Node<T> *next;  // 表示指针域,存储下一个节点的位置
};
template <typename T>
class LinkList : public Node<T>{private:/* 用于链表的定义 */Node<T> *head; // 头节点
public:/* 成员函数的声明 */LinkList(); // 重写默认的构造函数bool Empty(); // 判断链表是否为空int GetLen(); // 获取链表的长度void insert(T elem); // 默认插入链表的开头bool insert(int idx, T elem); // 在链表的指定位置插入元素void remove(T &elem); // 默认删除链表的第一个元素,并返回该元素bool remove(int idx, T &elem); // 删除并指定位置的元素bool index(int idx, T &elem); // 找出并返回指定位置的元素int index(bool (*compare(T, T)), T elem); // 找到并返回满足compare的元素void traverse(void (* print)(T &elem)); // 用于遍历整个链表
};/* 用于实现成员函数的定义 */
template <typename T>
inline LinkList<T>::LinkList() {this -> head = (Node<T> *)malloc(sizeof(Node<T>));if (!this -> head) {cerr << "Error allocating memory!" << endl;}this -> head -> next = nullptr;
}template <typename T>
inline bool LinkList<T>::Empty() {if (this -> head == nullptr) {return true;}return false;
} template <typename T>
int LinkList<T>::GetLen() {Node<T> *tmp = this -> head -> next;int counter = 0;while (tmp) {counter++;tmp = tmp -> next;}return counter;
}template <typename T>
inline void LinkList<T>::insert(T elem) {Node<T> *newnode = (Node<T> *)malloc(sizeof(Node<T>));newnode -> data = elem;newnode -> next = this -> head -> next;this -> head -> next = newnode;
}template <typename T>
bool LinkList<T>::insert(int idx, T elem) {if (idx < 1 || idx > this -> GetLen() + 1) {cerr << "subscript wrong!" << endl;return false;}int counter = 0;Node<T> *newnode = this -> head, *tmp = (Node<T> *)malloc(sizeof(Node<T>));while (counter < idx - 1 && newnode -> next != nullptr) {counter++;newnode = newnode -> next;}tmp -> data = elem;tmp -> next = newnode -> next;newnode -> next = tmp;return true;
}template <typename T>
void LinkList<T>::remove(T &elem) {Node<T> *tmp = this -> head -> next;this -> head -> next = tmp -> next;elem = tmp -> data;free(tmp);
}template <typename T>
bool LinkList<T>::remove(int idx, T &elem) {if (idx < 1 || idx > this -> GetLen()) {cerr << "subscript wrong!" << endl;return false;}Node<T> *newnode = this -> head, *tmp;int counter = 0;while (counter < idx - 1 && newnode -> next != nullptr) {newnode = newnode -> next;counter++;}tmp = newnode -> next;elem = tmp -> data;newnode -> next = tmp -> next;free(tmp);return true;
}template <typename T>
bool LinkList<T>::index(int idx, T &elem) {if (idx < 1 || idx > this -> GetLen()) {cerr << "subscript wrong!" << endl;return false;}Node<T> *newnode = this -> head -> next;int counter = 1;while (counter < idx) {counter++;newnode = newnode -> next;}elem = newnode -> data;return true;
}template <typename T>
int LinkList<T>::index(bool (*compare(T, T)), T elem) {Node<T> *newnode = this -> head;int counter = 0;while (newnode -> next != nullptr) {newnode = newnode -> next;counter++;if (compare(newnode -> data, elem)) {return counter;}}return -1;
}
template <typename T>
void LinkList<T>::traverse(void (* print)(T &elem)) {Node<T> *tmp = this -> head -> next;while (tmp) {print(tmp -> data);tmp = tmp -> next;}cout << endl;
}/* 用于定义非成员函数 */
template <typename T>
void show(LinkList<T> &L) {cout << "length : " << L.GetLen() << endl;
}template <typename T>
void print(T &elem) {cout << elem << " ";
}
#endif

main.cpp文件测试顺序表

#include <iostream>
#include "linklist.h"
using namespace std;
int main(int argc, char const *argv[])
{/* code */LinkList<int> L;L.insert(1);L.insert(2);L.insert(3);L.insert(4);L.insert(1,5);L.traverse(print);int tmp;L.remove(5,tmp);L.traverse(print);L.index(5, tmp);cout << tmp << endl;show(L);return 0;
}

喜欢的话,就点个关注吧。

链表(C++类模板实现)相关推荐

  1. 链表类模板的基本应用(c++)

    类模板都是以template开头写起,模式如下: template<class type> //其中class 是固定格式 type 可以指向任何一个数据的类型 如double等. //函 ...

  2. 泛型算法(lambda表达式、function类模板、bind函数适配器、迭代器类别、链表数据结构独有的算法)

    文章目录 概念 find()函数 迭代器令算法不依赖于容器 但算法依赖于元素类型的操作 算法永远不会执行容器的操作 只读算法 accumulate()函数 从两个序列中读取元素(equal函数为例) ...

  3. 基于C++类模板的链表

    一.基于C++类模板定义一个链表,管理学生成绩信息( 学号和成绩),进行排序后再输出. #include#includeusing namespace std; templateclass list{ ...

  4. 单链表(c++类模板实现)

    后续会写详细的实现过程,就是功能4,和功能13测试的时候还有点问题,不过会改进的 定义在"LinkedList.h" #include <iostream> using ...

  5. 泛函编程—模板函数_类模板

    函数业务逻辑一样,只是函数参数类型不同 函数模板的本质:类型参数化--泛型编程 语法: template <typename T> template <class T1,class ...

  6. wxWidgets:wxList< T >类模板的用法

    wxWidgets:wxList< T >类模板的用法 wxWidgets:wxList< T >类模板的用法 用法详细说明 wxWidgets:wxList< T &g ...

  7. 【C++深度剖析教程37】类模板的概念和意义

    加qq1126137994 微信:liu1126137994 一起学习更多技术!!! 1.类模板 一些类主要用于存储和组织数据元素 类中数据的组织方式和数据元素的具体类型无关 如 数组类,链表类,st ...

  8. MFC链表CList类

    一.类的概要和头文件 1. CList类:产生不定类型的列表,是一个集合类,也是一个双向链表类,是一个类模板. 2. #include "Afxtempl.h"   //包含头文件 ...

  9. C++的类继承与类模板

    类继承是面向对象编程中很重要(也是很难)的内容,其能有效地提高代码复用水平,提高开发的效率. 目录 基本概念 公有继承 私有继承.保护继承 包含 多重继承 类模板 基本概念 继承的种类与特点 C++中 ...

最新文章

  1. 时钟同步和时钟修正的小技巧
  2. 联想服务器 重装系统u盘启动,联想_Lenovo BIOS Setup Utility 设置U盘启动教程
  3. 几种开源的TCP/IP协议栈分析
  4. 《暗时间》的笔记-我在南大的七年
  5. 客户机操作系统已禁用 cpu。请关闭或重置虚拟机。_黑科技教学丨Win10竟然内置了一台虚拟机?教你如何玩转它...
  6. Sublime Text 3 详细安装教程
  7. 转:复杂网络分析总结
  8. InteliJ IDEA生成可执行jar运行提示没有主清单属性
  9. 【java学习之路】(java框架)007.IoC和DI注解开发
  10. nginx学习笔记01
  11. linux vi只写入1个字节,关于linux命令的说明(这是一个命令集)
  12. 基于jpress二次开发的H5商城(已开源)
  13. itext 生成 PDF(一)
  14. adobe cs5中文补丁 indesign_indesign cs5下载_Adobe InDesign CS5简体中文版下载 - 下载之家...
  15. 2018-DeepLabV3+论文解读
  16. 尺度空间-多尺度特征空间
  17. 六级未过,排名10%开外,如何保研浙大计算机?
  18. [SpringBoot](一)基本概念
  19. 书到用时方恨少? 整理了一份初中、高中数学教材pdf 百度云
  20. 超详细讲解!在字节跳动我是如何当面试官的,附大厂真题面经

热门文章

  1. ubuntu两个显示器只出一个_双显示器应用程序在错误的显示器
  2. 电子工程专业评副高总结_副高职称工作总结
  3. CReg类轻松读取注册表
  4. java三级审核_技术文档
  5. 【每日一题(8)】Mammoth's Genome Decoding CodeForces - 747B
  6. 清远市城市品牌及五大百亿农业产业区域公用品牌亮相
  7. 论文笔记:PICARD: Parsing Incrementally for Constrained Auto-Regressive Decoding from Language Models
  8. 基因数据处理70之Picard安装没成功
  9. matlab 从结构体struct提取某一字段filed
  10. 惠普服务器验收文档,通风工程-验收规范.doc