单链表(模板类)

#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();

}

时间: 2024-11-05 16:00:14

单链表(模板类)的相关文章

C++ 单链表模板类实现

单链表的C语言描述 基本运算的算法--置空表.求表的长度.取结点.定位运算.插入运算.删除运算.建立不带头结点的单链表(头插入法建表).建立带头结点的单链表(尾插入法建表),输出带头结点的单链表 #include<cstdio>#include<iostream>using namespace std;template <class T>class Linklist{private: struct node { T date; node * next; node():n

C++数据结构 单链表(模板类)

利用模板类实现单链表及其功能 需要实现的操作: [1] push_back       [2] push_front [3] show_list       [0] quit_system [4] pop_back        [5] pop_front [6] insert_val      [7] delete_val [8] find            [9]length [10] clear          [11]destroy [12] reserv         [13]

分享一个线程安全的单例模板类

单例模式应该说是最简单的设计模式了.在此分享一个线程安全的单例模板类. template <typename Type> class CSingleton { public: static Type* GetInstance() { // kBeingCreatedMarker用来表示单例实例正在创建过程中. // 此处初始化为1是因为操作系统不会分配地址为1的指针. static const volatile intptr_t kBeingCreatedMarker = 1; // 如果m_

单链表sLinkList类,模板类

sLinkList模板类,单链表代码 1 /* 2 该文件按习惯可以分成.h文件和实现的.cpp文件 3 */ 4 template <class elemType> 5 class sLinkList 6 { 7 private: 8 struct node{ //定义单链表中的结点结构 9 elemType data; 10 node *next; 11 12 node(const elemType &x, node *n = NULL) 13 { 14 data = x; nex

[数据结构]线性表之单链表的类模板实现

类的具体实现如下: ///////////////////////// #include"LinearList.h" #include <iostream> #include <cstdlib> using namespace std; template<class T> struct LinkNode //链表节点类 { T data; LinkNode<T>* link; LinkNode(LinkNode<T>* ptr

算法与数据结构基础3:简单单链表List类的实现

简单的单链表实现,数据类型定义成了int,如果要通用,需要改成模板类,然后稍微修改下就可以了. // List.h #include <cstdio> #include <cassert> #include <iostream> using namespace std; class List { public: // ************************************************************************** //

List链表模板类的简单实现(部分方法递归操作)

善哉. 此篇博客,旨在剖析下操作单链表时的递归思想.望各位施主笑纳. 1. 递归删除结点 * 空链表 - 直接返回 * 非空,若未找到待删除元素,递归.若找到,删除节点,返回尾链头 * 回溯,衔接形成新链 1 _Node* myErase_R(const Object& elem, _Node* curr){ 2 //空链 或 无此元素 3 if (curr == NULL) return NULL; 4 5 if (curr->elem == elem){ 6 _Node* tmp = c

数据结构—单链表(类C语言描写叙述)

单链表 1.链接存储方法 链接方式存储的线性表简称为链表(Linked List). 链表的详细存储表示为: ① 用一组随意的存储单元来存放线性表的结点(这组存储单元既能够是连续的.也能够是不连续的) ② 链表中结点的逻辑次序和物理次序不一定同样.为了能正确表示结点间的逻辑关系,在存储每一个结点值的同一时候,还必须存储指示其后继结点的地址(或位置)信息(称为指针(pointer)或链(link)) 注意: 链式存储是最经常使用的存储方式之中的一个.它不仅可用来表示线性表.并且可用来表示各种非线性

C++链表模板类

思想和上篇文章差不多,只是换了层包装. 直接上代码: // linklist.h #include <iostream> #include <cstdio> using namespace std; template <typename T> struct Node { T t; Node<T> *next; }; template <typename T> class LinkList { public: LinkList(); ~LinkLi