c++实现单链表
2017-03-25 17:57
204 查看
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <assert.h> using namespace std; typedef int dataType; struct ListNode { dataType _data; ListNode* _next; ListNode(const dataType x) :_data(x) ,_next(NULL) { } }; class List { typedef ListNode Node; public: List() :_head(NULL) ,_tail(NULL) {} List(const List& s) :_head(NULL),_tail(NULL) { if (s._head == NULL) { return; } else { Node* cur = s._head; while (cur) { PushBack(cur->_data); cur = cur->_next; } } } void PushBack(dataType x) { if(_head==NULL) { _head=_tail=new Node(x); } else { Node* tmp=new Node(x); _tail->_next=tmp; _tail=_tail->_next; } } void PushFront(dataType x) { if(_head==NULL) { _head=_tail=new Node(x); } else { Node* tmp=_head; _head=new Node(x); _head->_next=tmp; } } void PopBack() { if (_tail==NULL) { return; } else if (_head->_next==NULL) { delete _tail; _tail=NULL; _head=NULL; } else { Node* cur=_head; while(cur->_next!=_tail) { cur=cur->_next; } delete _tail; _tail=cur; _tail->_next=NULL; } } void PopFront() { if (_head==NULL) { return; } else if (_head->_next==NULL) { delete _tail; _tail=NULL; _head=NULL; } else { Node* del=_head; _head=_head->_next; delete del; } } Node* Find(dataType x) { Node* cur=_head; while(cur) { if (cur->_data==x) { return cur; } cur=cur->_next; } } void Insert(Node* pos,dataType x) { assert(pos); if (pos == _tail) { PushBack(x); } else if (pos==_head) { PushFront(x); } else { Node* cur = _head; while (cur->_next != pos) { cur = cur->_next; } cur->_next = new Node(x); cur->_next ->_next = pos; } } void Erase(Node* pos) { assert(pos); if (pos==_head) { PopFront(); } else if(pos==_tail) { PopBack(); } else { Node* cur = _head; while (cur->_next != pos) { cur = cur->_next; } cur->_next = pos->_next; delete pos; } } //赋值现代写法 void Swap(List& s) { swap(_head, s._head); swap(_tail, s._tail); } List& operator=(const List& L1) { if (this!=&L1) { List tmp(L1); Swap(tmp); } return *this; } //链表的逆置 void Reverse() { if (_head==NULL||_head->_next==NULL) { return; } else { Node* cur = _head; Node* pre = _head; Node* del = _head->_next; while (cur->_next) { PushFront(cur->_next->_data); cur = cur->_next; } while (del) { Node* pdel = del; del = del->_next; delete pdel; } _tail = pre; _tail->_next = NULL; } } void Print() { Node* cur=_head; while (cur) { cout<<cur->_data<<" "; cur=cur->_next; } cout<<endl; } private: Node* _head; Node* _tail; };
#include"List.h"
void TestList()
{
List list1;
List list2;
//PushBack测试
/*list1.PushBack(1);
list1.PushBack(2);
list1.PushBack(3);
list1.PushBack(4);
list1.PushBack(5);
list1.Print();*/
//PopBack测试
//list1.PushBack(1);
//list1.PushBack(2);
//list1.PushBack(3);
//list1.PushBack(4);
//list1.PushBack(5);
//list1.PopBack();
// list1.PopBack();
//list1.PopBack();
//list1.PopBack();
//list1.PopBack();
//list1.PopBack();
// list1.Print();
//PushFront测试
//list1.PushFront(1);
// list1.PushFront(2);
// list1.PushFront(3);
// list1.PushFront(4);
// list1.PushFront(5);
//list1.Print();
//PopFront()测试
// list1.PushBack(1);
// list1.PushBack(2);
// list1.PushBack(3);
// list1.PushBack(4);
// list1.PushBack(5);
// //list1.PopFront();
// //list1.PopFront();
// //list1.PopFront();
// list1.PopFront();
// list1.PopFront();
// list1.PopFront();
//list1.Print();
//Insert测试
list1.PushBack(1);
////list1.PushBack(2);
////list1.PushBack(3);
//list1.PushBack(4);
//
list1.Insert(list1.Find(2),2);
list1.Insert(list1.Find(3),3);
list1.Insert(list1.Find(3),4);
list1.Print();
list2=list1;
list1.Print();
//Erase测试
list1.Erase(list1.Find(1));
list1.Erase(list1.Find(4));
// /*list1.Erase(list1.Find(2));*/
// // list1.Erase(list1.Find(3));
// //list1.Erase(list1.Find(4));
// //// list1.Erase(list1.Find(4));
list1.Print();
//Reverse测试
list1.Reverse();
list1.Print();
}
int main()
{
TestList();
return 0;
}
相关文章推荐
- [C++]实现异质链表
- 用无序链表实现字典ADT(C++描述)
- C++链表实现堆栈:LinkList:Build a linklist using C++ class Stack
- 用单链表实现一元多项式相加 C++代码
- c++简单链表实现
- 【头文件】c++实现链表
- 链表C++实现
- 用数组实现链表(C++)
- 链表的顺序表示和实现(C++模板类实现)
- 链表——用C++模板实现
- 用无序链表实现字典ADT(C++描述)
- C++链表实现
- C++ 学习练手 - 双向链表的模板实现
- 用C++实现的双向链表
- VC++编译问题汇总1 单链表的表示和实现,基于c++
- 用无序链表实现字典ADT(C++描述)
- C++版-----链表实现
- 数据结构 链表的lua实现 仿照C++中list 实现
- 双向循环链表的c++ 实现
- 数据结构C++模板实现之----------------单向链表