C++实现单链表

来源:互联网 发布:仿起点小说php源码 编辑:程序博客网 时间:2024/05/21 07:04

C++和C语言最大的不同就是类和模板。利用C++实现单链表当然少不了类。首先利用结构体定义好结点。

struct Node{ DataType _data;struct Node* _next;Node(const DataType& d):_data(d),_next(NULL){}};

接下来,利用类的构造函数、析构函数以及运算符的重载等来将链表搭建起来。

class sList{public:sList():_head(NULL),_tail(NULL){}~sList(){if(_head==NULL){return;}Node *cur=_head;while(cur->_next!=NULL){Node *del=cur;cur=cur->_next;delete del;}delete cur;}sList(const sList& s):_head(NULL),_tail(NULL){Node* cur=s._head;while(cur){(cur->_data);cur=cur->_next;}}sList& operator=(sList& s){swap(_head,s._head);swap(_tail,s._tail);}friend ostream& operator<<(ostream& os,const sList& s);void PushBack(const DataType& d);void PushFront(const DataType& d);void PopBack();void PopFront();Node* Find(const DataType& d);void Insert(Node* pos,const DataType& d);void Reverse();void Sort();void Remove(const DataType& d);void RemoveAll(const DataType& d);private:Node *_head;Node *_tail;};ostream& operator<<(ostream& os,const sList& s){Node* cur=s._head;while(cur!=NULL){os<<cur->_data<<"->";cur=cur->_next;}cout<<"over"<<endl;return os;}

如上所示,将函数在类内声明,实现写在类外,和C语言的写法大致相同,不在详述。

void sList::PushBack(const DataType& d){Node* newNode=new Node(d);if(_head==NULL){_head=newNode;_tail=_head;}else{_tail->_next=newNode;_tail=newNode;}}void sList::PushFront(const DataType& d){Node* newNode=new Node(d);if(_head==NULL){_head=newNode;_tail=_head;}else{newNode->_next=_head;_head=newNode;}}void sList::PopBack(){if(_head==NULL){return;}else if(_head==_tail){delete _head;_head=NULL;_tail=NULL;return;}else{Node* cur=_head;while(cur->_next!=_tail){cur=cur->_next;}delete _tail;_tail=cur;cur->_next=NULL;}}void sList::PopFront(){if(_head==NULL){return;}else{Node* del=_head;_head=_head->_next;delete del;del=NULL;}}Node* sList::Find(const DataType& d){Node* cur=_head;while(cur!=NULL){if(cur->_data==d){return cur;}cur=cur->_next;}return NULL;}void sList::Insert(Node* pos,const DataType& d){if(pos==NULL){return;}Node* newNode=new Node(d);if(pos==_tail){_tail->_next=newNode;_tail=newNode;}else{newNode->_next=pos->_next;pos->_next=newNode;}}void sList::Reverse(){if((_head==NULL) || (_head==_tail)){return;}Node* cur=_head;//therr pointNode* prev=NULL;Node* newHead=NULL;while(cur){prev=cur;cur=cur->_next;prev->_next=newHead;newHead=prev;_head=newHead;}}void sList::Sort(){Node* cur=_head;Node* end=NULL;while(cur!=end){while(cur && cur->_next!=end){if(cur->_data  <  cur->_next->_data){DataType tmp=cur->_data;cur->_data=cur->_next->_data;cur->_next->_data=tmp;}cur=cur->_next;}end=cur;cur=_head;}}void sList::Remove(const DataType& d){Node* cur=_head;Node* del=NULL;Node* prev=NULL;while(cur!=NULL){if(cur->_data==d){del=cur;if(cur==_head){_head=_head->_next;}else{prev->_next=cur->_next;}delete del;break;}prev=cur;cur=cur->_next;}}void sList::RemoveAll(const DataType& d){Node* cur=_head;Node* del=NULL;Node* prev=NULL;while(cur!=NULL){if(cur->_data==d){del=cur;if(cur==_head){_head=_head->_next;_head=cur;}else{prev->_next=cur->_next;cur=prev->_next;}delete del;}else{prev=cur;    cur=cur->_next;}}}void sList::Erase(Node* pos){if(pos==NULL){return;}Node* del=NULL;Node* cur=_head;Node* prev=NULL;while(cur!=NULL){if(pos==cur){del=cur;{if(pos==_head){_head=_head->_next;}else{prev->_next=cur->_next;}delete del;break;}}prev=cur;cur=cur->_next;}}Node* sList::FindMidNode(){Node* Slow=_head;Node* Fast=_head;if(Fast==NULL || Fast->_next==NULL){return NULL;}else{while(Fast && Fast->_next){Slow=Slow->_next;Fast=Fast->_next->_next;}return Slow;}}Node* sList::CheckCircle(){Node* Slow=_head;Node* Fast=_head;while(Fast && Fast->_next){Slow=Slow->_next;Fast=Fast->_next->_next;if(Fast==Slow){return Slow;}}return NULL;}int GetCircleLength(Node* meet){Node* Start=meet;int count=0;do{count++;Start=Start->_next;}while(Start!=meet);return count;}Node* sList::GetCircleEntryNode(Node* meet){Node* entry=_head;while(entry!=meet){entry=entry->_next;meet=meet->_next;}return entry;}


本文出自 “七月朔风” 博客,请务必保留此出处http://luminous.blog.51cto.com/10797288/1752180

0 0