C++模板類實現單鏈表
阿新 • • 發佈:2019-02-06
c++模板類實現單鏈表
#include<iostream>
using namespace std;
#include<assert.h>
#include<string>
template<class T>
struct LinkNode
{
LinkNode<T>* _next;
T _data;
LinkNode(const T& x)
:_data(x)
,_next(NULL)
{}
};
template<class T>
class List
{
typedef LinkNode<T> Node;
public:
List() //建構函式
:_head(NULL), _tail(NULL)
{
}
List(const List<T>& s) //拷貝建構函式
:_head(NULL), _tail(NULL)
{
if (s._head == NULL)
{
return;
}
else
{
Node * cur(s._head); //呼叫尾插的方法
while (cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
}
void Swap(List<T>& s) //交換函式
{
swap(_head, s._head);
swap(_tail, s._tail);
}
List<T>& operator=(List<T> s) //賦值運算子的過載
{
Swap(s);
return *this;
}
~List() //解構函式
{
clear();
}
void clear() //清理函式
{
Node *cur = _head;
while (cur)
{
Node *next = cur->_next;
delete cur;
cur = next;
}
_head = _tail = NULL;
}
void Display() //顯示函式
{
Node *cur = _head;
while (cur)
{
cout << cur->_data << " ";
cur = cur->_next;
}
cout << endl;
}
void PushBack(const T& x) //尾插
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
Node* tmp = new Node(x);
_tail->_next = tmp;
_tail = tmp;
}
}
void PushFront(const T& x) //頭插
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
Node *tmp = new Node(x);
tmp->_next = _head;
_head = tmp;
}
}
void PopBack() //尾刪
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _tail;
_head = _tail = NULL;
}
else
{
Node*tmp = _head;
while (tmp->_next != _tail)
{
tmp = tmp->_next;
}
delete _tail;
_tail = tmp;
_tail->_next = NULL;
tmp = NULL;
}
}
void PopFront() //頭刪
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node *_del = _head;
_head = _del->_next;
delete _del;
_del = NULL;
}
}
Node* Find(T x) //查詢函式
{
if (_head == NULL)
return NULL;
else
{
Node *cur(_head);
while (cur != NULL)
{
if (cur->_data == x)
{
break;
}
cur = cur->_next;
}
return cur;
}
}
void Insert(Node* pos, T x) // 指定元素前插入資料
{
assert(pos);
if (pos == _head)
{
PushFront(x);
}
else
{
Node *tmp = new Node(x);
Node *cur = _head;
while (cur->_next != pos)
{
cur = cur->_next;
}
tmp->_next = pos;
cur->_next = tmp;
}
}
void Destroy()//銷燬單鏈表
{
if (_head == NULL)
{
_head = _tail = NULL;
}
else
{
Node *cur(_head);
while (cur != NULL)
{
cur = cur->_next;
delete _head;
_head = cur;
}
_head = _tail = NULL;
}
}
void Erase(Node* pos) //刪除指定元素
{
assert(pos);
if (pos == _head)
{
_head = _head->_next;
delete pos;
}
else
{
Node* cur(_head);
while (cur->_next != pos)
{
cur = cur->_next;
}
cur->_next = pos->_next;
delete pos;
pos = NULL;
}
}
Node* Reverse() //單鏈表的逆置
{
Node *pre = _head;
Node *cur = _head->_next;
Node *next = NULL;
if (_head == NULL || _head->_next == NULL)
return NULL;
while (cur != NULL)
{
next = cur->_next;
cur->_next = pre;
pre = cur;
cur = next;
}
_head->_next = NULL;
_head = pre;
return _head;
}
protected:
Node *_head;
Node *_tail;
};
void test1()//尾插和頭插
{
List<int> s1;
s1.PushBack(3);
s1.PushBack(4);
s1.PushFront(2);
s1.PushFront(1);
s1.Display();
}
void test2() //尾刪和頭刪
{
List<int> s2;
s2.PushBack(3);
s2.PushBack(4);
s2.PushFront(2);
s2.PushFront(1);
s2.PopBack();
s2.PopBack();
s2.PopFront();
s2.PopFront();
s2.Display();
}
void test3() //指定元素前插入資料和銷燬連結串列
{
List<int> s3;
s3.PushBack(3);
s3.PushBack(4);
s3.PushFront(2);
s3.PushFront(1);
s3.Insert(s3.Find(1), 0);
s3.Display();
s3.Destroy();
s3.Display();
}
void test4() //拷貝構造
{
List<int> s3;
s3.PushBack(3);
s3.PushBack(4);
s3.PushFront(2);
s3.PushFront(1);
List<int> s4(s3);
s4.Display();
}
void test5() //賦值運算子的過載
{
List<int> s3;
s3.PushBack(3);
s3.PushBack(4);
s3.PushFront(2);
s3.PushFront(1);
List<int> s5 = s3;
s5.Display();
}
void test6() //測試插入字串
{
List<string> s6;
s6.PushBack("abc");
s6.PushBack("123456789123456789");
s6.Display();
}
void test7() //單鏈表指定元素刪除
{
List<int> s7;
s7.PushBack(3);
s7.PushBack(4);
s7.PushFront(2);
s7.PushFront(1);
s7.Erase(s7.Find(1));
s7.Display();
}
void test8() //單鏈表的逆置
{
List<int> s8;
s8.PushBack(1);
s8.PushBack(2);
s8.PushBack(3);
s8.PushBack(4);
s8.Reverse();
s8.Display();
}
int main()
{
//test1();
//test2();
//test3();
//test4();
//test5();
//test6();
//test7();
test8();
system("pause");
return 0;
}
需要指出的是這裡的型別不是List,而是List。程式碼均已測試。