顺序链表类模板的实现

编写的顺序链表类模板存放元素Node,类模板定义了一下成员:

  • 成员变量:
    • 表示链表表头的指针:Node *head
    • 链表长度:length
    • 链表大小:size_
  • 成员函数
    • 默认构造函数
    • 接受迭代器的构造函数
    • 在链表尾部添加成员
    • 删除链表第n个成员,返回删除成员的值
    • 查找指定值元素,找到则返回符合要求的第一个元素的指针,否则返回NULL
    • 返回表头
  • 模板代码如下:
#ifndef LINERLISTSEQ_H
#define LINERLISTSEQ_H
#include <memory>
#include <functional>
typedef std::size_t size;

template<typename Node> struct LinerList_seq{
public:
    //一般构造函数,链表长度为0,步长为10
    LinerList_seq(size n = 10):length(0),step(n){
        std::allocator<Node> p;
        head = p.allocate(step);
        size_ = step;
    }
    //传递两个迭代器的构造函数版本,步长必须大于传递的迭代器长度
    template<typename It> explicit LinerList_seq(It beg,It end,size n = 10):step(n){
        std::allocator<Node> p;
        head = p.allocate(step);
        size_ = step;
        std::uninitialized_copy(beg,end,head);
        step = n;
        length = 0;
        for (;beg!=end;beg++)
        {
            length++;
        }
    }

    //往链表里添加一个节点,当分配空间不足时,需重新分配空间,分配步长为step,此时链表空间大小size_为size_+step,将原空间数据赋值过空间,并将新元素插入到链表最后
    void add_Node(Node node);

    //删除链表第n个元素,返回该元素值
    Node delete_n(size n);

    //查找元素,找到则返回符合要求的第一个元素的指针,否则返回NULL
    Node *find_(Node no1,std::function<bool(Node n1,Node n2)> compare_);

    //查找元素,找到所有符合要求的元素的,返回指向第一个元素的迭代器
    //virtual Node &find(Node no1,std::function<bool(Node n1,Node n2)> compare_);

    //常看链表的第n个元素的值
    Node check(size n){
        return *(head+n-1);
    }

    //返回表头
    const Node *get_head(void ){
            return head;
        }

    //设置步长
    void set_step(size n){
        step = n;
    }

    //返回步长
    size get_step(void){
        return step;
    }

    //返回链表长度
    size get_length(void){
        return length;
    }

    //查看链表是否为空
    bool isempty(void){
        return (length ==0)
    }

    //返回链表大小
    size get_size(void){
        return size_;
    }

    //add_element(T t)
private:
    Node* head;
    size length;
    size size_; 
    size step;
};
//template<typename Node>   virtual Node &LinerList_seq<Node>::find(Node no1,std::function<bool(Node n1,Node n2)> compare_){
//
//  return NULL;
//}
template<typename Node> Node *LinerList_seq<Node>::find_(Node no1,std::function<bool(Node n1,Node n2)> compare_){
    Node *temp = head;
    while(temp != head+length)
        if (compare_(*(temp++) ,no1) )
        {
            return --temp;
        }
        return NULL;
}

template<typename Node> Node LinerList_seq<Node>::delete_n(size n){
    if (n > length)
    {
        std::cerr << "out of range";
        return NULL;
    }
    std::allocator<Node> p;
    Node temp = check(n);
    Node *de = head+n-1;
    p.destroy(de);
    for (;n<length;n++)
    {
        *de = *(de+1);
        de++;
    }
    p.destroy(de);
    length--;
    return temp;
}

template<typename Node> void LinerList_seq<Node>::add_Node(Node node){
    std::allocator<Node> p;
    if (length == size_){           
        Node* head_temp = p.allocate(size_+step);
        std::uninitialized_copy(head, head+length, head_temp);

        for (Node* t = head; t <= head+length;t++)
        {
            p.destroy(head);
        }
        head = head_temp;
        size_ = size_ + step;
    }
    p.construct(head+length, node);
    length++;
}

#endif
  • 测试函数如下:
#include "stdafx.h"
#include "Liner_list_seq.h"
#include <vector>
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
    int i[9]={0,1,2,3,4,5,6,7,8};
    //std::vector<int> j(i,i+8);
    LinerList_seq<int>list_(i, i+9);
    //const int *m = list_.get_head();
    std::size_t length = list_.get_length();
    int q =9;
    list_.add_Node(q);
    int p =10;
    list_.add_Node(p);
    int &&mm = list_.check(1);

    int nn = list_.delete_n(1);

    for(int i = 1;i <= list_.get_length();i++){
        std::cout << list_.check(i) << std::endl;
    }
    int * ii = list_.find_(2,std::equal_to<int>());
    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、付费专栏及课程。

余额充值