C++小记:C++实现简易单链表模板类,重载输出流“<<”。
2016-11-14 19:59
471 查看
直接上代码:
SLList.h//SLList.h //线性表的链接式存储之单链表 template<class T> class SLList; template<class T> std::ostream & operator<<(std::ostream & os,const SLList<T> &t); //Singly-Linked List Node template<class T> struct SLNode{ T data; SLNode<T> *next; SLNode(SLNode *nextNode=NULL){ next=nextNode; } SLNode(const T& item,SLNode *nextNode=NULL){ data=item; next=nextNode; } }; //Singly-Linked List template<class T> class SLList{ private: SLNode<T> *head; public: SLList(){head=new SLNode<T>();} SLList(T &item); ~SLList(); bool IsEmpty()const{return head->next==NULL;} int Length()const; bool Find(int k, T &item)const; int Search(const T &item)const; void Delete(int k,T &item); void Insert(int k,const T &item); friend std::ostream& operator<< <T>(std::ostream &os,const SLList<T> &t); }; template<class T> std::ostream& operator<<(std::ostream &os,const SLList<T> &t){ SLNode<T> *p=t.head->next; while(p!=NULL){ cout<<"["<<p->data<<"]."; p=p->next; } return os; } template<class T> SLList<T>::~SLList(){ SLNode<T> *p=head; while(!IsEmpty()){ head=head->next; delete p; p=head; } delete head; } template<class T> int SLList<T>::Length()const{ int i=-1; SLNode<T> *p=head; while(p!=NULL){ i++; p=p->next; } return i; } template<class T> bool SLList<T>::Find(int k,T &item)const{ if(k<1){ cout<<"The node does not exit!"<<endl; return false; } SLNode<T> *p=head; for(int i=1; p!=NULL&&i<=k; i++){ p=p->next; //cout<<k<<" "<<i<<endl; } if(p==NULL){ cout<<"The node does not exit!"<<endl; return false; } item=p->data; return true; } template<class T> int SLList<T>::Search(const T &item)const{ SLNode<T> *p=head->next; int i=1; while(p!=NULL&&p->data!=item){ p=p->next; i++; } if(p!=NULL){ return i; } cout<<"The node does not exit!"<<endl; return -1; } template<class T> void SLList<T>::Delete(int k, T &item){ if(k<1){ cout<<"Delete illegal!"<<endl; return; } SLNode<T> *p=head; int i=0; while(p!=NULL&&i<k-1){ p=p->next; i++; } if(p->next==NULL){ cout<<"The Node does not exit!"<<endl; return; } SLNode<T> *q=p->next; p->next=q->next; item=p->data; delete q; return; } template<class T> void SLList<T>::Insert(int k,const T &item){ if(k<0){ cout<<"Insert illegall"<<endl; return; } SLNode<T> *p=head; int i=0; while(p!=NULL&&i<k){ p=p->next; i=i+1; } if(p==NULL){ cout<<"The Node does not exit!"<<endl; return; } SLNode<T> *q=new SLNode<T>(); q->next=p->next; p->next=q; q->data=item; return;}main.cpp
#include <iostream> #include"SLList.h" using namespace std; void testSLList(); int main() { testSLList(); } void testSLList(){ cout<<"Testing SLList!"<<endl; int a=1; int b=9999; SLList<int> test; for(a=1;a<11;a++){ test.Insert(a-1,a); } test.Insert(test.Length(),a); cout<<test<<endl; cout<<"\ttest Delete!"<<endl; test.Delete(0,b); cout<<test<<endl; cout<<"\ttest Search!"<<endl; int sea=11; cout<<test.Search(sea)<<endl; cout<<"\ttest Find!"<<endl; cout<<test<<endl; test.Find(1,b); cout<<b<<endl; cout<<"\ttest Length!"<<endl; cout<<test.Length(); return; }编译环境:win10+MinGW
输出结果:
相关文章推荐
- 第九周 C++任务一。定义Complex类中的<<和>>运算符的重载,实现输入和输出
- 《C++第九周实验报告1-1》--------接第8周任务1,定义Complex类中的<<和>>运算符的重载,实现输入和输出
- C++第九周【任务一】定义Complex类中的<<和>>运算符的重载,实现输入和输出
- 数据结构之单链表——C++模板类实现
- C++模板类实现单链表
- 第九周C++报告二。定义Time类中的<<和>>运算符重载,实现时间的输入输出
- 模板类的友元重载,用C++代码实现
- C++程序学习--C++模板类重载左移操作符<<的细节
- C++ 重载输出运算符<<
- c++ 重载 >>(输入) 、<< (输出) 操作符
- C++_重载运算符<< ostream流重载注意事项_使自定义数据可以用cout输出
- 模板类中操作符重载问题("<<"和">>"重载)[c++]
- C++ class template中,重载输出运算符(<<)的方法
- 用c++实现 c++单链表的实现(采用模板类)
- 《C++第九周实验报告2-1》---接第8周任务2,定义Time类中的<<和>>运算符重载,实现时间的输入输出
- 用C++实现单链表的创建、逆置和输出
- c++文件输入输出流fstream,对输入>>和输出<<重载
- c++ 重载 >>(输入) 、<< (输出) 操作符
- 第九周任务一(定义Complex类中的<<和>>运算符的重载,实现输入和输出)
- 九周任务三:定义分数类中的<<和>>的重载,实现分数的输入和输出