您的位置:首页 > 其它

模板实现顺序表和循环双链表

2017-08-04 09:27 281 查看
1.模板实现顺序表

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<string>
using namespace std;
template<class T>
class SeqList
{
public:
SeqList()
:_a(NULL)
,_size(0)
,_capacity(0)
{
}

SeqList(const SeqList<T>& s)
{
_size=s._size ;
_capacity=_size;
_a=new T[_size];
for(size_t i=0;i<_size;i++)
{
_a[i]=s._a[i];
}

}

SeqList& operator=(SeqList<T> s)
{
swap(_a,s._a);
swap(_size,s._size);
swap(_capacity,s._size);
return *this;
}

~SeqList()
{
if(_a)
{
delete[] _a;
}
}

void CheckCapacity()
{
if(_size==_capacity)
{
size_t newsize=_capacity ? 2*_capacity : 3 ;
T* tmp=new T[newsize];//若为自定义类型,将其初始化
for(size_t i=0;i<_size;i++)
{
tmp[i]=_a[i];//string类会存在深浅拷贝问题,string                                       //类的operator=,可解决浅拷贝带来的问题
}
delete[] _a;
_a=tmp;
_capacity=newsize;
}
}

void PushBack(const T& x)
{
CheckCapacity();
_a[_size++]=x;
}

void PopBack()
{
if(_size>0)
{
_size--;

}
}

void Insert(size_t pos, const T& x)
{
CheckCapacity();

for(int end=(int)_size;end>=(int)pos;end--)
{
_a[end+1]=_a[end];
}
_a[pos]=x;
_size++;
}

void Erase(size_t pos)
{
for(size_t i=pos;i<_size;i++)
{
_a[i]=_a[i+1];
}
_size--;
}

T& operator[](size_t pos)
{
return _a[pos];
}

void Print()
{
for(size_t i=0;i<_size;i++)
{
cout<<_a[i]<<"  " ;
}
cout<<endl;
}

private:

T* _a;

size_t _size;

size_t _capacity;

};


2.模板实现循环双向链表

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<cassert>

using namespace std;
template<class T>
struct ListNode

{

T _data;
ListNode* _next;
ListNode* _prev;
ListNode(const T& x)
:_data(x)
,_next(NULL)
,_prev(NULL)
{
}

};

template<class T>

class List

{

typedef ListNode<T> Node;
public:
List()
{
_head=new Node(0);
_head->_next =_head;
_head->_prev=_head;
}
List(const List<T>& l)
{
Node* cur=l._head->_next;
_head=new Node(0);
_head->_next =_head;
_head->_prev=_head;
while(cur!=l._head)
{
PushBack(cur->_data);
cur=cur->_next ;
}

}
List& operator=( List<T> l)
{
swap(_head,l._head);
return *this;
}
~List()
{
Node* cur=_head->_next;
while(cur!=_head)
{
Node* del=cur;
delete del;
cur=cur->_next;
}
_head->_next=NULL;
_head->_prev=NULL;
delete _head;
}
public:
void PushBack(const T& x)
{
Insert(_head,x);
}
void PopBack()
{

Erase(_head->_prev);
}
void PushFront(const T& x)
{
Insert(_head->_next,x);
}
void PopFront()
{
Erase(_head->_next);
}
void Insert(Node* pos, const T& x)
{
assert(pos);
Node* prev=pos->_prev ;
Node* tmp=new Node(x);
tmp->_prev =prev;
prev->_next =tmp;
tmp->_next =pos;
pos->_prev =tmp;
}
void Erase(Node* pos)
{
assert(!Empty()&& pos);
Node* prev=pos->_prev ;
Node* next=pos->_next ;
delete pos;
prev->_next =next;
next->_prev =prev;

}
Node* Find(const T& x)
{
Node* cur=_head->_next ;
while(cur!=_head)
{
if(cur->_data==x)
{
return cur;
}
cur=cur->_next ;
}
return NULL;
}
void Print()
{
Node* cur=_head->_next;
while(cur!=_head)
{
cout<<cur->_data<<" ";
cur=cur->_next ;
}
cout<<endl;
}
T& Back()
{
assert(!Empty());
return _head->_next->_data;
}
bool Empty()
{
return _head==_head->_next;
}
private:
Node* _head;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: