您的位置:首页 > 编程语言 > C语言/C++

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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: