c++实现单链表
2015-08-26 12:07
381 查看
#include<iostream> #include<assert.h> using namespace std; typedef int DataType; struct LinkNode //创建一个节点 { DataType _data; LinkNode* _next; LinkNode(const DataType &x) :_data(x), _next(NULL) {} }; class SList { public: SList(LinkNode* head = NULL, LinkNode* tail = NULL) :_Head(head), _Tail(tail) {} void Swap( SList &s) { swap(_Head, s._Head); swap(_Tail, s._Tail); } SList(const SList &s) :_Head(NULL) , _Tail(NULL) { if (s._Head == NULL) { return; } LinkNode* begin = s._Head; do{ PushBack(begin->_data); begin = begin->_next; } while (begin != s._Head); } ~SList() { while (_Head) { if (_Head == _Tail) { delete _Head; _Head = NULL; _Tail = NULL; } else { LinkNode *del = _Head; _Head = _Head->_next; delete del; _Tail->_next = _Head; } } } SList operator= ( SList& s) { SList tmp(s); Swap(tmp); return *this; } public: void Destory() { if (_Head == NULL) { return; } //先不管_Head和_Tail等全部删完将_Head和_Tail置为NUL LinkNode* begin = _Head; do{ LinkNode* del = begin; begin = begin->_next; delete del; } while (begin != _Head); _Head = NULL; _Tail = NULL; } void Print() { //1.没有节点 //2.多个节点 if (_Head == NULL) { return; } LinkNode *begin = _Head; do{ cout << begin->_data<< "->"; begin = begin->_next; } while (begin != _Head); cout<< endl; } void PushBack(const DataType& x)//尾插 {//1.没有节点 //2.有多个节点 if (_Head == NULL) { _Head = new LinkNode(x); _Tail = _Head; _Tail->_next = _Head; } else { LinkNode *tmp = new LinkNode(x); _Tail->_next = tmp; tmp->_next = _Head; _Tail = tmp; } } void PopBack()//尾删 { if (_Head == NULL) //没有节点 { cout << "SList is empty" << endl; return; } else if (_Head == _Tail) //有一个节点 { delete _Head; _Head = NULL; _Tail = NULL; } else //有多个节点 { LinkNode *prev = _Head; while (prev->_next != _Tail) { prev = prev->_next; } prev->_next = _Head; delete _Tail; _Tail = prev; } } void PopFront()//头删 { if (_Head == NULL) //没有节点 { cout << "SList is empty" << endl; return; } else if (_Head == _Tail) //有一个节点 { delete _Head; _Head = NULL; _Tail = NULL; } else { LinkNode* del = _Head; _Head = _Head->_next; delete del; _Tail->_next = _Head; } } void PushFront(DataType x)//头插 { if (_Head == NULL) //没有节点 { _Head = new LinkNode(x); _Tail = _Head; _Tail->_next = _Head; } else //有多个节点 { LinkNode* tmp = new LinkNode(x); tmp->_next = _Head; _Head = tmp; _Tail->_next = _Head; } } LinkNode* Find(DataType x) { if (_Head == NULL) //没有节点 { cout << "SList is empty" << endl; return NULL; } LinkNode* begin = _Head; do{ if (begin->_data == x) { cout << "find " << x << endl; return begin; } begin = begin->_next; } while (begin != _Head); cout << "not find " << x << endl; return NULL; } bool Remove(LinkNode * n) { assert(n); if (_Head == NULL) //没有节点 { cout << "SList is empty" << endl; return false; } if (n == _Head) //n是头指针 { _Head = _Head->_next; delete n; _Tail->_next = _Head; return true; } LinkNode* prev = _Head; do{ if (prev->_next == n) //n是中间指针 { prev->_next = n->_next; delete n; if (n == _Tail) //n是尾指针 { _Tail = prev; _Tail->_next = _Head; } return true; } prev = prev->_next; } while (prev != _Head); return false; } void Insert(LinkNode * n, DataType x) //先找到节点n,在n后面插一个节点 { assert(n); if (_Head == NULL) //没有节点 { cout << "SList is empty" << endl; return ; } LinkNode* begin = _Head; do{ if (begin == n) { LinkNode* tmp = new LinkNode(x); tmp->_next = n->_next; n->_next = tmp; if (n == _Tail) { _Tail = tmp; _Tail->_next = _Head; } return; } begin = begin->_next; } while (begin != _Head); cout << "not find " << n << endl; } void Reverse() //翻转 { if (_Head == NULL) //没有节点 { cout << "SList is empty" << endl; return; } if (_Head == _Tail) { cout << "只有一个节点" << endl; return; } LinkNode* tmp = _Head; LinkNode* newhead = tmp, *newtail = tmp; do{ LinkNode* prev = tmp; tmp = tmp->_next; prev->_next = newhead; newhead = prev; } while (tmp != _Head); newtail->_next = newhead; _Head = newhead; } private: LinkNode* _Head; LinkNode* _Tail; }; void test() { SList s1; s1.PushBack(1); s1.PushBack(2); s1.PushBack(3); s1.Print(); SList s2 = s1; /*s2.PushBack(4); s2.PushBack(5); s2.PushBack(6);*/ s2.Print(); /*s1 = s2; s1.Print();*/ //s1.PopBack(); //s1.PopBack(); //s1.PopBack(); //s1.PopBack(); //没有节点的时候删除 //s1.Print(); } void test2() { SList s1; s1.PushFront(1); s1.PushFront(2); s1.PushFront(3); s1.Print(); /*s1.Reverse(); s1.Print();*/ LinkNode* node = s1.Find(1); s1.Insert(node, 4); /*s1.Remove(node);*/ s1.Print(); /*s1.PopFront(); s1.PopFront(); s1.PopFront(); s1.PopFront(); s1.Find(2); s1.Print();*/ } int main() { test(); getchar(); return 0; }
相关文章推荐
- 创建新线程
- 对于EnterCriticalSection和LeaveCriticalSection的理解和用法
- C语言编程中建立和解除内存映射的方法
- 设计模式C++实现(1)——工厂模式
- 几种快速傅里叶变换(FFT)的C++实现
- C++_编写动态链接库
- 深入理解C语言的函数调用过程
- c++与perl在正则表达式运算速度上的比较
- 【资料整理】C语言位运算总结
- C++多态
- WTL小问题解决方法汇总
- LeetCode 215. Kth Largest Element in an Array
- [VS2010]_[初级]_[VC++ Express 使用WDK的ATL编译出现的问题解决办法]
- [VS2010]_[初级]_[VC++ Express 使用WDK的ATL编译出现的问题解决办法]
- C语言中字符串的内存地址操作的相关函数简介
- C++中的标准库类型
- C# 中动态调用C++动态链接
- C# 中动态调用C++动态链接
- 黑马程序员——C语言中的数据
- C++ 预处理、编译、汇编、链接