C++ 单链表模板类实现
单链表的C语言描述
基本运算的算法——置空表、求表的长度、取结点、定位运算、插入运算、删除运算、建立不带头结点的单链表(头插入法建表)、建立带头结点的单链表(尾插入法建表),输出带头结点的单链表
#include<cstdio>
#include<iostream>
using namespace std;
template <class T>
class Linklist
{
private:
struct node
{
T date;
node * next;
node():next(NULL) {}
node(T d):date(d),next(NULL) {}
};
node* head;
node* tail;
node* cur;
int len;
public:
Linklist();
node* getNode()
{
return cur;
}
void setValue(T d)
{
node *tmp = new node(d);
tail->next = tmp;
tail = tmp;
len++;
}
T getValue()
{
T ret = cur->next->date;
cur = cur->next;
return ret;
}
bool hasNext()
{
if(cur->next == NULL)
{
cur = head;
return false;
}
return true;
}
int getLength()
{
return len;
}
bool Insert(Linklist<T> & dusk,int i,T e)
{
int j = 1;
node * p = new node(e);
tail = cur;
while(tail && j<i)
{
tail= tail ->next;
j++;
}
if(!tail||j>i)return false;
p->next = tail ->next;
tail->next = p;
len++;
return true;
}
bool Delete(Linklist <T> & dusk,int i)
{
int j = 1;
tail = cur;
while(tail&&j<i)
{
tail = tail -> next;
j++;
}
if(!tail || j>i)return false;
node * p = new node();
p= tail ->next;
tail->next = p->next;
delete(p);
return true;
}
bool Destroy(Linklist<T> & dusk)
{
node * p = new node();
if(head == NULL)
return true;
while(head)
{
p = head ->next;
delete(head);
head = p;
}
}
};
template <class T>
Linklist<T>::Linklist()
{
head = new node();
tail = head;
cur = head;
head -> next = NULL;
len = 0;
}
int main()
{
int a,b=99;
Linklist<int>dusk;
for(int i = 0; i < 10; i ++)
{
dusk.setValue(i);
}
while(dusk.hasNext())
{
cout<<dusk.getValue()<<" ";
}
cout<<endl;
while(dusk.hasNext())
{
cout<<dusk.getValue()<<" ";
}
}
建立不带头结点的单链表(头插入法建表)
#include<iostream>
using namespace std;
template <class T>
class Sqlist
{
private:
struct node{
T data;
node * next;
node():next(NULL){};
node(T d):data(d),next(NULL){};
};
public:
Sqlist(T d);
void setValue(T d)
{
node * tem = new node(d);
tem -> next =head;
head = tem;
len++;
}
T getValue()
{
T ret = cur->data;
cur = cur -> next;
return ret;
}
int len;
node * head;
node * cur;
};
template <class T>
Sqlist<T>::Sqlist(T d)
{
node * head = new node(d);
cur = head;
};
int main()
{
int a = 1;
Sqlist<int> dusk(a);
for(int i = 0 ; i < 5 ; i++)
dusk.setValue(i);
dusk.cur = dusk.head;
for(int i = 0 ; i< 5 ; i++)
{
cout<<dusk.getValue();
}
}
C++ 单链表模板类实现的更多相关文章
- C++实现一个单例模板类
单例模式在项目开发中使用得比较多,一个单例的模板类显得很有必要,避免每次都要重复定义一个单例类型 //非多线程模式下的一个单例模板类的实现 // template_singleton.h #inclu ...
- 数据结构—单链表(类C语言描写叙述)
单链表 1.链接存储方法 链接方式存储的线性表简称为链表(Linked List). 链表的详细存储表示为: ① 用一组随意的存储单元来存放线性表的结点(这组存储单元既能够是连续的.也能够是不连续的) ...
- 单链表sLinkList类,模板类
sLinkList模板类,单链表代码 /* 该文件按习惯可以分成.h文件和实现的.cpp文件 */ template <class elemType> class sLinkList { ...
- C++链表模板类
思想和上篇文章差不多,只是换了层包装. 直接上代码: // linklist.h #include <iostream> #include <cstdio> using nam ...
- 单链表的类的c++实现
#include<iostream> using namespace std;template <class T>struct linkNode{ T data; linkNo ...
- 一个基于C++11的单例模板类
#ifndef _SINGLETON_H_#define _SINGLETON_H_ template<typename T>class Singleton : public Uncopy ...
- C++实现线性表的链接存储结构(单链表)
将线性表的抽象数据类型定义在链接存储结构下用C++的类实现,由于线性表的数据元素类型不确定,所以采用模板机制. 头文件linklist.h #pragma once #include <iost ...
- 数据结构:单链表结构字符串(python版)添加了三个新功能
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
- 数据结构:单链表结构字符串(python版)改进
此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...
随机推荐
- tensorflow 关于 矩阵 运算 + 符号得含义。 2维 数组 + 1纬数组, 就是每一行都 加一边 1纬数组。 呵呵
小锋子Shawn(403568338) 13:51:23mnist.training.images?墨须(964489899) 13:51:27我的图片是100*100的,该怎么兼容. 小锋子S ...
- 微信企业号 发送信息 shell
微信企业号发送信息shell #可作为shell函数模块调用,用于微信通知.jenkins发版微信通知等等 # 微信API官方文档 https://work.weixin.qq.com/api/doc ...
- linux如何处理多连接请求?
1.TCP迭代服务器程序 这种方式就是服务器同一时间只处理一个客户端的请求,这个请求处理完以后才转向下一个客户请求.当然这样的服务器程序比较少见,这就像一个公司只能一次处理一个客户,后面的客户只能等待 ...
- python对excel表格进行操作
python 对 EXCEL 进行操作 背景:对excel表格中某一列进行base 64解码操作,由于数据量比较庞大,就考虑用Python代码完成. 首先,分析整个文件操作中分为三步,第一步,对需要解 ...
- jdk下载安装后为什么要设置环境变量?
因为电脑不知道javac这个命令是在C:\Program Files\JAVA\jdk1.8.0_65\bin的这个路径下面,所以我们要设置好环境变量,来让电脑知道其路径
- HDU 6538 Neko and quadrilateral(极角排序+旋转坐标系)
这道题简直太好了,对于计算几何选手需要掌握的一个方法. 首先对于求解四边形面积,我们可以将四边形按对角线划分成两个三角形,显然此时四边形的面积最大最小值就变成了求解里这个对角线最近最远的点对. 对于此 ...
- HDU-1394 Minimum Inversion Number (逆序数,线段树或树状数组)
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- Javascript | DOM\DOM树浅析
DOM Document Object Model(文档对象模型) 定义了表示和修改文档所需的方法. DOM对象即为宿主对象,由浏览器厂商定义,用来操作html和xml功能的一类对象的集合.也有人称D ...
- Docker实战部署应用——Redis
Redis 部署 拉取Redis镜像 docker pull redis 创建Redis容器 docker run -id --name=sun_redis -p 6379:6379 redis 客户 ...