在之前的博客里面已经分享过顺序表的写法,今天我们直接用模板来实现顺序表,其代码如下:
#pragma once
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
#pragma warning( disable : 4996)
template<class T>
class SeqList
{
public:
SeqList()
:_a(NULL)
, _size(0)
, _capacity(0)
{}
SeqList(const SeqList& s)
{
:_a(s._data)
, _size(s._size)
, _capacity(s._capacity)
{}
}
SeqList& operator=(SeqList& s)
{
swap(_a,s._a);
swap(_size,s._size);
swap(_capacity,s._capacity);
return *this;
}
~SeqList()
{
if(_a)
{
free(_a);
}
}
void CheakCapacity()
{
if(_size == _capacity)
{
size_t NewSize = _size ? _size * 2 : 3;
_a = (T*)realloc(_a, NewSize*sizeof(T));
_capacity = NewSize;
}
}
void PushBack(const T& x)
{
CheakCapacity();
_a[_size++] = x;
}
void PopBack()
{
assert(_size);
--_size;
}
void Insert(size_t pos, const T& x)
{
assert(pos);
assert(_size);
CheckCapacity();
for (size_t end = pos; end > pos; ++end)
{
_data[end] = _data[end + 1];
}
_data[pos] = x;
_size++;
}
void Erase(size_t pos)
{
assert(pos);
for (size_t i = pos; i < _size; ++i)
{
_a[i - 1] = _a[i];
}
--_size;
}
T& operator[](size_t pos)
{
assert(pos < _size);
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;
};
//void TestSeqList()
//{
// SeqList<int>s1;
// s1.PushBack(1);
// s1.Print();
//}
void TestSeqList()
{
SeqList<char*> s2;
s2.PushBack("aaaaaaaaaaaaaaaaaaa");
s2.PushBack("bbb");
s2.PushBack("ccc");
s2.PushBack("ddd");
s2.Print();
}
int main()
{
TestSeqList();
system("pause");
return 0;
}
链表可分为带结点和不带结点,单向和双向以及循环和不循环
双向循环链有指向前一个数的指针和指向后一个数的指针。
如下图
#include<iostream>
#include<assert.h>
#include<windows.h>
using namespace std;
template<class T>
struct ListNode
{
T _data;
ListNode* _prev;
ListNode* _next;
ListNode(const T& x=T())
:_data(x)
, _prev(NULL)
, _next(NULL)
{}
};
template <class T>
class List
{
typedef ListNode<T> Node;
public:
List()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
//s1(s2)
List(const List<T>& l)
{
_head = new Node();
_head->_next = _head;
_head->_prev = _head;
Node* cur = (l._head)->_next;
while (cur != l._head)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
//s1=s2
/*List<T>& operator=(const List<T>& l)
{
if (this != &s)
{
Clear();
Node* cur = (l._head)->_next;
while (cur != l._head)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
return *this;
}*/
List<T>& operator=(List<T> l)
{
swap(_head, l._head);
return *this;
}
~List()
{
Clear();
delete _head;
_head = NULL;
}
void Clear()
{
Node* cur = _head->_next;
while (cur != _head)
{
Node* next = cur->_next;
delete cur;
cur = next;
/*Node* del = cur;
cur = cur->_next;
delete cur;*/
}
_head->_next = _head;
_head->_prev = _head;
}
void Print()
{
Node* cur = _head->_next;
while (cur != _head)
{
cout << cur->_data << " ";
cur = cur->_next;
}
cout << endl;
}
void PushBack(const T& x)
{
/* Node* tail = _head->_prev;
Node* tmp = new Node(x);
tail->_next = tmp;
tmp->_prev = tail;
_head->_prev = tmp;
tmp->_next = _head; */
Insert(_head, x);
}
void PopBack()
{
/* assert(_head->_next != _head);
Node* tail = _head->_prev;
Node* prev = tail->_prev;
delete tail;
_head->_prev = prev;
prev->_next = _head;*/
Erase(_head->_prev);
}
void PushFront(const T& x)
{
Insert(_head->_next,x);
}
void PopFront()
{
Erase(_head->_next);
}
Node* Find(const T& x)
{
Node* cur = _head->_next;
while (cur != _head)
{
if(cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
void Insert(Node* pos,const T& x)
{
assert(pos);
Node* prev = pos->_prev;
Node* tmp = new Node(x);
prev->_next = tmp;
tmp->_prev = prev;
tmp->_next = pos;
pos->_prev = tmp;
}
void Erase(Node* pos)
{
assert(pos && pos != _head);
Node* prev = pos->_prev;
Node* next = pos->_next;
prev->_next = next;
next->_prev = prev;
delete pos;
}
private:
Node* _head;
};
void Test()
{
List<int> t;
t.PushBack(1);
t.PushBack(2);
t.PushBack(3);
t.PushBack(4);
t.Print();
t.PopFront();
t.Print();
/*t.PopBack();
t.Print()*/;
/*t.Insert(t.Find(2),5);
t.Print();*/
/*t.Erase(t.Find(2));
t.Print();*/
}
int main()
{
Test();
system("pause");
return 0;
}