C++实现顺序栈:
//顺序栈
#include <iostream>
using namespace std;
#define MAXSIZE 10
struct __TrueType
{
bool Get()
{
return true;
}
};
struct __FalseType
{
bool Get()
{
return false;
}
};
template <class _T>
struct TypeTraits
{
typedef __FalseType __IsPODType;
};
template <>
struct TypeTraits< bool>
{
typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< char>
{
typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< short>
{
typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< int>
{
typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< long>
{
typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< unsigned long long>
{
typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< float>
{
typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< double>
{
typedef __TrueType __IsPODType;
};
template<class T>
class Stack
{
public:
Stack()
: _pData(new T[MAXSIZE])
, _size(0)
, _capacity(MAXSIZE)
{}
Stack(const Stack<T>& s)
: _pData(new T[s._capacity])
, _size(s._size)
, _capacity(s._capacity)
{
for(size_t i=0; i<_size; i++)
{
_pData[i] = s._pData[i];
}
}
Stack<T>& operator=(const Stack<T>& s)
{
if(this != &s)
{
_pData = new T[s._capacity];
_size = s._size;
_capacity = s._capacity;
for(size_t i=0; i<_size; i++)
{
s._pData[i] = _pData[i];
}
}
return *this;
}
void Push(const T& data)
{
if(_size == _capacity)
{
CheckCapacity();
}
_pData[_size++] = data;
}
void Pop()
{
if(Empty())
{
cout<<"栈已空!"<<endl;
return;
}
--_size;
}
bool Empty()const
{
return _size==0;
}
T& Top()
{
return _pData[_size-1];
}
size_t Size()const
{
return _size;
}
~Stack()
{
delete[] _pData;
}
void PrintStack()
{
for(size_t i=0; i<_size; i++)
{
cout<<_pData[i]<<" ";
}
cout<<endl;
}
private:
void CheckCapacity()
{
// 申请新空间
_capacity += 3;
T* tmp = new T[_capacity];
// 拷贝元素到新空间
//memcpy() 优点:快 缺点:浅拷贝
//for() 优点:避免了浅拷贝 缺点:速度慢
if(TypeTraits<T>::__IsPODType.get())// 判断返回值
{
memcpy(tmp, _pData, _capacity*sizeof(T));
}
else
{
for(size_t i=0; i<_size; i++)
{
tmp[i] = _pData[i];
}
}
// 释放旧空间
delete[] _pData;
_pData = tmp;
}
private:
T* _pData;
size_t _size;
size_t _capacity;
};
C++实现循环队列:
//循环队列
#include <iostream>
using namespace std;
template<typename T>
class SqQueue
{
public:
SqQueue(size_t size = 10)
: _front(0)
, _rear(0)
, _size(size+1)
{
_pData = new T[_size];
}
size_t GetLength()
{
return _size-1;
}
~SqQueue()
{
delete[] _pData;
}
size_t GetFront()
{
if(IsEmpty())
{
cout<<"队列为空!";
}
return _pData[_front];
}
size_t GetData(size_t i)
{
if(IsEmpty())
{
cout<<"队列为空!";
}
_front = (_front+i)%_size;
return _pData[_front];
}
bool IsEmpty()
{
if(_front == _rear)
return true;
return false;
}
bool IsFull()
{
if((_rear+1)%_size == _front)
return true;
return false;
}
void EnQueue(const T& value)
{
if(IsFull())
{
cout<<"队列为满!";
}
_pData[_rear] = value;
_rear = (_rear+1)%_size;
}
T DeQueue()
{
if(IsEmpty())
{
cout<<"队列为空!";
}
T value = _pData[_front];
_front = (_front+1)%_size;
return value;
}
private:
T* _pData;
size_t _front;
size_t _rear;
size_t _size;
};