3.DataStructure Cp2 T1 Singly-Linked Node

来源:互联网 发布:手机记日记什么软件好 编辑:程序博客网 时间:2024/06/06 02:54

3. DataStructure Cp2 T1 Singly-Linked Node

this one is quite similar with doubly-linked linknode

singlelinknode.h

#include"stdafx.h"#include"targetver.h"#ifndef singlelinknode_h#define singlelinknode_h#include<iostream>using namespace std;#pragma oncetemplate <class type>class list{public://virtual function shall be put into public    virtual void clear() = 0;    virtual int length()const = 0;    virtual void insert(int i, const type&x) = 0;    virtual void remove(int i) = 0;    virtual int search(const type&x)const = 0;//一开始以为是用来调用结构中的数字    virtual type visit(int i)const = 0;//加在后面不会修改数据    virtual void traverse()const = 0;    virtual ~list();};class OutOfBound {};class IllegalSize {};template <class type>class singlelinknode :public list<type>//<type>{private:    //part I    struct node//这个struct实际上是个class,注意构造函数    {        type data;        node*next;        node() :next(NULL){};        node(const type& x, node*m)        {            data = x; next = m;/*修改完毕*/        }        ~node() {};//我就说不用析构函数    };    //partII    node*head, *tail;    int currentlength;    //partIII 不写成内置类编译怎么都不通过    node* move(int i)const    {        if (i<0 || i>currentlength)            throw OutOfBound(); //at first,this hacebeen ignorant        node*tmp = head;        while (i>0)/*check*/        {            tmp = tmp->next;            i--;        }//应该看看是不是移动到第i个节点了,有个问题,head是第0还是第一?        return tmp;    }public:    singlelinknode();    ~singlelinknode();    void clear();    int length()const;    void insert(int i, const type&x);    void remove(int i);    int search(const type&x)const;//一开始以为是用来调用结构中的数字    type visit(int i)const;//加在后面不会修改数据    void traverse()const;};#endif

singlelinknode.cpp

#include "stdafx.h"#include "singlelinknode.h"template<class type>//不知道写什么?为head tail申请空间,随后再连一起singlelinknode<type>::singlelinknode(){    head = new node;    tail = new node;    head->next = tail;/*check*/    currentlength = 0;//忘写了}template<class type>singlelinknode<type>::~singlelinknode(){    clear();    delete head;}template<class type>void singlelinknode<type>::clear(){    node*tmp = head->next;    head->next = tail;    while (tmp != tail)//跟课本上不太一样    {        node*p = tmp;        tmp = tmp->next;/*check*/        delete p;    }    currentlength = 0;//注意清零,忘写了}template<class type>int singlelinknode<type>::length() const{    return currentlength;}template<class type>//插到i前面还是i后面? 是i和i-1void singlelinknode<type>::insert(int i, const type & x){    node*p = move(i-1);    node*tmp = new node(x, p->next);    p->next = tmp;    currentlength++;//注意清零,忘写了}template<class type>void singlelinknode<type>::remove(int i){    char*tmp = move(i-1);/*diffenence from doubly-linked is                         we can only start from the very beginning*/    char*p = tmp->next;    tmp->next = tmp->next->next;/*check*/    delete p;//曾经在这一块有过疑问,    //事实上删除指针会自动启用对应的析构函数    currentlength--;}template<class type>int singlelinknode<type>::search(const type & x) const{    node*tmp = head;    int i = 0;    while (tmp != tail && tmp->data != x)    {        tmp = tmp->next;        i++;    }    if (tmp == tail)    {        cout << "none exit\t\n";        return -1;    }    else return i;//same question,should i start from 0?    return 0;}template<class type>type singlelinknode<type>::visit(int i) const{    if (i<0 || i>currentlength)throw OutOfBound();//at first,this hacebeen ignorant    node*tmp = head;    int n = 0;    while (n < i)    {        tmp = tmp->next;        n++;    }    return tmp->data;//我也不知道应该是前一个还是后一个}template<class type>void singlelinknode<type>::traverse() const{    node*tmp = head - next;    while (node != tail)    {        cout << tmp->data << endl;        tmp = tmp->next;    }}