码迷,mamicode.com
首页 > 其他好文 > 详细

单链表(模板类)

时间:2016-04-19 14:08:38      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
#include<assert.h>
using namespace std;

template <class T>
struct Node
{
Node(const T& x)
:_data(x)
, _pNext(NULL)
{ }

Node<T> *_pNext;
T _data;
};
template <class T>
class SList
{
public:
SList()
:_pHead(NULL)
, _size(0)
{ }

SList(const SList<T>& l)
{
Node<T>*pNode = l._pHead;

while (pNode)
{
PushBack(pNode->_data);
pNode = pNode->_pNext;
}
}

~SList()
{
Clear();
}

void Clear()
{
Node<T>*pNode = _pHead;

while (pNode)
{
Node<T>* pDel = pNode;
pNode = pNode->_pNext;
delete pDel;
}
_pHead = NULL;
_size = 0;
}

SList& operator = (const SList& other)
{
if (this != &other)
{
while (_size != 0)
{
PopBack();
}

Node<T> *pNode = other._pHead;
while (NULL != pNode)
{
PushBack(pNode->_data);
pNode = pNode->_pNext;
}
}

return *this;
}

//尾插
void PushBack(const T& x)
{
Node<T>*pNode = _BuyNode(x);
Node<T>*cur = _pHead;
if (Empty())
{
_pHead = pNode;
}
else
{
while (cur->_pNext)
{
cur = cur->_pNext;
}
cur->_pNext = pNode;
}
_size++;
}
//尾删
void PopBack()
{
if (Empty())
{
return;
}
else if (_size == 1)
{
delete _pHead;
_pHead = NULL;
_size = 0;
}
else
{
Node<T>*pNode = _pHead;
Node<T>*pPreNode = NULL;
while (pNode->_pNext)
{
pPreNode = pNode;
pNode = pNode->_pNext;
}
pPreNode->_pNext = NULL;
delete pNode;
_size--;
}

}
//头插
void PushFront(const T&x)
{
Node<T>*pNode = _BuyNode(x);
if (Empty())
{
_pHead = pNode;
}
else
{
pNode->_pNext = _pHead;
_pHead = pNode;
}
_size++;
}
//头删
void PopFront()
{
if (Empty())
{
return;
}
else if (_size == 1)
{
delete _pHead;
_pHead = NULL;
_size = 0;
}
else
{
Node<T>*pNewNode = _pHead;
_pHead = _pHead->_pNext;
delete pNewNode;
pNewNode = NULL;
_size--;
}
}
//显示
void Print()
{
Node<T>*cur = _pHead;
while (cur)
{
cout << cur->_data << "->";
cur = cur->_pNext;
}
cout << "NULL";
}
//查找
Node<T>* Serach(const T&x)
{
if (Empty())
{
return 0;
}
Node<T>*cur = _pHead;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_pNext;
}
return NULL;
}
//删除
void Erase(Node<T>*pos)
{

if (Empty())
{
return;
}
if (pos == _pHead)
{
_pHead = pos->_pNext;
delete pos;
}
else
{
Node<T>*cur = _pHead;
while (cur)
{
if (cur->_pNext == pos)
{
cur->_pNext = pos->_pNext;
delete pos;
}
cur = cur->_pNext;
}
}
}

//排序(冒泡法)
void BubbleSort()
{
Node<T>* ptail = NULL;
Node<T>*pNode = NULL;
Node<T>* ppreNode = NULL;

if (_pHead == NULL || _pHead->_pNext == NULL)
{
return;
}
while (_pHead != ptail)
{
int exchange = 0;
ppreNode = _pHead;
pNode = _pHead->_pNext;
while (pNode != ptail)
{
if (ppreNode->_data > pNode->_data)
{
T temp;
temp = ppreNode->_data;
ppreNode->_data = pNode->_data;
pNode->_data = temp;
exchange = 1;
}
ppreNode = pNode;
pNode = pNode->_pNext;
}
if (exchange == 0)
{
return;
}
ptail = ppreNode;
}
}

//逆置(前插)
void Reverse()
{
Node<T>* pPreNode = NULL;
Node<T>* pNewNode = NULL;
Node<T>* pNode = _pHead;
if (_pHead == NULL || (_pHead)->_pNext == NULL)
{
return;
}
while (pNode)
{
pPreNode = pNode;
pNode = pNode->_pNext;
pPreNode->_pNext = pNewNode;
pNewNode = pPreNode;
}
_pHead = pNewNode;
}

bool Empty()
{
return _size == 0;
}

T & operator[](size_t index)
{
if (index >= _size)
{
cout << "index error" << endl;
abort();
}
Node<T>* pNode = _pHead;
while (index--)
{
pNode = pNode->_pNext;
}
return pNode->_data;
}

const T & operator[](size_t index) const
{
if (index >= _size)
{
cout << "index error" << endl;
abort();
}
Node* pNode = _pHead;
while (index--)
{
pNode = pNode->_pNext;
}
return pNode->_data;
}

friend ostream& operator << (ostream& _cout, const SList<T>&l)
{
Node<T> *pNode = l._pHead;

while (pNode)
{
_cout << pNode->_data << "->";
pNode = pNode->_pNext;
}
_cout << "NULL";
return _cout;
}

Node<T>*FindMidNode();

private:
Node<T>* _BuyNode(const T& data)
{
return new Node<T>(data);
}

private:
Node<T> *_pHead;
size_t _size;
};
//查找单链表的中间节点
template <typename T>
Node<T>*SList<T>::FindMidNode()
{
Node<T>*pFast = _pHead;
Node<T>*pSlow = _pHead;
while (pFast&&pFast->_pNext)
{
pFast = pFast->_pNext->_pNext;
pSlow = pSlow->_pNext;
}
return pSlow;
}

void main()
{
SList<int> s;

s.PushBack(6);
s.PushBack(3);
s.PushBack(7);
cout << s[0] << endl;
cout << s << endl;

SList<int> s1(s);

cout << s1 << endl;

s.Print();

}

单链表(模板类)

标签:

原文地址:http://www.cnblogs.com/-zyj/p/5407843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!