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方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...
随机推荐
- jt获取鼠标指针的位置
屏幕 screenX和screenY属性表示鼠标在整个显示屏的位置,从屏幕(而不是浏览器)的左上角开始计算的. 页面 pageX和pageY属性表示鼠标指针在这个页面的位置.页面的顶部可能在可见区域之 ...
- 【MM系列】SAP 主要模块及简介
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 主要模块及简介 前言部分 ...
- linux中安装gitlab和cicd(断网版)
1:先介绍一下怎么查找所需要的依赖包 #yum install rpmname--downloadonly --downloaddir=/rpmpath 例如:yum install gitlab-r ...
- 宝塔 windows下apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件
首先我们来看两段对上传目录设置无权限的列子,配置如下: //在宝塔下如用/upload这个路径应用无效,一定要C:/wwwroot/upload才有效果 <Directory "要去掉 ...
- String hashCode 这个数字,很多人不知道!
作者:coolblog segmentfault.com/a/1190000010799123 1. 背景 某天,我在写代码的时候,无意中点开了 String hashCode 方法.然后大致看了一下 ...
- RESUful风格
1.7 RESTful风格 1.7.1 RESTful风格介绍 RESTful是一种软件架构风格! RESTful架构风格规定,数据的元操作,即CRUD(create, read, update和de ...
- SpringBoot-Vue实现增删改查及分页小DEMO
前言 主要通过后端 Spring Boot 技术和前端 Vue 技术来简单开发一个demo,实现增删改查.分页功能以及了解Springboot搭配vue完成前后端分离项目的开发流程. 开发栈 前端 开 ...
- P2835 刻录光盘 (tarjan缩点)
[题目描述] 现在假设总共有N个营员(2<=N<=200),每个营员的编号为1~N.LHC给每个人发了一张调查表,让每个营员填上自己愿意让哪些人到他那儿拷贝资料.当然,如果A愿意把资料拷贝 ...
- 题解 P5265 【模板】多项式反三角函数
→_→ OI 生涯晚期才开始刷板子题的咱 其实这题就是道公式题,搞过多项式全家桶的同学贴贴板子照着公式码两下都能过... 至于公式的证明嘛...总之贴上公式: \[Arcsin(F)=\int{F'\ ...
- 0基础入门 docker 部署 各种 Prometheus 案例 - 程序员学点xx 总集篇
目录 大家好, 学点xx 系列也推出一段时间了.虽然 yann 能力有限,但还是收到了很多鼓励与赞赏.对这个系列 yann 还是很喜欢的,特别是 Prometheus 篇,在期间经历公众号 100 篇 ...