链表模板类

/*
	LinearList.h是基类
	Node.h是结点类
	SingleList.h是链表类
	SingleList.cpp是SingleList.h的具体实现
	main.cpp是测试
*/
//LinearList.h
using namespace std;
template <typename T>
class LinearList
{
    public:
    LinearList()
    {
        ;
    }
    ~LinearList()
    {
        ;
    }
    virtual bool IsEmpty() const = 0;
    virtual int Length() const = 0;
    virtual bool Find(int, T&)const = 0;
    virtual int Search(const T&) const = 0;
    virtual bool Insert(int, const T&) = 0;
    virtual bool Delete(int) = 0;
    virtual bool Update(int, const T&) = 0;
    virtual void Output(ostream&) const = 0;
};



//Node.h
template <typename T>
class Node
{
    public:
    Node()
    {
        ;
    }
    Node(const T &x)
    {
        data = x;
    }
    ~Node()
    {
        ;
    }
    T data;
    Node<T> *next;
};



//SingleList.h
#include "LinearList.h"
#include "Node.h"
template <typename T>
class SingleList:public LinearList<T>
{
    public:
    SingleList()
    {
        head = new Node<T>();
        head->next = NULL;
    }
    ~SingleList()
    {
        if (head)
        {
            delete head;
        }
    }
    bool IsEmpty() const;
    int Length() const;
    bool Find(int, T&)const;
    int Search(const T&) const;
    bool Insert(int, const T&);
    bool Delete(int);
    bool Update(int, const T&);
    void Output(ostream&) const;
    bool Append(const T&);
    private:
    Node<T> *head;
    int length;
};
#include "SingleList.cpp"



//SingleList.cpp
#include <conio.h>
template <typename T>
bool SingleList<T>::IsEmpty() const
{
    return (length == 0);
}
template <typename T>
int SingleList<T>::Length() const
{
    return length;
}
template <typename T>
bool SingleList<T>::Find(int k, T &x)const
{
    if (k < 0 || k > length - 1)
    {
        return false;
    }
    Node<T> *p = head->next;
    int i = 0;
    while ( p && (i < k) )
    {
        p = p->next;
        ++i;
    }
    x = p->data;
    return true;
}
template <typename T>
int SingleList<T>::Search(const T &x) const
{
    Node<T> *p = head->next;
    int index = 0;
    while (p)
    {
        if (p->data == x)
        {
            return index;
        }
        p = p->next;
        ++index;
    }
    return false;
}
template <typename T>
bool SingleList<T>::Insert(int k, const T &x)
{
    if (k < 0 || k > length - 1)
    {
        return false;
    }
    Node<T> *p = head;
    Node<T> *temp = new Node<T>();
    temp->data = x;
    int i = 0;
    while ( (i < k) && p )
    {
        p = p->next;
        ++i;
    }
    temp->next = p->next;
    p->next=temp;
    return true;
}
template <typename T>
bool SingleList<T>::Delete(int k)
{
    if (k < 0 || k > length - 1)
    {
        return false;
    }
    Node<T> *p = head;
    Node<T> *temp;
    int i = 0;
    while (i < k)
    {
        p = p->next;
        ++i;
    }
    temp = p->next;
    p->next = temp->next;
    delete temp;
    return true;
}
template <typename T>
bool SingleList<T>::Update(int k, const T &x)
{
    if (k < 0 || k > length - 1)
    {
        return false;
    }
    Node<T> *p = head;
    int i = 0;
    while (i < k)
    {
        p = p->next;
        ++i;
    }
    p->next->data = x;
    return true;
}
template <typename T>
void SingleList<T>::Output(ostream &os) const
{
    Node<T> *p = head->next;
    while (p != NULL)
    {
        os << p->data << " ";
        p = p->next;
    }
}
template <typename T>
bool SingleList<T>::Append(const T &x)
{
    Node<T> *p = head;
    while (p->next)
    {
        p = p->next;
    }
    Node<T> *temp = new Node<T>(x);
    temp->next = NULL;
    p->next = temp;
    ++length;
    return true;
}



//main.cpp
#include <iostream>
#include "SingleList.h"
using namespace std;
int main(void)
{
    SingleList<int> *sl = new SingleList<int>();
    for (int i = 0; i < 10 ; ++i)
    {
        sl->Append(i);
    }
    sl->Output(cout);
    cout << endl;
    cout << sl->Length() << endl;
    cout << sl->IsEmpty() << endl;
    int temp;
    cout << sl->Find(9, temp) << endl;
    cout << temp << endl;
    cout << sl->Search(15) << endl;
    cout << sl->Insert(9, -1) << endl;
    sl->Output(cout);
    cout << endl;
    sl->Delete(9);
    sl->Output(cout);
    cout << endl;
    sl->Update(10 ,666);
    sl->Output(cout);
    cout << endl;
    return 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、付费专栏及课程。

余额充值