#pragma once
typedef int DataType;
class SeqList
{
public:
SeqList()
:_a(NULL)
,_size(0)
,_capacity(0)
{}
class SeqList
{
public:
SeqList()
:_a(NULL)
,_size(0)
,_capacity(0)
{}
// 拷贝构造
SeqList(const SeqList& s)
:_a(new DataType[s._size])
,_size(s._size)
,_capacity(s._size)
{
memcpy(_a, s._a, sizeof(DataType)*s._size);
}
SeqList(const SeqList& s)
:_a(new DataType[s._size])
,_size(s._size)
,_capacity(s._size)
{
memcpy(_a, s._a, sizeof(DataType)*s._size);
}
SeqList& operator=(SeqList s)
{
swap(_a, s._a);
swap(_size, s._size);
swap(_capacity, s._capacity);
{
swap(_a, s._a);
swap(_size, s._size);
swap(_capacity, s._capacity);
return *this;
}
}
void PushBack(const DataType& x)
{
CheckCapacity();
_a[_size++] = x;
}
{
CheckCapacity();
_a[_size++] = x;
}
void PopBack()
{
assert(_size > 0);
{
assert(_size > 0);
--_size;
}
void Insert(size_t pos, const DataType& x)
{
assert(pos >= 0 && pos<_size);
CheckCapacity();
size_t i = 0;
for (i = _size; i>pos; i--)
{
_pArr[i] = _pArr[i - 1];
}
_pArr[pos] = d;
_size++;
}
void Erase(size_t pos)
{
assert(pos >= 0 && pos<_size);
if (_size == 0)
return;
else
{
size_t i = 0;
for (i = pos; i<_size - 1; i++)
{
_pArr[i] = _pArr[i + 1];
}
_size--;
}
}
size_t Find(const DataType& x)
{
for (size_t i = 0 ; i < _size; ++i)
{
if (x == _a[i])
{
return i;
}
}
}
void Insert(size_t pos, const DataType& x)
{
assert(pos >= 0 && pos<_size);
CheckCapacity();
size_t i = 0;
for (i = _size; i>pos; i--)
{
_pArr[i] = _pArr[i - 1];
}
_pArr[pos] = d;
_size++;
}
void Erase(size_t pos)
{
assert(pos >= 0 && pos<_size);
if (_size == 0)
return;
else
{
size_t i = 0;
for (i = pos; i<_size - 1; i++)
{
_pArr[i] = _pArr[i + 1];
}
_size--;
}
}
size_t Find(const DataType& x)
{
for (size_t i = 0 ; i < _size; ++i)
{
if (x == _a[i])
{
return i;
}
}
return nPos;
}
}
DataType& operator[](size_t pos)
{
assert(pos < _size);
{
assert(pos < _size);
return _a[pos];
}
}
~SeqList()
{
if(_a)
{
delete[] _a;
_size = _capacity = 0;
}
}
{
if(_a)
{
delete[] _a;
_size = _capacity = 0;
}
}
void CheckCapacity()
{
if (_size >= _capacity)
{
_capacity = _capacity > 0 ? _capacity*2 : 3;
DataType* tmp = new DataType[_capacity];
memcpy(tmp, _a, sizeof(DataType)*_size);
delete[] _a;
_a = tmp;
}
}
{
if (_size >= _capacity)
{
_capacity = _capacity > 0 ? _capacity*2 : 3;
DataType* tmp = new DataType[_capacity];
memcpy(tmp, _a, sizeof(DataType)*_size);
delete[] _a;
_a = tmp;
}
}
void Print()
{
for (size_t i = 0; i < _size; ++i)
{
cout<<_a[i]<<" ";
}
cout<<endl;
}
{
for (size_t i = 0; i < _size; ++i)
{
cout<<_a[i]<<" ";
}
cout<<endl;
}
static size_t nPos;
private:
DataType* _a;
size_t _size;
size_t _capacity;
};
DataType* _a;
size_t _size;
size_t _capacity;
};
size_t SeqList::nPos = -1;
void TestSeqList()
{
SeqList s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.Print();
{
SeqList s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.Print();
SeqList s2(s1);
s2.Print();
s2.Print();
SeqList s3;
s3.PushBack(10);
s3.PushBack(10);
s3.Insert(2,30);
s3.Print();
s3.PushBack(10);
s3.PushBack(10);
s3.Insert(2,30);
s3.Print();
s3.Erase(3);
s3.Print();
s3.Print();
s1 = s3;
s1.Print();
s3.Print();
}
#pragma once
typedef int DataType;
struct SListNode
{
SListNode* _next;
DataType _data;
{
SListNode* _next;
DataType _data;
SListNode(const DataType& x)
:_data(x)
,_next(NULL)
{}
};
:_data(x)
,_next(NULL)
{}
};
class SList
{
typedef SListNode Node;
public:
SList()
:_head(NULL)
,_tail(NULL)
{}
{
typedef SListNode Node;
public:
SList()
:_head(NULL)
,_tail(NULL)
{}
SList(const SList& l)
:_head(NULL)
,_tail(NULL)
{
Copy(l._head);
}
:_head(NULL)
,_tail(NULL)
{
Copy(l._head);
}
SList& operator=(SList l)
{
swap(_head, l._head);
swap(_tail, l._tail);
{
swap(_head, l._head);
swap(_tail, l._tail);
return *this;
}
}
void Clear()
{
Node* cur = _head;
while (cur)
{
Node* next = cur->_next;
delete cur;
cur = next;
}
{
Node* cur = _head;
while (cur)
{
Node* next = cur->_next;
delete cur;
cur = next;
}
_head = _tail = NULL;
}
}
void Copy(Node* head)
{
Node* cur = head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
{
Node* cur = head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
~SList()
{
Clear();
}
{
Clear();
}
void PushBack(const DataType& x)
{
if (_tail == NULL)
{
_head = _tail = new Node(x);
}
else
{
_tail->_next = new Node(x);
_tail = _tail->_next;
}
}
{
if (_tail == NULL)
{
_head = _tail = new Node(x);
}
else
{
_tail->_next = new Node(x);
_tail = _tail->_next;
}
}
void PopBack()
{
if (_head == NULL) //没有节点
{
return;
}
else if (_head == _tail) //有一个节点
{
delete _head;
_head = NULL;
_tail = NULL;
}
else //有多个节点
{
Node *prev = _head;
while (prev->_next != _tail)
{
prev = prev->_next;
}
prev->_next = _head;
delete _tail;
_tail = prev;
}
{
if (_head == NULL) //没有节点
{
return;
}
else if (_head == _tail) //有一个节点
{
delete _head;
_head = NULL;
_tail = NULL;
}
else //有多个节点
{
Node *prev = _head;
while (prev->_next != _tail)
{
prev = prev->_next;
}
prev->_next = _head;
delete _tail;
_tail = prev;
}
}
void PushFront(const DataType& x)
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
_head->_next = new Node(x);
_tail = _tail->_next;
}
}
void PopFront()
{
if (_head == NULL) //没有节点
{
return;
}
else if (_head == _tail) //有一个节点
{
delete _head;
_head = NULL;
_tail = NULL;
}
else
{
Node* cur = _head;
_head = _head->_next;
delete cur;
_tail->_next = _head;
}
void PushFront(const DataType& x)
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
_head->_next = new Node(x);
_tail = _tail->_next;
}
}
void PopFront()
{
if (_head == NULL) //没有节点
{
return;
}
else if (_head == _tail) //有一个节点
{
delete _head;
_head = NULL;
_tail = NULL;
}
else
{
Node* cur = _head;
_head = _head->_next;
delete cur;
_tail->_next = _head;
}
}
void Insert(Node* pos, const DataType& x)
{
Node* tmp = new Node(x);
if (_head == pos)
{
if (_head == NULL)
{
_head = _tail = tmp;
}
else
{
Node* prev = NULL;
Node* next = NULL;
tmp->next = _head;
_head->prev = tmp;
_head = tmp;
}
}
}
void Erase(Node* pos)
{
if (_head == pos)
{
PopFront();
}
else if (_tail == pos)
{
PopBack();
}
else
{
Node* prev = NULL;
Node* next = NULL;
Node* tmp = NULL;
tmp = pos->next;
delete pos;
pos->next->prev = tmp;
}
}
Node* Find(const DataType& x)
{
if (_Head == NULL) //没有节点
{
return NULL;
}
LinkNode* begin = _Head;
do{
if (begin->_data == x)
{
cout << "find " << x << endl;
return begin;
}
begin = begin->_next;
} while (begin != _Head);
return NULL;
void Insert(Node* pos, const DataType& x)
{
Node* tmp = new Node(x);
if (_head == pos)
{
if (_head == NULL)
{
_head = _tail = tmp;
}
else
{
Node* prev = NULL;
Node* next = NULL;
tmp->next = _head;
_head->prev = tmp;
_head = tmp;
}
}
}
void Erase(Node* pos)
{
if (_head == pos)
{
PopFront();
}
else if (_tail == pos)
{
PopBack();
}
else
{
Node* prev = NULL;
Node* next = NULL;
Node* tmp = NULL;
tmp = pos->next;
delete pos;
pos->next->prev = tmp;
}
}
Node* Find(const DataType& x)
{
if (_Head == NULL) //没有节点
{
return NULL;
}
LinkNode* begin = _Head;
do{
if (begin->_data == x)
{
cout << "find " << x << endl;
return begin;
}
begin = begin->_next;
} while (begin != _Head);
return NULL;
}
void Print()
{
Node* cur = _head;
while (cur)
{
cout<<cur->_data<<" ";
cur = cur->_next;
}
cout<<endl;
}
{
Node* cur = _head;
while (cur)
{
cout<<cur->_data<<" ";
cur = cur->_next;
}
cout<<endl;
}
private:
Node* _head;
Node* _tail;
};
Node* _head;
Node* _tail;
};
void TestSList()
{
SList l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();
{
SList l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();
SList l2(l1);
l2.Print();
l2.Print();
l1.PopBack(4);
l1.PopBack(3);
l1.PopBack(2);
l1.PopBack(1);
l1.Print();
l1.PopBack(3);
l1.PopBack(2);
l1.PopBack(1);
l1.Print();
SList l3;
l3.PushBack(4);
l3.PushBack(4);
l3.PushBack(4);
l3.PushBack(4);
l3.PushBack(4);
l3.PushBack(4);
l3.PushBack(4);
l3.PushBack(4);
l1 = l3;
l1.Print();
l3.Print();
}
l1.Print();
l3.Print();
}
本文介绍两种序列列表的实现方法:一种是基于数组的SeqList,包括插入、删除等操作;另一种是基于链表的SList,提供了复制构造、赋值运算符重载等功能。
382

被折叠的 条评论
为什么被折叠?



