顺序表
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <assert.h>
using namespace std;;
typedef int DataType;
class SeqList
{
friend ostream& operator <<(ostream& out, const SeqList& s);
public:
SeqList()
:_array(new DataType[3])
,_size(0)
,_capacity(3)
{}
SeqList(DataType arr[], size_t size)
:_array(new DataType[size])
,_size(size)
,_capacity(size)
{
memcpy(_array, arr, size*sizeof(DataType));
}
SeqList(const SeqList& s)
:_array(NULL)
,_size(0)
,_capacity(0)
{
SeqList tmp(s._array, s._capacity);
tmp._size = s._size;
swap(*this, tmp);
}
SeqList& operator =(SeqList s)
{
swap(*this, s);
return *this;
}
~SeqList()
{
if(NULL != _array)
{
delete[] _array;
_array = NULL;
_size = 0;
_capacity = 0;
}
}
void PushBack(DataType x)
{
assert(_array);
CheckCapacity();
_array[_size++] = x;
}
void PopBack()
{
assert(_array);
if(_size != 0)
_size--;
}
void PushFront(DataType x)
{
CheckCapacity();
size_t i = 0;
for(i=_size; i>0; --i)
{
_array[i] = _array[i-1];
}
_array[0] = x;
_size++;
}
void PopFront()
{
size_t i = 0;
for(i=0; i<_size; ++i)
{
_array[i] = _array[i+1];
}
_size--;
}
void Insert(size_t pos, DataType x)
{
assert((pos>=0) && (pos<=_size));
CheckCapacity();
size_t i = 0;
for(i=_size; i>pos; --i)
{
_array[i] = _array[i-1];
}
_array[pos] = x;
_size++;
}
void Erase(size_t pos)
{
assert((pos>=0) && (pos<=_size));
if(_size != 0)
{
size_t i = 0;
for(i=pos; i<_size-1; ++i)
{
_array[i] = _array[i+1];
}
_size--;
}
}
int Find(const DataType& data)
{
for(size_t i = 0; i<_size; ++i)
{
if(_array[i] == data)
return i;
}
return -1;
}
size_t Size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
bool Empty()const
{
return _size == 0;
}
void Clear()
{
if(NULL != _array)
{
delete[] _array;
_size = 0;
_capacity = 0;
}
}
void Resize(size_t size, const DataType data)
{
if(_size < size)
{
size_t tmp = _size;
for(size_t i=tmp; i<size; ++i)
{
CheckCapacity();
_array[i] = data;
_size++;
}
}
if(_size >size)
{
_size = size;
}
}
DataType& operator [](size_t pos)
{
return _array[pos];
}
const DataType& operator [](size_t pos)const
{
return _array[pos];
}
DataType& Front()
{
assert(_size > 0);
return _array[0];
}
const DataType& Front() const
{
assert(_size > 0);
return _array[0];
}
DataType& Back()
{
assert(_size > 0);
return _array[_size-1];
}
const DataType& Back() const
{
assert(_size > 0);
return _array[_size-1];
}
private:
void CheckCapacity()
{
if(_size >= _capacity)
{
DataType* newspace = new DataType[2*_capacity];
size_t i = 0;
for(i=0; i<_size; ++i)
{
newspace[i] = _array[i];
}
delete[] _array;
_array = newspace;
_capacity *= 2;
}
}
private:
DataType* _array;
size_t _size;
size_t _capacity;
};
ostream& operator <<(ostream& out, const SeqList& s)
{
size_t i = 0;
for(i=0; i<s._size; ++i)
{
out<<s._array[i]<<"->";
}
out<<"end";
return out;
}
void Test()
{
int arr[5] = {0};
SeqList s1;
SeqList s2(arr, 5);
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
cout<<s1<<endl;
s1.PopBack();
cout<<s1<<endl;
s1.PushFront(0);
cout<<s1<<endl;
s1.PopFront();
cout<<s1<<endl;
s1.Erase(1);
cout<<s1<<endl;
s1.Insert(1, 2);
cout<<s1<<endl;
cout<<s1.Find(2)<<endl;
cout<<s1.Size()<<endl;
cout<<s1.capacity()<<endl;
cout<<s1.Empty()<<endl;
s1.Resize(20, 1);
cout<<s1<<endl;
cout<<s1.Front()<<endl;
cout<<s1.Back()<<endl;
cout<<s1[0]<<"->"<<s1[1]<<endl;
}
int main()
{
Test();
system("pause");
return 0;
}
单链表
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <assert.h>
using namespace std;
typedef int DataType;
typedef struct ListNode
{
ListNode(const DataType x)
:_next(NULL)
,_data(x)
{}
ListNode* _next;
DataType _data;
}Node;
class List
{
public:
List()
:_head(NULL)
,_tail(NULL)
{}
List(List& s)
:_head(NULL)
,_tail(NULL)
{
if(s._head == NULL)
{
return;
}
else
{
_head = BuyNode(s._head->_data);
Node* NowNode = _head;
Node* ret = s._head;
while(ret != s._tail)
{
ret = ret->_next;
NowNode->_next = BuyNode(ret->_data);
NowNode = NowNode->_next;
}
}
}
List& operator =(List s)
{
swap(_head, s._head);
swap(_tail, s._tail);
}
~List()
{
Node* tmp = _head;
while(tmp != NULL)
{
Node* ret = tmp;
tmp = tmp->_next;
delete[] ret;
}
_head = _tail = NULL;
}
void PushBack(DataType data)
{
if(_head == NULL)
{
_head = _tail = BuyNode(data);
}
else
{
_tail->_next = BuyNode(data);
_tail = _tail->_next;
}
}
void PopBack()
{
if(_head == NULL)
{
return;
}
else if(_head->_next == NULL)
{
delete[] _head;
_head = _tail = NULL;
}
else
{
Node* tmp = _head;
while(tmp->_next->_next != NULL)
{
tmp = tmp->_next;
}
delete[] tmp->_next;
tmp->_next = NULL;
_tail = tmp;
}
}
void PushFront(DataType data)
{
if(_head == NULL)
{
_head = _tail = BuyNode(data);
}
else
{
Node* newhead = BuyNode(data);
newhead->_next = _head;
_head = newhead;
}
}
void PopFront()
{
if(_head == NULL)
{
return;
}
else if(_head->_next == NULL)
{
delete[] _head;
_head = _tail = NULL;
}
else
{
Node* tmp = _head;
_head = _head->_next;
delete[] tmp;
}
}
Node* Find(const DataType& data)
{
Node* tmp = _head;
while(tmp != NULL)
{
if(tmp->_data == data)
return tmp;
else
tmp = tmp->_next;
}
return NULL;
}
void Insert(Node* pos, DataType data)
{
assert(_head);
Node* newNode = BuyNode(pos->_data);
newNode->_next = pos->_next;
pos->_next = newNode;
pos->_data = data;
if(pos == _head)
{
_head = newNode;
}
}
void Erase(Node* pos)
{
assert(pos);
if(_head == NULL)
{
return;
}
else if(_head->_next == NULL)
{
delete[] _head;
_head = _tail = NULL;
}
else
{
Node* tmp = _head;
while(tmp->_next != pos)
{
tmp = tmp->_next;
}
tmp->_next = pos->_next;
delete[] pos;
}
}
void Print()
{
Node* tmp = _head;
while(tmp != NULL)
{
cout<<tmp->_data<<"->";
tmp = tmp->_next;
}
cout<<"NULL"<<endl;
}
private:
Node* BuyNode(const DataType& data)
{
return new Node(data);
}
private:
Node* _head;
Node* _tail;
};
void Test()
{
List s1;
s1.Print();
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.Print();
List s2(s1);
s2.Print();
s1.PopBack();
s1.Print();
s1.PushFront(0);
s1.Print();
s1.PopFront();
s1.Print();
Node* tmp = s1.Find(2);
s1.Erase(tmp);
s1.Print();
tmp = s1.Find(3);
s1.Insert(tmp, 2);
s1.Print();
}
int main()
{
Test();
return 0;
}