C++数据结构模板,可以实现基本功能,用法和stl差不多,比如Q.pop();Q.push(a);Q.front();...... 

(由于动态链表用的不多,若有错误望各位大神不吝赐教:)

队列:

 class Queue
{
private:
int Head,Tail,Size;
int val[]; public: Queue()
{
Head=;
Tail=-;
Size=;
memset(val,,sizeof(val));
} inline bool empty()
{
return Size==;
} inline void push(const int & ele)
{
Tail=(Tail+)%;
Size++;
val[Tail]=ele;
return ;
} inline void pop()
{
if(Size==)return ;
Head=(Head+)%;
Size--;
return ;
} inline int front()
{
if(Size==)return ;
return val[Head];
} inline int back()
{
if(Size==)return ;
return val[Tail];
} inline int size()
{
return Size;
} inline void clear()
{
Head=;
Tail=-;
Size=;
memset(val,,sizeof(val));
return ;
}
}Q;

栈:

class    stack
{
private: int Size;
int val[]; int h_top; public: inline int top()
{
return val[h_top];
} inline int size()
{
return Size;
} inline bool pop()
{
if(Size==)return false;
val[h_top]=;
h_top--;
Size--;
return true;
} inline bool push(int temp_s)
{
h_top++;
Size++;
val[h_top]=temp_s;
return true;
} inline void clear()
{
memset(val,,sizeof(val));
h_top=-;
Size=;
return ;
} inline bool empty()
{
return Size==;
}
}st;

动态链表:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime> using namespace std; template<typename _Tp>
class List{
public: class List_val{
private:
List_val *_next;
List_val *_last;
_Tp _data; public:
List_val(){
_next=NULL;
_last=NULL;
_data=;
return ;
} public:
void insert_back(_Tp _new_data){
List_val *_new_elem;
_new_elem=new List_val;
if(_next!=NULL)_new_elem->_next=_next;
if(_next!=NULL)_next->_last=_new_elem;
_new_elem->_last=this;
_next=_new_elem;
_new_elem->_data=_new_data;
return ;
} void insert_front(_Tp _new_data){
List_val *_new_elem;
_new_elem=new List_val;
if(_last!=NULL)_new_elem->_last=_last;
if(_last!=NULL)_last->_next=_new_elem;
_new_elem->_next=this;
_last=_new_elem;
_new_elem->_data=_new_data;
return ;
} public:
List_val* next(){
return _next;
} List_val* last(){
return _last;
} _Tp data(){
return _data;
} public:
void change_next(List_val* _new_next){
_next= _new_next;
return ;
} void change_last(List_val* _new_last){
_last= _new_last;
return ;
} void change_data(_Tp _new_data){
_data= _new_data;
return ;
} void del(){
_last->_next=_next;
_next->_last=_last;
delete[] this;
}
}; private:
List_val *_begin;
List_val *_end; public:
List(){
_begin=new List_val;
_end =new List_val;
_begin->change_next(_end);
_end->change_last(_begin);
return ;
} void push_front(_Tp _new_data){
_begin->insert_back(_new_data);
return ;
} void push_back(_Tp _new_data){
_end->insert_front(_new_data);
return ;
} List_val* begin(){
return _begin->next();
} List_val* end(){
return _end;
} List_val* find(List_val* _from,_Tp _find_data){
List_val *i;
for(i=_from;i!=NULL;i=i->next()){
if(i->data()==_find_data)return i;
} return _end;
} void insert_back(List_val* _from,_Tp _new_data){
_from->insert_back(_new_data);
return ;
} void insert_front(List_val* _from,_Tp _new_data){
_from->insert_front(_new_data);
return ;
} void del(List_val* _from){
_from.del();
return ;
}
}; int main(){
List<int> l;
int i; for(i=;i<=;++i)
l.push_back(i);
for(i=;i<=;++i)
l.push_front(i);
List<int>::List_val *j;
for(j=l.begin();j!=l.end();j=j->next()){
cout << j->data() << ' ' ;
} cout << endl; l.find(l.begin(),)->insert_front();
l.find(l.begin(),)->insert_back();
l.find(l.begin(),)->del();
for(j=l.begin();j!=l.end();j=j->next()){
cout << j->data() << ' ' ;
} cout << endl; return ;
}

C++ 数据结构模板 队列 栈 动态链表 模板 Queue Stack List的更多相关文章

  1. 数据结构应用实例#栈&单链表#简易计算器

    修改BUG的时候一不小心BUG越修越多,鉴于维护程序并不是学习数据结构的初衷,我已经果断的弃坑了!! 以下内容再不更新,Github上的代码直接无法正常编译运行.... 参考参考就好,学习到栈的作用就 ...

  2. 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks

    class Queue { public: // Push element x to the back of queue. void push(int x) { while (!nums.empty( ...

  3. [Python] 数据结构--实现顺序表、链表、栈和队列

    说明: 本文主要展示Python实现的几种常用数据结构:顺序表.链表.栈和队列. 附有实现代码. 来源主要参考网络文章. 一.顺序表 1.顺序表的结构 一个顺序表的完整信息包括两部分,一部分是表中元素 ...

  4. c++学习书籍推荐《数据结构C++语言描述:应用标准模板库STL(第2版)》下载

    本书是Ford和Topp两位教授于1996看出版的名著Data Structures with C++的第2版,在全球范围内已经有数以万计的学生从中受益.作者将C++语言作为算法描述语言,应用包含规范 ...

  5. SDUT-2131_数据结构实验之栈与队列一:进制转换

    数据结构实验之栈与队列一:进制转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入一个十进制非负整数,将其转换成对 ...

  6. 【BZOJ 1507】【NOI 2003】&【Tyvj P2388】Editor 块状链表模板题

    2016-06-18 当时关于块状链表的想法是错误的,之前维护的是一个动态的$\sqrt{n}$,所以常数巨大,今天才知道原因TwT,请不要参照这个程序为模板!!! 模板题水啊水~~~ 第一次写块状链 ...

  7. C++采用模板实现栈的方法

    今天又看了遍<effective C++>,手动实现了一下条款42中的栈,贴出来当博客的处女贴. 首先栈的声明如下,采用了模板传入类型,而栈的底层采用是个链表. // stack.h // ...

  8. springboot mail整合freemark实现动态生成模板

    目标:1:springboot 整合 mail2: mail 使用freemark 实现模板动态生成(就是通过字符串生成模板,不需要在工程中写入固定模板)3: springboot 整合aop 实现日 ...

  9. Python列表操作与深浅拷贝(5)——数字处理函数、类型判断、列表链表队列栈

    python内建数据结构 分类 数值型: int float complex bool 序列对象: 字符串str 列表list 元组tuple 键值对: 集合set 字典dict 数值型 (list ...

随机推荐

  1. MySQL 字符编码问题详细解释

    http://www.codesoil.net/tag/charset Character Set Problem in PHP + MySQL4.1+ 和许多人一样,我也是在转移blog时才发现这个 ...

  2. 解决 EF where<T>(func) 查询的一个性能问题

    前两年帮朋友 做了个网吧管理软件,采用动软的三层架构 sql语句生成的.最近因功能变更 要改动,而我这段正在做asp.net mvc +ef+autofac的一个电商网站.索性 就把原来的底层全重新了 ...

  3. php打包文件夹成zip文件

    function addFileToZip($path,$zip){    $handler=opendir($path); //打开当前文件夹由$path指定.    while(($filenam ...

  4. codevs地鼠游戏(贪心)

    1052 地鼠游戏  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 王钢是一名学习成绩优异的学生,在平时的学习中,他 ...

  5. 判断人员js

    var allchooseEmpID = "";var allchooseEmpName = "";//自选经办人 function getJbrWinForM ...

  6. 简单 android popupwindow 实现

    1. popupWindow 设置大小: popupWindow = new  PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGr ...

  7. Linq学习(三)-基本查询

    一.本将主要介绍内容 从linq,sql,lambda三个角度比较来学习 select.orderby.分页.group by.distinct.子查询.in的用法 1.select 查询用户和它们的 ...

  8. Java系列学习(十二)-开始Eclipse

    1.用Eclipse来写一个HelloWorld (1)选择工作空间 工作空间其实就是我们写的源代码所在的目录 (2)创建一个Java项目 [File-New-Java Project] (3)创建包 ...

  9. MonoBehaviour简述

    Unity中的脚本都是继承自MonoBehaviour. 一.基础函数: 创建脚本就默认的update.start方法:(这些官方的文档都是有的) Start:Update函数第一次运行前调用,一般用 ...

  10. 回顾Google IO 2016 -Keynote【图解】

    Google IO大会倒计时进行中~~ 两名演奏者在使用高空“古筝”. 最后5秒倒计时~~~~全场轰动~ 倒计时结束,IO大会正式开始.屏幕中,一个人把纯白的唱片放入唱片机中. 然后欢快的音乐响起,台 ...