模拟实现STL中的list
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
template<class T>
struct __ListNode
{
__ListNode<T>* _prev;
__ListNode<T>* _next;
T _data;
__ListNode(const T&x)
:_data(x)
, _prev(NULL)
, _next(NULL)
{}
};
template<class T,class Ref,class Ptr>
struct __ListIterator
{
typedef __ListNode<T> Node;
typedef __ListIterator<T, Ref, Ptr> Self;
__ListIterator(Node* node)
:_node(node)
{}
__ListIterator()
{}
Node* _node;
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
bool operator==(const Self&s)const
{
return _node == s._node;
}
bool operator!=(const Self&s)const
{
return _node != s._node;
}
Self& operator++() //前置
{
_node = _node->_next;
return *this;
}
Self& operator++(int)
{
Self temp(_node);
_node = _node->_next;
return *this;
}
Self& operator--()
{
_node = _node->_prev;
return *this;
}
Self& operator--(int)
{
Self tmp(_node);
_node = _node->_prev;
return *this;
}
};
template<class T>
class List
{
typedef __ListNode<T> Node;
public:
typedef __ListIterator<T, T&, T*> Iterator; //迭代器
typedef __ListIterator<T, const T&, const T*> ConstIterator; //const迭代器
Node* Buynode(const T&x)
{
Node* node = new Node(x);
return node;
}
List()
{
_head = Buynode(T());
_head->_next = _head;
_head->_prev = _head;
}
//void PushBack(const T&x)
//{
// Node* tail = _head->_prev;
// Node* tmp = Buynode(x);
// tail->_next = tmp->_next;
// tmp->_prev = tail;
// tmp->_prev = tail;
// tmp->_next = _head;
// // // tail->_next = tmp;
// // // tmp->_prev = tail;
// //
// // // tmp->_next = _head;
// // // _head->_prev = tmp;
//}
void PushBack(const T&x)
{
Insert(End(),x);
}
void PushFront(const T&x)
{
Insert(Begin(), x);
}
void Pop()
{
Erase(--End());
}
void PopFront()
{
Erase(Begin());
}
void Insert(Iterator pos,const T&x) //在pos前面插入
{
Node* cur = pos._node;
Node* prev = cur->_prev; //在prev后面插入
Node* tmp = Buynode(x);
prev->_next = tmp->_next;
tmp->_prev = prev;
prev->_next = tmp;
tmp->_next = cur;
// tmp->_next = cur;
// cur->_prev = tmp;
//
// prev->_next = tmp;
// tmp->_prev = prev;
}
void Erase(Iterator pos) //在pos前面删除
{
assert(pos != End());
Node* cur = pos._node->_prev; //要删除的元素
Node* prev = cur->_prev;
prev->_next = cur->_next;
prev->_next = prev;
delete cur;
}
Iterator Begin()
{
return Iterator(_head->_next);
}
Iterator End()
{
return Iterator(_head);
}
private:
Node* _head;
};
void main()
{
//TestList();
List<int> l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
l.PushBack(4);
List<int>::Iterator it = l.Begin();
while (it != l.End())
{
cout << *it << " ";
++it;
}
cout << endl;
}
模拟实现STL中的list的更多相关文章
- 用数组模拟STL中的srack(栈)和queue(队列)
我们在理解stack和queue的基础上可以用数组来代替这两个容器,因为STL中的stack和queue有可能会导致程序运行起来非常的慢,爆TLE,所以我们使用数组来模拟他们,不仅可以更快,还可以让代 ...
- 适配器模式—STL中的适配器模式分析
适配器模式通常用于将一个类的接口转换为客户需要的另外一个接口,通过使用Adapter模式能够使得原本接口不兼容而不能一起工作的类可以一起工作. 这里将通过分析c++的标准模板库(STL)中的适配器来学 ...
- 模拟实现STL库
最近在复习STL,感觉再看的时候比刚开始学的时候通透很多.以前模拟实现了一个STL库,最近复习完又重构了一遍.代码放出来以供后面学习.如果有写的不好的地方欢迎大家批评指正. STL_List.h #p ...
- C++STL中的vector的简单实用
[原创] 使用C++STL中的vector, #include <stdio.h> #include<stdlib.h> #include<vector> usin ...
- C++ 队列!还是要从 STL 中的说起……
1. 前言 队列和栈一样,都是受限的数据结构. 队列遵循先进先出的存储原则,类似于一根水管,水从一端进入,再从另一端出去.进入的一端称为队尾,出去的一端称为队头. 队列有 2 个常规操作: 入队:进入 ...
- STL中的set容器的一点总结
1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...
- STL中的next_permutation
给定一个数组a[N],求下一个数组. 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1 ..... 在STL中就有这个函数: 1.参数是(数组的第一个元素,数组的末尾),注意这是前闭后开 ...
- 3.2 STL中的函数对象类模板
*: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...
- C++的模板特化 和 STL中iterator_traits模板的偏特化
C++中有类模板和函数模板,它们的定义如下所示: 类模板: template<class T1,class T2> class C { //... }; 函数模板: template< ...
随机推荐
- PHP READ PPT FILE
function parsePPT($filename) { // This approach uses detection of the string "chr(0f).Hex_value ...
- HDU 1385 Minimum Transport Cost (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...
- Es使用。
http://jingyan.baidu.com/article/3052f5a1e8a06397f31f8699.html --------------------------- http://el ...
- Visual Studio 常用快捷键 (二)
想不到上一篇 [Visual Studio 常用快捷键] 受这么多人的欢迎.看来大家对Visual Studio的用法非常感兴趣. 接下来我准备写一个 “Visual Studio使用技巧 ” 一个系 ...
- hdu 4722 Good Numbers 数位DP
数位DP!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include< ...
- 使用jmeter对websocket进行压力测试[转载]
前段时间本着练习angularJS+requireJS的目的写了一个基于nodeJS和socket.io的聊天室,github地址为:https://github.com/towersxu/node- ...
- CoreData的简单使用(一)数据库的创建
iOS有多种数据持久化得方式 plist文件(属性列表) preference(偏好设置,NSUserDefaults) NSKeyedArchiver(归档,用的不多) SQLite 3 (需要导入 ...
- TCP协议RST:RST介绍、什么时候发送RST包
TCP协议RST:RST介绍.什么时候发送RST包 RST标示复位.用来异常的关闭连接. 1. 发送RST包关闭连接时,不必等缓冲区的包都发出去,直接就丢弃缓冲区中的包,发送RST. 2. 而接收端收 ...
- React组件生命周期-初始化阶段的函数执行顺序
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...
- 【查找结构4】红黑树 [RBT]
红黑树的性质与定义 红黑树(red-black tree) 是一棵满足下述性质的二叉查找树: 1. 每一个结点要么是红色,要么是黑色. 2. 根结点是黑色的. 3. 所有叶子结点都是黑色的(实际上都是 ...