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

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++ 学习笔记