+ -
当前位置:首页 → 问答吧 → 一个不算问题的问题

一个不算问题的问题

时间:2011-12-03

来源:互联网

C/C++ code
#include<iostream>
#include<time.h>
using namespace std;

template <class Elem>
class Node{
    public:
    Elem data;
    Node *next;
    Node(const Elem &elemvel,Node * nextval = NULL)//在提供1-2个参数的情况下
    {data = elemvel;next = nextval;}
    Node(Node * nextval = NULL)//在不提供参数的情况下
    {next = nextval;}
};

template<class Elem>
class List{
    Node<Elem>* head;//头结点
    Node<Elem>* tail;//尾结点
    Node<Elem>* temp;//临时结点
    int leng;
    void init(){//初始化链表
        head = tail = new Node<Elem>(0);
        leng = 0;
    }
    void removeall(){//删除链表
        while(head){
            temp = head;
            head = head->next;
            delete temp;
        }
        leng = 0;
    }
    public:
    List(){init();}//构造
    ~List(){removeall();}//析构
    bool DestrayNode(const Elem);//删除一个结点
    void CreatNode(const Elem item){//创建一个新结点
        tail = tail->next = new Node<Elem>(item);
        leng++;
    }
    void PutList(){//输出链表
        for(temp = head->next;temp != NULL; temp = temp->next)
            cout<<temp->data<<' ';
    }
    int GetLeng()const{return leng;}//取链表的长度
    Elem GetNode(const int count);//取一个结点的值
    bool Lineinsert(const int count,const int value);//在count后插入一个结点
};

template<class Elem>
bool List<Elem>::DestrayNode(const Elem item){//删除一个结点
    if(item <=0 || item > leng)
        return false;
    else if(item == leng){
        temp = head->next;
        while(temp->next != tail)
            temp = temp->next;
        tail = temp;
        temp = temp->next;
        delete temp;
    }
    else{
        temp = head;
        for(int i = 1; i < item;i++)
            temp = temp->next;
        Node<Elem> *pNode;
        pNode = temp->next;
        temp->next = pNode->next;
        delete pNode;
    }
    leng--;
    return true;
}

template<class Elem>
Elem List<Elem>::GetNode(const int count){//取一个结点的值
    if(count >= 1 && count <= leng)
    temp = head->next;
    for(int i = 1; i < count; i++)
        temp = temp->next;
    return temp->data;
}

template<class Elem>
bool List<Elem>::Lineinsert(const int count,const int value){//增加一个结点
    if(count <= 0 || count > leng)
        return false;
    temp = head;
    for(int i = 1; i < count; i++)
    temp = temp->next;
    temp->next = new Node<Elem>(value,temp->next);
    leng++;
    return true;
}

int main()
{
    List<int> l;
    srand((unsigned)time(NULL));
    for(int i = 0; i < 10; i++)
        l.CreatNode(rand()%100);
    l.PutList();
    return 0;
}



代码本身无问题 问题是在编译的时候
比如说我在main函数中申请了个对象l
在调用函数的时候在编译器中输入"l."后面就会跟着出现类中所有的函数跟参数
我奇怪的是这段代码写完之后 输入"l."竟然发现少了3个函数 并且这3个函数都是在类外面定义的在类中定义的会自动弹出
为什么在类外定义的小时了???

作者: xuminghui382   发布时间: 2011-12-03

我现在在看,你那个鸡蛋里钻出来的是老鼠,还是小鸡,白色的晏鼠?

作者: PointertoPointer   发布时间: 2011-12-03

哎~你这么一说 我也觉的不像小鸡

作者: xuminghui382   发布时间: 2011-12-03