• 单链表的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. string与wstring的互相转换接口(Windows版本)

    string与wstring的互相转换接口(Windows版本) std::wstring stringToWstring( const std::string & str ) { LPCST ...

  2. 使用fiddler,提示系统找不到相应的文件FSE2.exe文件

    使用fiddler时候遇到了如下问题: Rules中customize rules 时,提示系统找不到相应的文件FSE2.exe文件. 这个文件的位置可以在Tools->opinions-> ...

  3. 【ABAP系列】SAP GUI740 PATCH5出现弹窗BUG

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP GUI740 PATCH ...

  4. python+selenium的frame表单切换

    switch_to.frame()                               切换frame switch_to.default_content()                切 ...

  5. java 历年版本特征(简化)

     无论是学习任何新知识,我都是从历史的角度着手,这让我对这些新知识是如何孕育出来的,有一个很好的理解 只从java 5-8,之后我也开始转nodejs了,感谢java伴随的这几年 Java5的新特性 ...

  6. [Python3] 019 函数:确认过参数,返回对的值

    目录 0. 函数简介 1. 初识函数 2. 函数的参数与返回值 少废话,上例子 3. 查找函数的帮助文档 4. 函数的参数 (1) 参数分类 (2) 结构介绍 1) 普通参数 2) 默认参数 3) 关 ...

  7. Javascript之谈对象

    谈谈如何理解对象 使用预定义对象只是面向对象语言的能力的一部分,ECMAScript 真正强大之处在于能够创建自己专用的类和对象.面向对象的语言有一个标志,那就是它们都有类的概念,而通过类可以创建任意 ...

  8. hdu-2389.rain on your parade(二分匹配HK算法)

    Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Ot ...

  9. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  10. 贪心(change)

    http://codeforces.com/gym/100989/problem/H After the data structures exam, students lined up in the ...