上一次的C++链表实现两个单链表的连接不太理想,此次听了一些视频课,自己补了个尾插法,很好的实现了两个链表的连接,当然了,我也是刚接触,可能是C++的一些语法还不太清楚,不过硬是花了一些时间尽量在数据结构中将c++的语言特点表现出来。一开始也是不愿意读c++的数据结构,只是一种挑战心里,不想读着读着感觉自己太low了,c++的内容更加丰富,所以还得多多练习......

头文件

 #ifndef LIST_H
#define LIST_H
#include <iostream> template <class Type> class List; //前置声明
template <class Type> class ListIterator; //前置声明 //创建结点类
template <class Type> //类模板
class ListNode
{
friend class List<Type>; //友元函数--29行
friend class ListIterator<Type>; //友元函数--46行
private:
Type data;
ListNode *link;
ListNode(Type);
}; template <class Type>
ListNode<Type>::ListNode(Type element) //创建头结点
{
data = element;
link = ;
} //创建链表类
template <class Type>
class List
{
friend class ListIterator<Type>; //友元函数--46行
public:
List() { first=tail= ; };
void Insert(Type); //头插法
void Inserttail(Type); //尾插法
void Delete(Type); //按值删除元素
void Invert(); //反转链表
void Concatenate(List<Type>); //连接链表
void Show(); //显示链表做测试用,创建迭代器后可以不用它显示 private:
ListNode<Type> *first, *tail; // 创建头指针
}; /**************************************************************/
//创建迭代器类
template <class Type>
class ListIterator
{
public:
ListIterator(const List<Type>& l):list(l),current(l.first){}
bool NotNull();
bool NextNotNull();
Type* First();
Type* Next();
private:
const List<Type> &list;
ListNode<Type>* current;
}; template <class Type>
bool ListIterator<Type>::NotNull()
{
if (current) return true;
else return false;
} template <class Type>
bool ListIterator<Type>::NextNotNull()
{
if (current && current->link) return true;
else return false;
} template <class Type>
Type* ListIterator<Type>::First()
{
if (list.first) return &list.first->data;
else return ;
} template <class Type>
Type* ListIterator<Type>::Next()
{
if (current)
{
current = current->link;
return &current->data;
}
else return ;
}
/**************************************************************/ // 前插法
template <class Type>
void List<Type>::Insert(Type k)
{
ListNode<Type> *newnode = new ListNode<Type>(k); // 21行,新建结点并为data域赋值k //下面两行的意义就是头插法,将新建立的结点从头插入
newnode->link = first;
first = newnode;
} template <class Type>
void List<Type>::Inserttail(Type k)
{
if (tail != ) {
tail->link = new ListNode<Type>(k);
tail = tail->link;
}
else first = tail = new ListNode<Type>(k);
} template <class Type>
void List<Type>::Delete(Type k)
{
ListNode<Type> *previous = ;
ListNode<Type> *current;
for (current = first; current && current->data != k;
previous = current, current = current->link); if (current)
{
if (previous) previous->link = current->link;
else first = first->link;
delete current;
}
} template <class Type>
void List<Type>::Invert()
{
ListNode<Type> *p = first, *q = ;
while (p)
{
ListNode<Type> *r = q; q = p;
p = p->link;
q->link = r;
}
first = q;
} template <class Type>
void List<Type>::Concatenate(List<Type> b)
{
if (!first) { first = b.first; return; }
if (b.first)
{
ListNode<Type> *p;
for (p = first; p->link; p = p->link);
p->link = b.first;
}
} template <class Type>
void List<Type>::Show()
{
for (ListNode<Type> *current = first; current; current = current->link)
{
std::cout << current->data;
if (current->link) std::cout << "->";
}
std::cout << std::endl;
} #endif

源文件

 #include<iostream>
#include"List.h"
using namespace std; int main()
{
cout << "测试" << endl;
cout << "自建的迭代器" << endl;
List<int> intList;
intList.Insert();
intList.Insert();
intList.Insert();
intList.Insert(); /**************************************************************/
//可以先注释这段迭代器输出
ListIterator<int> li(intList);
if (li.NotNull())
{
cout << *li.First();
while (li.NextNotNull())
cout << "->" << *li.Next();
cout << endl;
}
/**************************************************************/ intList.Show();
intList.Invert();
intList.Show(); intList.Delete();
intList.Show();
intList.Delete();
intList.Show(); List<char> charList;
charList.Insert('a');
charList.Insert('b');
charList.Insert('c');
charList.Insert('d');
charList.Show();
charList.Invert();
charList.Show(); List<char> char2List;
char2List.Insert('e');
char2List.Insert('f');
char2List.Show();
char2List.Invert();
char2List.Show(); charList.Concatenate(char2List);
charList.Show(); List<int> intList2;
intList2.Inserttail();
intList2.Inserttail();
intList2.Inserttail();
intList2.Inserttail();
intList2.Show(); intList.Concatenate(intList2);
ListIterator<int> li2(intList);
if (li2.NotNull())
{
cout << *li2.First();
while (li2.NextNotNull())
cout << "->" << *li2.Next();
cout << endl;
} return ;
}

数据结构-单链表-类定义2-C++的更多相关文章

  1. 数据结构-单链表-类定义C++

    原理可访问https://www.cnblogs.com/yang901112/p/11674333.html 头文件 #ifndef RLIST_H #define RLIST_H #include ...

  2. C# 数据结构--单链表

    什么是单链表 这两天看到很多有关单链表的面试题,对单链表都不知道是啥的我.经过学习和整理来分享一下啥是单链表和单链表的一些基本使用方法.最后看些网上有关单链表的面试题代码实例. 啥是单链表? 单链表是 ...

  3. python算法与数据结构-单链表(38)

    一.链表 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括 ...

  4. python实现数据结构单链表

    #python实现数据结构单链表 # -*- coding: utf-8 -*- class Node(object): """节点""" ...

  5. C语言数据结构-单链表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作

    1.数据结构-单链表的实现-C语言 typedef struct LNode { int data; struct LNode* next; } LNode,*LinkList; //这两者等价.Li ...

  6. 数据结构——单链表java简易实现

    巩固数据结构 单链表java实现 单链表除了表尾 每个几点都有一个后继 结点有数据和后继指针组成  通过构建表头和表尾(尾部追加需要)两个特殊几点 实现单链表的一些操作,代码如下 package co ...

  7. 数据结构 - 单链表 C++ 实现

    单链表 单链表的定义 typedef int ElemType; typedef struct LNode { ElemType data; LNode *next; } LNode, *LinkLi ...

  8. 数据结构—单链表(类C语言描写叙述)

    单链表 1.链接存储方法 链接方式存储的线性表简称为链表(Linked List). 链表的详细存储表示为: ① 用一组随意的存储单元来存放线性表的结点(这组存储单元既能够是连续的.也能够是不连续的) ...

  9. 数据结构实验2:C++实现单链表类

    太简单了,直接贴题目然后上代码. 题目: 实验2 2.1 实验目的 熟练掌握线性表的链式存储结构. 熟练掌握单链表的有关算法设计. 根据具体问题的需要,设计出合理的表示数据的链式存储结构,并设计相关算 ...

随机推荐

  1. CentOS下载与服务器版安装(VMware)

    1. 下载 首先需要选择一个版本,因为华为云最新只提供了CentOS 7.6,所以要选择CentOS 7版本的. 官网只提供了最新的版本,而且服务器在国外,下载速度贼慢. 不过官方提供了分布在各个地区 ...

  2. name_scope与variable_scope 详解

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/lucky7213/article/deta ...

  3. ArcGIS Python人门到精通目录基于ArcGIS10.2,100以上案例15章42个视频806分钟,51GIS网站上线

    ArcGIS Python人门到精通目录 闫老师 QQ:276529800 微信13108507190 1.  ArcGIS Python基础 1.1  ArcGIS为什么学习Python 1.2 A ...

  4. Java_jdbc 基础笔记之三 数据库连接 (Statement)

    /** * 通过JDBC向之指定的数据表中插入一条记录 1 Statement :用于执行SQL语句的对象 * ==>通过Connection的createStatement()方法来获取 == ...

  5. script 命令/方法/函数

    $redis->script('load', $script); $redis->script('flush'); $redis->script('kill'); $redis-&g ...

  6. Docs-.NET-C#-指南-语言参考-预处理器指令:#endregion(C# 参考)

    ylbtech-Docs-.NET-C#-指南-语言参考-预处理器指令:#endregion(C# 参考) 1.返回顶部 1. #endregion(C# 参考) 2015/07/20 #endreg ...

  7. Objective-C轻量级泛型

    在Apple发布Xcode7的时候,不仅把Swift编程语言升级到了2.0版本,而且还对Objective-C做了许多提升,包括引入__nonnull/__nullable.其中,对于Objectiv ...

  8. maven项目新检出后不编译爬坑记 及 mvn clean package报错 WagonTransporterFactory: java.util.NoSuchElementException 异常【我】

    从SVN新检出一个maven项目,配置好后,发现项目无法编译(只有一个test包中的代码显示编译报错,其他所有包中的代码都不编译,也不报错), 先注释掉报错的test包中的所有内容, 用Eclipse ...

  9. Python高级笔记(八)with、上下文管理器

    1. 上下文管理器 __enter__()方法返回资源对象,__exit__()方法处理一些清除资源 如:系统资源:文件.数据库链接.Socket等这些资源执行完业务逻辑之后,必须要关闭资源 #!/u ...

  10. Qt编写气体安全管理系统14-邮件转发

    一.前言 邮件转发功能和短信告警功能基本一致,都是在判断报警后触发,可能稍微不同的是,邮件转发需要依赖互联网,而且能够发送的数据量很大,没有短信60个汉字的局限(当然短信也可以拆分多条发送,但是费钱, ...