test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
//#include"Seqlist.h"
#include"List.h"
int main()
{
/*test1();*/
//testStackt1();
//testSeqList1();
TestQueue();
system("pause");
return 0;
}
SeqList.h
//#define _CRT_SECURE_NO_WARNINGS 1
//template<typename T>
//class SeqList
//{
//public:
// SeqList()
// :_data(NULL)
// , _size(0)
// , _capacity(0)
// {}
// ~SeqList()
// {
// Distory();
// }
// SeqList(const SeqList<T> &s)
// :_data(new T[s._size])
// , _size(s._size)
// , _capacity(s._size)
// {
// for (size_t i = 0; i < _size; i++)
// {
// _data[i] = s._data[i];
// }
// }
// SeqList<T>&operator=(const SeqList<T>&s)
// {
// if (this != &s)
// {
// T*tmp = new T[s._size];
// for (size_t i = 0; i < s._size; i++)
// {
// tmp[i] = s._data[i];
// }
// delete[]_data;
// _data = tmp;
// _size = s._size;
// _capacity = s._size;
// }
// return *this;
// }
// void PushBack(const T& x)
// {
// _CheckCapacity();
// _data[_size] = x;
// _size++;
// }
// void PopBack()
// {
// if (_size > 0)
// {
// --_size;
// }
// }
// void PushFront(const T&x)
// {
//_CheckCapacity();
// for (size_t i = _size; i > 0; i--)
// {
// _data[i] = _data[i - 1];
// }
// _data[0] = x;
// _size++;
// }
// void PopFront()
// {
// if (_size > 0)
// {
// for (size_t i = 0; i < _size - 1; i++)
// {
// _data[i] = _data[i + 1];
// }
// _size--;
// }
// }
// T& Back()
// {
// assert(_size >0);
// return _data[_size - 1];
// }
// size_t Size()
// {
// return _size;
// }
// bool Empty()
// {
// return _size == 0;
// }
//
// void Print()
// {
// if (_size ==0)
// {
// cout << "顺序表为空";
// }
// for (size_t i = 0; i < _size; i++)
// {
// cout << _data[i] << " ";
// }
// cout << endl;
// }
//
//protected:
// void _CheckCapacity()
// {
// if (_size >= _capacity)
// {
// T*tmp = new T[_capacity * 2 + 3];
// for (size_t i = 0; i < _size; i++)
// {
// tmp[i] = _data[i];
// }
// delete[]_data;
// _data = tmp;
// _capacity = _capacity * 2 + 3;
//
// }
// }
// void Distory()
// {
// if (_data)
// {
// delete[]_data;
// _data = NULL;
// _size = 0;
// _capacity = 0;
// }
// }
//protected:
// T*_data;
// size_t _size;
// size_t _capacity;
//};
//void test1()
//{
// SeqList <int>L;
// L.PushBack(1);
// L.PushBack(1);
// L.PushBack(2);
// L.PushFront(5);
// SeqList<int>L1;
// L1 = L;
// L.PopFront();
// L.PopBack();
// L.Print();
// L1.Print();
//}
////用适配器实现一个栈
////template<class T,class Container=SeqList<T>>
////class Stack
////{
////public:
//// void Push(const T& x)
//// {
//// _con.PushBack(x);
//// }
//// void Pop()
//// {
//// _con.PopBack();
//// }
//// const T&Top()
//// {
//// return _con.Back();
//// }
//// size_t Size()
//// {
//// return _con.Size();
//// }
//// bool Empty()
//// {
//// return _con.Empty();
//// }
////protected:
//// Container _con;
////};
////void testStack()
////{
//// Stack<int> S;
//// S.Push(1);
//// S.Push(2);
//// S.Push(3);
//// while (!S.Empty())
//// {
//// cout << S.Top() << " ";
//// S.Pop();
//// }
//// cout << endl;
////}
////模板的模板参数
//template<class T,template<class> class Container=SeqList>
//class Stackt
//{
//public:
// void Push(const T& x)
// {
// _con.PushBack(x);
// }
// void Pop()
// {
// _con.PopBack();
// }
// const T& Top()
// {
// return _con.Back();
// }
// size_t Size()
// {
// return _con.Size()
// }
// bool Empty()
// {
// return _con.Empty();
// }
//private:
// Container <T> _con;
//};
//void testStackt1()
//{
// Stackt<int>S;
// S.Push(1);#pragma once
template<class T>
struct ListNode
{
ListNode(const T&x)
:_data(x)
, _prev(NULL)
, _next(NULL)
{}
ListNode<T>*_prev;
ListNode<T>*_next;
T _data;
};
template<class T>
class List
{
typedef ListNode<T> Node;
public:
List()
:_head(NULL)
, _tail(NULL)
{}
List(const List<T>&L)
:_head(NULL)
, _tail(NULL)
{
Node* cur = L._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
List<T>& operator=(List<T> L)
{
swap(_head,L._head);
swap(_tail,L._tail);
return *this;
}
~List()
{
Node*cur = _head;
while (cur)
{
Node*tmp = cur->_next;
delete cur;
cur = tmp;
}
_head = _tail = NULL;
}
void PushBack(const T&x)
{
Node*tmp = new Node(x);
if (_head == NULL)
{
_head = _tail = tmp;
}
else
{
_tail->_next = tmp;
tmp->_prev = _tail;
_tail = tmp;
}
}
void PopBack()
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node*tmp = _tail->_prev;
delete _tail;
_tail = tmp;
_tail->_next = NULL;
}
}
void PushFront(const T&x)
{
Node*tmp = new Node(x);
if (_head == NULL)
{
_head = _tail = tmp;
}
else
{
tmp->_next = _head;
_head->_prev = tmp;
_head = tmp;
}
}
void PopFront()
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node*tmp = _head->_next;
delete _head;
_head = tmp;
}
}
void Print()
{
Node*cur = _head;
while (cur)
{
cout << cur->_data << " ";
cur = cur->_next;
}
cout << endl;
}
T& Front()
{
assert(_head);
return _head->_data;
}
T& Back()
{
assert(_tail);
return _tail->_data;
}
size_t Size()
{
size_t count = 0;
Node* cur = _head;
while (cur)
{
count++;
cur = cur->_next;
}
return count;
}
bool Empty()
{
return _head == NULL;
}
protected:
Node* _head;
Node* _tail;
};
void test1()
{
List<int> L;
L.PushBack(1);
L.PushFront(2);
L.PopFront();
L.PopBack();
L.Print();
List<int> L1(L);
L1.Print();
}
//适配器模拟实现一个队列
template<class T,class Container=List<T>>
class Queue
{
public:
void Push(const T& x)
{
_con.PushBack(x);
}
void Pop()
{
_con.PopFront();
}
T& Front()
{
return _con.Front();
}
T& Back()
{
return _con.Back();
}
size_t Size()
{
return _con.Size();
}
bool Empty()
{
return _con.Empty();
}
protected:
Container _con;
};
void TestQueue()
{
Queue<int>q;
q.Push(2);
q.Push(3);
q.Push(4);
while (!q.Empty())
{
cout << q.Front() << " ";
q.Pop();
}
cout << endl;
}
// S.Push(2);// while (!S.Empty())// {// cout << S.Top() << " ";// S.Pop();// }// cout << endl;//}////非类型的类模板参数//const size_t N = 20;//template<class T,size_t N=20>//class SeqList1//{//protected:// T _a[N];// size_t _size;//};//void testSeqList1()//{// SeqList1<int> s1;// SeqList1<double, 2000>s2;//}
List.h