1. 实现顺序表
#pragma once
typedef int DataType;
class SeqList
{
public:
SeqList()
:_array(NULL)
,_size(0)
,_capacity(0)
{}
SeqList(const SeqList& s)
{
_array = (DataType*)malloc(sizeof(DataType)*s._size);
memcpy(_array, s._array, sizeof(DataType)*s._size);
_size = _capacity = s._size;
}
// s1 = s2
//SeqList& operator=(const SeqList& s)
//{
// if (this != &s)
// {
// free(_array);
// _array = (DataType*)malloc(sizeof(DataType)*s._size);
// memcpy(_array, s._array, sizeof(DataType)*s._size);
// _size = _capacity = s._size;
// }
// return *this;
//}
// s1 = s2
//SeqList& operator=(const SeqList& s)
//{
// if (this != &s)
// {
// SeqList tmp(s);
// this->Swap(tmp);
// }
// return *this;
//}
// s1 = s2
SeqList& operator=(SeqList s)
{
Swap(s);
return *this;
}
// s1.Swap(s2);
void Swap(SeqList& s)
{
swap(_array, s._array);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
~SeqList()
{
if (_array)
{
free(_array);
_size = _capacity = 0;
_array = NULL;
}
}
void PushBack(DataType x)
{
//CheckCapcacity();
//_array[_size++] = x;
Insert(_size, x);
}
void PopBack()
{
//assert(_size > 0);
//--_size;
Erase(_size-1);
}
void PushFront(DataType x)
{
/*CheckCapcacity();
int end = _size-1;
while (end >= 0)
{
_array[end+1] = _array[end];
--end;
}
_array[0] = x;
++_size;*/
Insert(0, x);
}
void PopFront()
{
assert(_size > 0);
/*for (size_t i = 1; i < _size; ++i)
{
_array[i-1] = _array[i];
}
--_size;*/
Erase(0);
}
inline void Insert(size_t pos, DataType x)
{
assert(pos <= _size);
CheckCapcacity();
for (int end = _size-1; end >= (int)pos; --end)
{
_array[end+1] = _array[end];
}
_array[pos] = x;
++_size;
}
inline void Erase(size_t pos)
{
assert(pos < _size);
for (size_t i = pos+1; i < _size; ++i)
{
_array[i-1] = _array[i];
}
--_size;
}
inline DataType& operator[](size_t pos)
{
assert(pos < _size);
return _array[pos];
}
void CheckCapcacity()
{
if (_size == _capacity)
{
_capacity = _capacity*2+3;
_array = (DataType*)realloc(_array, _capacity*sizeof(DataType));
}
}
void Print()
{
for (size_t i = 0; i < _size ; ++i)
{
cout<<_array[i]<<" ";
}
cout<<endl;
}
private:
DataType* _array;
size_t _size;
size_t _capacity;
};
void TestSeqList()
{
SeqList s;
s.PushBack(1);
s.PushBack(2);
s.PushBack(3);
s.PushBack(4);
s.Print();
s.PushFront(0);
s.Print();
s.Insert(3, 30);
s.Insert(0, 30);
s.Print();
SeqList s1(s);
s1.PushFront(10000);
s1.Print();
s = s1;
s.Print();
}
2. 实现单链表
#pragma once
typedef int DataType;
struct SListNode
{
SListNode* _next;
DataType _data;
SListNode(DataType x)
:_data(x)
,_next(NULL)
{}
};
class SList
{
typedef SListNode Node;
public:
SList()
:_head(NULL)
,_tail(NULL)
{}
void Copy(const SList& s)
{
Node* cur = s._head;
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
void Destory()
{
Node* cur = _head;
while (cur)
{
Node* next = cur->_next;
delete cur;
cur = next;
}
_head = _tail = NULL;
}
// s2(s1)
SList(const SList& s)
:_head(NULL)
,_tail(NULL)
{
Copy(s);
}
// s1 = s2;
// s1 = s1;
/*SList& operator=(const SList& s)
{
if(this != &s)
{
Destory();
Copy(s);
}
return *this;
}*/
// s1 = s2
SList& operator=(SList s)
{
swap(_head, s._head);
swap(_tail, s._tail);
return *this;
}
~SList()
{
Destory();
}
void PushBack(DataType x)
{
// 1.空
// 2.有一个及以上的节点
if (_head == 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 = _tail = NULL;
}
else
{
Node* prev = _head;
while (prev->_next != _tail)
{
prev = prev->_next;
}
delete _tail;
_tail = prev;
_tail->_next = NULL;
}
}
void PushFront(DataType x)
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
Node* tmp = new Node(x);
tmp->_next = _head;
_head = tmp;
}
}
void PopFront()
{
if (_head == NULL)
{
return;
}
else if (_head == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* del = _head;
_head = _head->_next;
delete del;
}
}
// 插入一个节点在pos的前面
void Insert(Node* pos, DataType x)
{
assert(pos);
if (pos == _head)
{
PushFront(x);
}
else
{
Node* prev = _head;
while (prev->_next != pos)
{
prev = prev->_next;
}
Node* tmp = new Node(x);
prev->_next = tmp;
tmp->_next = pos;
}
}
void Erase(Node* pos)
{
assert(pos);
if (pos == _head)
{
PopFront();
}
else if (pos == _tail)
{
PopBack();
}
else
{
Node* prev = _head;
while (prev->_next != pos)
{
prev = prev->_next;
}
prev->_next = pos->_next;
delete pos;
}
}
Node* Find(DataType x)
{
Node* cur = _head;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
void Print()
{
Node* cur = _head;
while (cur)
{
cout<<cur->_data<<" ";
cur = cur->_next;
}
cout<<endl;
}
private:
Node* _head;
Node* _tail;
};
3. 实现双向链表
#pragma once
typedef int DataType;
struct ListNode
{
ListNode* _next;
ListNode* _prev;
DataType _data;
ListNode(DataType x)
:_next(NULL)
,_prev(NULL)
,_data(x)
{}
};
class List
{
typedef ListNode Node;
public:
List()
:_head(NULL)
,_tail(NULL)
{}
List(const List& l);
List& operator=(const List& l);
~List()
{
Node* cur = _head;
while (cur)
{
Node* del = cur;
cur = cur->_next;
delete del;
}
_head = _tail = NULL;
}
void PushBack(DataType x)
{
if (_tail == NULL)
{
_head = _tail = new Node(x);
}
else
{
Node* tmp = new Node(x);
_tail->_next = tmp;
tmp->_prev = _tail;
_tail = tmp;
}
}
void PopBack()
{
Erase(_tail);
}
void PushFront(DataType x)
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
Insert(_head, x);
}
}
void PopFront()
{
Erase(_head);
}
// 在pos的前面插入一个
void Insert(Node* pos, DataType x)
{
assert(pos);
if (pos == _head)
{
Node* tmp = new Node(x);
tmp->_next = _head;
_head->_prev = tmp;
_head = tmp;
}
else
{
Node* cur = pos;
Node* prev = cur->_prev;
Node* tmp = new Node(x);
prev->_next = tmp;
tmp->_next = cur;
cur->_prev = tmp;
tmp->_prev = prev;
}
}
void Erase(Node* pos)
{
assert(pos);
Node* prev = pos->_prev;
Node* next = pos->_next;
// 1.只有一个节点
// 2.头删
// 3.尾删
// 4.中间删
if(prev == NULL && next == NULL)
{
assert(pos == _head);
_head = _tail = NULL;
}
else if (prev == NULL)
{
_head = next;
_head->_prev = NULL;
}
else if (next == NULL)
{
_tail = prev;
_tail->_next = NULL;
}
else
{
prev->_next = next;
next->_prev = prev;
}
delete pos;
}
//void Reverse()
//{
// Node* left = _head;
// Node* right = _tail;
// /*while (left != right && left->_prev != right)*/
// while (!(left == right || left->_prev == right))
// {
// swap(left->_data, right->_data);
// left = left->_next;
// right = right->_prev;
// }
//}
void Reverse()
{
Node* cur = _head;
while (cur)
{
swap(cur->_prev, cur->_next);
cur = cur->_prev;
}
swap(_head, _tail);
}
void Print()
{
Node* cur = _head;
while (cur)
{
cout<<cur->_data<<" ";
cur = cur->_next;
}
cout<<endl;
}
Node* Find(DataType x)
{
Node* cur = _head;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
private:
Node* _head;
Node* _tail;
};
void TestList()
{
List l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
l.PushBack(4);
l.Print();
ListNode* pos = l.Find(3);
l.Insert(pos, 30);
l.Print();
l.Erase(pos);
l.Print();
l.Reverse();
l.Print();
}