模板实现栈队列以及链表

模板实现链表

//test.h
#include <iostream>
#include <cstdio>
#include <assert.h>
using namespace std;template <class T>
struct ListNode
{ListNode* _prev;ListNode* _next;T _data;ListNode(const T& x):_prev(NULL),_next(NULL),_data(x){}
};template <class T>
class List
{typedef ListNode<T> Node;
public:List();~List();//l(l1)//l.List(this, l1)List(const List<T>& l);List<T>& operator = (const List<int>& l);void Clean();void Insert(Node* pos, const T& x);void Erase(Node* pos);void PushFront(T x);void PushBack(T x);void PopBack();void PopFront();void Print();
protected:Node* _head;
};//test.cc
#include "test.h"
template <class T>
List <T>::List()
{_head = new Node(T());_head -> _next = _head;_head -> _prev = _head;
}template <class T>
List <T>::~List()
{Clean();delete _head;
}//l(l1)
//l.List(this, l1)
template <class T>
List<T>::List(const List<T>& l)
{_head = new Node(T());_head -> _next = _head -> _prev = _head;Node* cur = l._head -> _next;while(cur != l._head){PushBack(cur -> _data);cur = cur -> _next;}
}template <class T>
List <T>& List <T>::operator = (const List<int>& l)
{_head = l._head;return *this;
}template <class T>
void List <T>::Clean()
{Node* cur = _head -> _next;while(cur != _head){Node* next = cur -> _next;delete cur;cur = next;}
}template <class T>
void List <T>::Insert(Node* pos, const T& x)
{assert(pos);Node* new_node = new Node(x);Node* prev = pos -> _prev;Node* next = pos;new_node -> _next = next;next -> _prev = new_node;prev -> _next = new_node;new_node -> _prev = prev;
}template <class T>
void List <T>::Erase(Node* pos)
{assert(pos != _head);Node* prev = pos -> _prev;Node* next = pos -> _next;prev -> _next = next;next -> _prev = prev;delete [] pos;
}template <class T>
void List <T>::PushFront(T x)
{Insert(_head -> _next, x);
}template <class T>
void List <T>::PushBack(T x)
{Insert(_head -> _prev -> _next, x);
}template <class T>
void List <T>::PopBack()
{Erase(_head -> _prev);
}template <class T>
void List <T>::PopFront()
{Erase(_head -> _next);
}template <class T>
void List <T>::Print()
{Node* cur = _head -> _next;while(cur != _head){cout << cur -> _data << " ";cur = cur -> _next;}cout << endl;
}

模板实现队列

//test.cc
#include "test.h"template <class T>
Vector<T>::Vector():_start(NULL),_finish(NULL),_endofstorage(NULL)
{
}template <class T>
//v1(v)
Vector <T>::Vector(const Vector <T>& v)
{size_t size = v.Size();T* start = new T[size];delete [] _start;_start = start;_finish = _start + size;_endofstorage = _start + v.Capacity;
}template <class T>
Vector <T>& Vector <T>::operator = (const Vector <T>& v)
{swap(_start, v._start);swap(_finish, v._start);swap(_endofstorage, v._endofstorage);return *this;
}template <class T>
void Vector <T>::Expand(size_t new_capacity)
{if(new_capacity > Capacity()){size_t size = Size();T* tmp = new  T[new_capacity];if(_start){for(int i = 0; i < (int)size; i++){tmp[i] = _start[i]; }delete [] _start;}_start = tmp;_finish = _start + size;_endofstorage = _start + new_capacity;}
}template <class T>
void Vector <T>::Reserve(size_t size)
{Expand(size);
}template <class T>
const T& Vector <T>::operator [] (size_t pos)const
{return _start[pos];
}
template <class T>
size_t Vector <T>:: Size()
{return _finish - _start;
}template <class T>
void Vector <T>::Insert(size_t pos, const T& x)
{assert(pos <= Size());if(_finish == _endofstorage){size_t new_capacity = Capacity() == 0 ? 3 : Capacity() * 2;Expand(new_capacity);}T* end = _finish - 1;while(end >= _start + pos){*(end + 1) = *end; --end;}*end = x;++_finish;
}template <class T>
void Vector <T>::PushFront(const T& x)
{if(Size() == Capacity()){size_t new_capacity = Capacity() == 0 ? 3 : 2 * Capacity();Expand(new_capacity);}T* end = _finish;for(; end >= _start; --end){*(end + 1) = *(end);}*_start = x;++_finish;
}
template <class T>
void Vector <T>::PushBack(const T& x)
{if(_finish == _endofstorage){size_t new_capacity = Capacity() == 0 ? 3 : 2 * Capacity();Expand(new_capacity);}*_finish = x;++_finish;
}template <class T>
void Vector <T>::Erase(size_t pos)
{assert(pos < Size());T* start = _start + pos;while(start < _finish)// TODO{*start = *(start + 1);start++;}--_finish;
}template <class T>
void Vector <T>::ReSize(size_t size, const T& value)
{Expand(size);memset(_start, value, sizeof(T) * size);
}template <class T>
size_t Vector <T>::Capacity()
{return _endofstorage - _start;
}template <class T>
Vector <T> ::~Vector()
{if(_start){delete [] _start;}
}template <class T>
bool Vector <T> ::Empty()
{return _start == _finish;
}template <class T>
void Vector<T>::PopBack()
{Erase(Size() - 1);
}template <class T>
void Vector <T>::PopFront()
{Erase(0);
}template <class T, class Container>
void Queue <T, Container>::Push(const T& x)
{_con.PushBack(x);
}template <class T, class Container>
void Queue<T, Container>::Pop()
{_con.PopFront();
}template <class T>
T& Vector<T>::Front()
{if(_start != NULL){return *_start;}
}template <class T, class Container>
T& Queue <T, Container>::Front()
{return _con.Front();
}template <class T, class Container>
bool Queue <T, Container>::Empty()
{return _con.Empty();
}
//test.h
#include <iostream>
#include <cstdio>
#include <assert.h>
#include <string.h>
using namespace std;#define TESTHEADER printf("\n==============%s===========\n", __FUNCTION__)template <class T>
class Vector
{
public:Vector();~Vector();Vector(const Vector <T>& v);Vector& operator = (const Vector <T>& v);void Reserve(size_t size);void ReSize(size_t size, const T& value = T());const T& operator [] (size_t pos) const;void Insert(size_t pos, const T& x);void PushFront(const T& x);void PushBack(const T& x);void Erase(size_t pos);size_t Capacity();bool Empty();size_t Size();void PopBack();void PopFront();T& Front();
protected:void Expand(size_t size);
protected:T* _start;T* _finish;T* _endofstorage;
};template <class T, class Container>
class Queue
{
public:void Push(const T& x);void Pop();T& Front();bool Empty();
protected:Container _con;
};

模板实现栈

//test.h
#include <iostream>
#include <cstdio>
#include <assert.h>
#include <string.h>
using namespace std;#define TESTHEADER printf("\n==================%s==================\n", __FUNCTION__)
template <class T>
class Vector
{
public:Vector();~Vector();Vector(const Vector <T>& v);Vector& operator = (const Vector <T>& v);void Reserve(size_t size);void ReSize(size_t size, const T& value = T());const T& operator [] (size_t pos) const;void Insert(size_t pos, const T& x);void PushFront(const T& x);void PushBack(const T& x);void Erase(size_t pos);size_t Capacity();bool Empty();size_t Size();void PopBack();void PopFront();const T& Back();
protected:void Expand(size_t size);
protected:T* _start;T* _finish;T* _endofstorage;
};template<class T, class Container>
class Stack
{
public:void Push(const T& x);void Pop();const T& Top();bool Empty();
protected:Container _con;
};
//test.cc
#include "test.h" 
template <class T>
Vector<T>::Vector():_start(NULL),_finish(NULL),_endofstorage(NULL)
{
}template <class T>
//v1(v)
Vector <T>::Vector(const Vector <T>& v)
{size_t size = v.Size();T* start = new T[size];delete [] _start;_start = start;_finish = _start + size;_endofstorage = _start + v.Capacity;
}template <class T>
Vector <T>& Vector <T>::operator = (const Vector <T>& v)
{swap(_start, v._start);swap(_finish, v._start);swap(_endofstorage, v._endofstorage);return *this;
}template <class T>
void Vector <T>::Expand(size_t new_capacity)
{if(new_capacity > Capacity()){size_t size = Size();T* tmp = new  T[new_capacity];if(_start){for(int i = 0; i < (int)size; i++){tmp[i] = _start[i]; }delete [] _start;}_start = tmp;_finish = _start + size;_endofstorage = _start + new_capacity;}
}template <class T>
void Vector <T>::Reserve(size_t size)
{Expand(size);
}template <class T>
const T& Vector <T>::operator [] (size_t pos)const
{return _start[pos];
}
template <class T>
size_t Vector <T>:: Size()
{return _finish - _start;
}template <class T>
void Vector <T>::Insert(size_t pos, const T& x)
{assert(pos <= Size());if(_finish == _endofstorage){size_t new_capacity = Capacity() == 0 ? 3 : Capacity() * 2;Expand(new_capacity);}T* end = _finish - 1;while(end >= _start + pos){*(end + 1) = *end; --end;}*end = x;++_finish;
}template <class T>
void Vector <T>::PushFront(const T& x)
{if(Size() == Capacity()){size_t new_capacity = Capacity() == 0 ? 3 : 2 * Capacity();Expand(new_capacity);}T* end = _finish;for(; end >= _start; --end){*(end + 1) = *(end);}*_start = x;++_finish;
}
template <class T>
void Vector <T>::PushBack(const T& x)
{if(_finish == _endofstorage){size_t new_capacity = Capacity() == 0 ? 3 : 2 * Capacity();Expand(new_capacity);}*_finish = x;++_finish;
}template <class T>
const T& Vector<T>::Back()
{return _start[Size() - 1];
}template <class T>
void Vector <T>::Erase(size_t pos)
{assert(pos < Size());T* start = _start + pos;while(start < _finish)// TODO{*start = *(start + 1);start++;}--_finish;
}template <class T>
void Vector <T>::ReSize(size_t size, const T& value)
{Expand(size);memset(_start, value, sizeof(T) * size);
}template <class T>
size_t Vector <T>::Capacity()
{return _endofstorage - _start;
}template <class T>
Vector <T> ::~Vector()
{if(_start){delete [] _start;}
}template <class T>
bool Vector <T> ::Empty()
{return _start == _finish;
}template <class T>
void Vector<T>::PopBack()
{Erase(Size() - 1);
}template <class T>
void Vector <T>::PopFront()
{Erase(0);
}///////////////////////////////////////////////////////////////////////////////////////////////
//模板参数实现容器适配器
///////////////////////////////////////////////////////////////////////////////////////////////template<class T, class Container>
void Stack<T, Container>::Push(const T& x)
{_con.PushFront(x);
}template<class T, class Container>
void Stack<T, Container>::Pop()
{_con.PopBack();
}template<class T, class Container>
bool Stack<T, Container>::Empty()
{_con.Empty();
}template<class T, class Container>
const T& Stack<T, Container>::Top()
{return _con.Back();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/383891.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于Visual C++2013拆解世界五百强面试题--题5-自己实现strstr

http://blog.csdn.net/itcastcpp/article/details/12907371 请用C语言实现字符串的查找函数strstr&#xff0c; 找到则返回子字符串的地址&#xff0c;没有找到返回为空&#xff0c;请用数组操作与指针操作实现 看到题目想到最简单的方法就是母字符串和子字符串比较&#xff0c…

卡特兰数

卡特兰数的引入与n边形分成三角形的个数有关&#xff1a; 我们令f[n]表示n边形可以分成的三角形的个数&#xff0c;特殊的&#xff0c;令f[2]1 我们考虑以顶点1顶点的一个三角形&#xff0c;假设用的是n边形的k-k1边&#xff0c;那么这种情况的方案数就是f[k]∗f[n−k1]f[k]*…

软件测试相关概念

什么叫软件测试 软件测试就是测试产品没有错误,同时又证明软件是可以正确运行的 测试和调试的区别 调试一般都在开发期间 ,测试是伴随着整个软件的生命周期, 调试是发现程序中问题并且解决问题, 测试是发现程序中的缺陷 软件测试的目的和原则 目的:验证软件有没有问题 原…

Linux 线程信号量同步

https://www.cnblogs.com/jiqingwu/p/linux_semaphore_example.html 信号量和互斥锁(mutex)的区别&#xff1a;互斥锁只允许一个线程进入临界区&#xff0c;而信号量允许多个线程同时进入临界区。 不多做解释&#xff0c;要使用信号量同步&#xff0c;需要包含头文件semaphore.…

本原勾股数组

勾股数我们都很熟悉&#xff0c;a2b2c2&#xff0c;可是如何快速找到所有的勾股数组呢&#xff1f; 本原勾股数组a2b2c2性质&#xff1a; 1. a,b奇偶不同&#xff0c;c一定是奇数 2. 若b为偶数&#xff0c;c-b和cb一定是完全平方数 3. 设t>s>1,且均为奇数&#xff0c;则…

C++静态成员函数访问非静态成员的几种方法

https://www.cnblogs.com/rickyk/p/4238380.html 大家都知道C中类的成员函数默认都提供了this指针&#xff0c;在非静态成员函数中当你调用函数的时候&#xff0c;编译器都会“自动”帮你把这个this指针加到函数形参里去。当然在C灵活性下面&#xff0c;类还具备了静态成员和静…

费马大定理

当n>3时方程 xnynzn没有正整数解 结论很简洁&#xff0c;刚才看了一下证明的历史&#xff0c;我勒个去。。。。

类中的静态成员函数访问非静态成员变量

http://blog.csdn.net/u011857683/article/details/52294353 1.思路&#xff1a; 静态成员函数属于类(通过类访问&#xff0c;调用函数时没有提供this指针)&#xff0c; 非静态成员函数属于实例&#xff08;通过对象访问&#xff09;&#xff08;默认都提供了this指针&#xf…

Pollar Rho算法

原本是想把这个算法搞懂的&#xff0c;然后在网上看了又看&#xff0c;觉得&#xff0c;还是有时间再来看吧&#xff0c;我错了。 看到了一个大佬的博客&#xff0c;顺带收集一下板子 这个板子可以求大数的最大的因子。 #define LL long long bool IsPrime(LL);//返回素性测…

小知识点总结

用户输入一个url之后到整个页面返回给客户这个过程都经历了一些什么 首先url是为了让人记忆方便的,计算机在进行网络传输的过程中只能通过ip地址找到对应的主机,所以当输入一个ip地址的时候,此时就需要找对应的url,首先从浏览器中取查找ip地址,再到系统中去查找,再到域名解析服…

C++学习之普通函数指针与成员函数指针

http://blog.csdn.net/lisonglisonglisong/article/details/38353863 函数指针&#xff08;function pointer&#xff09;是通过指向函数的指针间接调用函数。相信很多人对指向一般函数的函数指针使用的比较多&#xff0c;而对指向类成员函数的函数指针则比较陌生。我最近也被问…

HDU2683——欧拉完全数

题目要求符合等式的数&#xff0c;我们首先要做的就是分析这个数&#xff1a; 对于这个等式&#xff0c;我们可能什么都看不出来&#xff0c;左边很难化简的样子&#xff0c;所以我们就要想到通过变化怎么样把右边化成和左边形式差不多的样子。结合组合数我们想到二项式定理&am…

BZOJ-2005能量采集-数论函数

很入门的数论函数题目。我还是wa了一发&#xff08;爆long long 了&#xff09; 对于每个位置x,y&#xff0c;在他们和能量采集器中间的植物为gcd(x,y)-1&#xff0c;【在他们之间说明斜率相同&#xff0c;而和他们斜率相同的就是所有gcd(x/gcd(x,y),y/gcd(x,y))1的并且比他们小…

网络五层模型

TCP/IP五层模型 应用层: HTTP,HTTPS协议,其中HTTP没有对数据进行加密操作,但是HTTPS对数据进行了加密操作 其中HTTP端口号一般是80/8080等等,HTTPS端口号是443,SSH端口号一般是22,ftp是21 HTTP协议报头: 首行:请求方法,url,协议版本 请求报头: HOST:主机 Connection:长连接…

C++的静态成员函数指针

http://blog.csdn.net/sky453589103/article/details/47276789 先简单的说说非静态的成员函数。非静态成员函数指针的类型&#xff1a;类的非静态成员是和类的对象相关的。也就是说&#xff0c;要通过类的对象来访问变量。成员函数的类型定义为&#xff1a;typedef void (A::*p…

从一个字符串中删除另一个字符串中出现过的字符

http://blog.csdn.net/walkerkalr/article/details/39001155 定义一个函数&#xff0c;输入两个字符串&#xff0c;从第一个字符串中删除在第二个中出现过的所偶字符串。例如从第一个字符串"We are students."中删除第二个字符中“auiou”中出现过的字符得到的结果是…

SPOJ-VLATTICE Visible Lattice Points-莫比乌斯反演

需要将问题分解一下。 我们需要求的是这个立方体从(0,0,0)能看到的点的个数。可是在三个含有(0,0,0)的面上我们没有办法和其他的一起进行分析&#xff08;因为含有坐标是0&#xff0c;而我们的数论工具都是从1开始的&#xff09;&#xff0c;所以我们可以将那三个面分开考虑&a…

进程的通信

管道 什么是管道 由于进程之间是相互独立的,因此在进程与进程之间进行相互联系的时候,此时就需要采用一种机制,通过管道的方式将进程一个进程的执行数据交给另外一个进程,此时就可以以通过管道来实现 匿名管道和命名管道 匿名管道 创建一个匿名管道的方式是pipe(int pipe[…

获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf

http://blog.csdn.net/windeal3203/article/details/39320605 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq 结构体struct ifconf来获取网络接口的各种信息。 ioctl 首先看ioctl()用法ioctl()原型如下&#xff1a;#include <sys/ioctl.h>int ioctl(int fd, i…

欧拉降幂

我们记f(n)为n的欧拉函数值&#xff0c;则 当B>f©时&#xff0c;AB%CAB%f©f©%C&#xff0c;这里A,C可能不互质。 很好用&#xff0c;证明很复杂&#xff0c;等有时间回来学习一下。