C++小记:C++实现简易单链表模板类,重载输出流“<<”。

直接上代码:

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
输出结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值