• 单链表的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++ 单链表模板类实现的更多相关文章

  1. C++实现一个单例模板类

    单例模式在项目开发中使用得比较多,一个单例的模板类显得很有必要,避免每次都要重复定义一个单例类型 //非多线程模式下的一个单例模板类的实现 // template_singleton.h #inclu ...

  2. 数据结构—单链表(类C语言描写叙述)

    单链表 1.链接存储方法 链接方式存储的线性表简称为链表(Linked List). 链表的详细存储表示为: ① 用一组随意的存储单元来存放线性表的结点(这组存储单元既能够是连续的.也能够是不连续的) ...

  3. 单链表sLinkList类,模板类

    sLinkList模板类,单链表代码 /* 该文件按习惯可以分成.h文件和实现的.cpp文件 */ template <class elemType> class sLinkList { ...

  4. C++链表模板类

    思想和上篇文章差不多,只是换了层包装. 直接上代码: // linklist.h #include <iostream> #include <cstdio> using nam ...

  5. 单链表的类的c++实现

    #include<iostream> using namespace std;template <class T>struct linkNode{ T data; linkNo ...

  6. 一个基于C++11的单例模板类

    #ifndef _SINGLETON_H_#define _SINGLETON_H_ template<typename T>class Singleton : public Uncopy ...

  7. C++实现线性表的链接存储结构(单链表)

    将线性表的抽象数据类型定义在链接存储结构下用C++的类实现,由于线性表的数据元素类型不确定,所以采用模板机制. 头文件linklist.h #pragma once #include <iost ...

  8. 数据结构:单链表结构字符串(python版)添加了三个新功能

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  9. 数据结构:单链表结构字符串(python版)改进

    此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...

随机推荐

  1. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_4字符输出流的基本使用_写出单个字符

    写完之后不刷新,则没有数据.数据只是写如到了内存缓冲区中 必须要调用flush方法,把数据刷新过去 close关闭的时候也会把数据刷新到文件中.这里把flush注释了也是可以的

  2. Series.str方法

    1 对dataframe的某一列用str处理后,其类型是<class 'pandas.core.strings.StringMethods'>.可以对df.['列名'].str直接进行切片 ...

  3. 【ABAP系列】SAP ABAP 仓库库存-物料拆分的算法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 仓库库存-物料 ...

  4. Android.应用软件.常用程序下载地址_20190913

    1. 1.1. 健康友行 微信 官网 https://weixin.qq.com/ 抖音 chrome 百度网盘(账号:osskill)中有 1.2. 支付宝 官网 https://mobile.al ...

  5. 解决django项目无法连接远程mysql的问题

    我们都知道django项目可以通过修改settings.py文件中的DATABASES这个对象,使用不同的数据库. 如图所示,我们想连接远程的mysql,修改settings.py的配置 然后我们在终 ...

  6. python+selenium的WebElement对象操作

    webelement对象操作 webelement对象是selenium中所有元素的父类,也就是webelement对象拥有的方法,其它元素对象都会有: 只是不同的对象在调用特定方法时,效果是不一样的 ...

  7. python+selenium元素定位之CSS学习02

    参考文档:https://www.runoob.com/cssref/css-selectors.html CSS选择器用于选择你想要的元素的样式的模式. "CSS"列表示在CSS ...

  8. Trie字典树详解

    今天上午省选字符串......只会KMP.连hash都不会的我被大佬虐惨了......于是我要发奋图强学习字符串,学习字符串当然就要从Trie树这种可爱的数据结构开始啦!!! 一.什么是Trie树?? ...

  9. 经典的最大流题POJ1273(网络流裸题)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  10. IDEA错误: 找不到或无法加载主类 com.xxx.freight.dofreight.doFreight解决办法

    1.右键点击工程,选择open Module Settings或点击File选择Project Structure,进入页面 2.选择Artifacts->JAR->From module ...