数据结构-单链表-类定义2-C++
上一次的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 ¤t->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++的更多相关文章
- 数据结构-单链表-类定义C++
原理可访问https://www.cnblogs.com/yang901112/p/11674333.html 头文件 #ifndef RLIST_H #define RLIST_H #include ...
- C# 数据结构--单链表
什么是单链表 这两天看到很多有关单链表的面试题,对单链表都不知道是啥的我.经过学习和整理来分享一下啥是单链表和单链表的一些基本使用方法.最后看些网上有关单链表的面试题代码实例. 啥是单链表? 单链表是 ...
- python算法与数据结构-单链表(38)
一.链表 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括 ...
- python实现数据结构单链表
#python实现数据结构单链表 # -*- coding: utf-8 -*- class Node(object): """节点""" ...
- C语言数据结构-单链表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作
1.数据结构-单链表的实现-C语言 typedef struct LNode { int data; struct LNode* next; } LNode,*LinkList; //这两者等价.Li ...
- 数据结构——单链表java简易实现
巩固数据结构 单链表java实现 单链表除了表尾 每个几点都有一个后继 结点有数据和后继指针组成 通过构建表头和表尾(尾部追加需要)两个特殊几点 实现单链表的一些操作,代码如下 package co ...
- 数据结构 - 单链表 C++ 实现
单链表 单链表的定义 typedef int ElemType; typedef struct LNode { ElemType data; LNode *next; } LNode, *LinkLi ...
- 数据结构—单链表(类C语言描写叙述)
单链表 1.链接存储方法 链接方式存储的线性表简称为链表(Linked List). 链表的详细存储表示为: ① 用一组随意的存储单元来存放线性表的结点(这组存储单元既能够是连续的.也能够是不连续的) ...
- 数据结构实验2:C++实现单链表类
太简单了,直接贴题目然后上代码. 题目: 实验2 2.1 实验目的 熟练掌握线性表的链式存储结构. 熟练掌握单链表的有关算法设计. 根据具体问题的需要,设计出合理的表示数据的链式存储结构,并设计相关算 ...
随机推荐
- JavaWeb之基础(1) —— 文件、目录结构和创建项目
1. JavaWeb应用 JavaWeb应用从大类上分为静态和动态两种. 静态应用就是传统的HTML文件+素材资源构造的静态网页,不需要特殊的配置.JavaWeb也不是专门用来做静态网站的. 动态应用 ...
- Ubuntu+QEMU+Xv6环境搭建
操作系统:Ubuntu 16.04 32位 虚拟机:VMware 模拟器:QEMU 之前有一台centos 64位虚拟机,使用源码安装配置环境,出了一些列问题,最终环境都已经配好了,也能够在qemu上 ...
- 巧用 CSS 实现酷炫的充电动画
循序渐进,看看只使用 CSS ,可以鼓捣出什么样的充电动画效果. 画个电池 当然,电池充电,首先得用 CSS 画一个电池,这个不难,随便整一个: 欧了,勉强就是它了.有了电池,那接下来直接充电吧.最最 ...
- 【mybatis源码学习】与spring整合Mapper接口执行原理
一.重要的接口 org.mybatis.spring.mapper.MapperFactoryBean MapperScannerConfigurer会向spring中注册该bean,一个mapper ...
- MQTT教學(二):安裝MQTT伺服器Mosquitto,Windows系統篇
http://swf.com.tw/?p=1005 「認識MQTT」文章提到,MQTT的訊息全都透過稱為代理人(broker)的伺服器交流.本文將說明頗受歡迎的開放原始碼MQTT伺服器Mosquitt ...
- android studio: 快捷键生成getter/setter方法时自动加m的问题
平时使用Android Studio 在写实体类的时候,习惯给实体类的成员变量前面加上一个"m" 修饰符表示这是一个成员变量,这也是搞java的一种约定俗成的写法,本来这是没有问题 ...
- 蓝牙BLE: 蓝牙4.0 BLE广播数据解析(转)
BLE 设备工作的第一步就是向外广播数据.广播数据中带有设备相关的信息.本文主要说一下 BLE 的广播中的数据的规范以及广播包的解析. 1. 广播模式 BLE 中有两种角色 Central 和 Per ...
- Centos7 安装 weblogic12.2.1.0.0
下载地址:地址:http://www.oracle.com/technetwork/middleware/weblogic/downloads/wls-main-097127.html 下载最新的we ...
- 基于keras的triplet_loss
https://blog.csdn.net/yjy728/article/details/79570554 https://blog.csdn.net/yjy728/article/details/7 ...
- Python3入门(十三)——常用内置模块之时间日期模块datatime
1.日期时间模块——datatime //其他模块例如time.calender等模块暂不展开 (1)获取当前时间:datatime.now(): from datetime import datet ...