数据结构-单链表-类定义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 实验目的 熟练掌握线性表的链式存储结构. 熟练掌握单链表的有关算法设计. 根据具体问题的需要,设计出合理的表示数据的链式存储结构,并设计相关算 ...
随机推荐
- Linux/Centos下安装部署phantomjs
PhantomJS 是一个基于 WebKit 的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, ...
- sass登陆页面实例
sass登陆页面实例 一.总结 一句话总结: sass使用非常方便:使用就是将sass转化为css引入,并且动态监听让sass转化为css,可以很方便的所见即所得 1.sass安装? npm就可以按照 ...
- 下载GO的开源开发工具LITEIDE
下载GO的开源开发工具LITEIDE LITEIDE是免费且开源的GO IDE,支持WINDOWS, LINUX, MACOS https://sourceforge.net/projects/lit ...
- Debian9安装QT5.12.3
打开虚拟机,打开火狐浏览器,输入网址下载QT5.12(linux版本,约13.G) download.qt.io/archive/qt/5.12/5.12.0/ 文件默认下载在Downloads文件夹 ...
- 从0开始学爬虫7之BeautifulSoup模块的简单介绍
参考文档: https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ # 安装 beautifulsoup4 (pytools) D:\pyt ...
- 将一个多表关联的条件查询中的多表通过 create select 转化成一张单表的sql、改为会话级别临时表 【我】
将一个多表关联的条件查询中的多表通过 create select 转化成一张单表的sql 将结果改为创建一个会话级别的临时表: -- 根据下面这两个sql CREATE TABLE revenu ...
- libfacedetection环境配置
E:\Opencv\libfacedetection_install1\include E:\Opencv\libfacedetection_install1\lib libfacedetect-x6 ...
- 安卓 android studio 报错 All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com
这个问题是Android studio升级到3.0之后,运行的时候会提示gradle要升级到3.5版本才能编译.于是我把我的gradle升级到了 gradle-4.1-milestone-1 版本,是 ...
- matlab优化函数fminunc
一起来学演化计算-matlab优化函数fminunc 觉得有用的话,欢迎一起讨论相互学习~Follow Me fminunc 求无约束多变量函数的最小值 非线性编程求解器 找到指定问题的最小值,\(m ...
- 【437】Binary search algorithm,二分搜索算法
Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...