上一次的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. 优化Unity游戏项目的脚本(上)

    本文将由捷克独立游戏工作室Lonely Vertex的开发工程师Ondřej Kofroň,分享C#脚本的一系列优化方法,并提供改进Unity游戏性能的最佳实践. 在开发游戏时,我们遇到了游戏过程中偶 ...

  2. js的prototype理解

    转载:https://www.cnblogs.com/douyage/p/8630529.html 在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实 ...

  3. Java中JVM内存结构

    Java中JVM内存结构 线程共享区 方法区: 又名静态成员区域,包含整个程序的 class.static 成员等,类本身的字节码是静态的:它会被所有的线程共享和是全区级别的: 属于共享内存区域,存储 ...

  4. Js 实现返回上一页

    Js 实现返回上一页 <a href="javascript:history.go(-1)">返回上一页</a> <a href="java ...

  5. 伪代码Pseudocode

    程序员之间交流,比划来比划去,与其用产品经理擅长的各种类图.时序图,还不如来一段伪代码来的直接! 伪代码 伪代码(Pseudocode)是一种算法描述语言.使用伪代码的目的是为了使被描述的算法可以容易 ...

  6. word: 插入或修改文字时后面的字消失 解决办法

    在编辑Word文档中的文字时,我们有时需要插入或修改文字,可是在插入或修改时会发现改动处后面的文字会消失.比如插入或修改3个字,后面的文字随之也会消失3个,这时该怎么办呢? 点击-“文件”-“选项”- ...

  7. logrotate机制&原理

    logrotate机制&原理 centos系统中默认安装logrotate,logrotate主配置文件:/etc/logrotate.conf,其中定义了系统默认的logrotate规则,当 ...

  8. netty5心跳与业务消息分发实例

    继续基于我们之前的例子(参见netty5自定义私有协议实例),这次我们加上连接校验和心跳机制: 只要校验通过,客户端发送心跳和业务消息是两个不同的事件发送的,彼此互不干扰.针对以上流程,我们需要增加4 ...

  9. Qt编写气体安全管理系统15-网络转发

    一.前言 在本系统中网络转发是个什么功能含义呢,其实就是将本地采集设备的所有数据打包发送到指定的网络地址,默认采用UDP的形式,无连接开销小,我也是看到很多的组态软件有这个功能,其实现有的很多的气体探 ...

  10. WebGL高级编程:开发Web3D图形 PDF(中文版带书签)

    WebGL高级编程:开发Web3D图形 目录 WebGL简介11.1 WebGL基础11.2 浏览器3D图形吸引人的原因21.3 设计一个图形API31.3.1 即时模式API31.3.2 保留模式A ...