Link
博客专区 > MtrS 的博客 > 博客详情
Link
MtrS 发表于8个月前
Link
  • 发表于 8个月前
  • 阅读 9
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

#include <iostream>

enum {kIsSmaller,kIsLarger, kIsSame};

class Data{

    public:
        Data(int newVal):value(newVal){}
        ~Data(){}
        int compare(const Data&);
        void show(){std::cout << value << "\n";}
    private:
        int value;
};

int Data::compare(const Data& otherData){
    if(value < otherData.value)
        return kIsSmaller;
    if(value > otherData.value)
        return kIsLarger;
    else
        return kIsSame;
}

class Node;
class HeadNode;
class TailNode;
class InternalNode;

class Node{
    public:
        Node() {}
        virtual ~Node(){}
        virtual Node* insert(Data*data) =0;
        virtual void show() = 0;
    private:
};

class InternalNode: public Node{

    public:
        InternalNode(Data*dara, Node*next);
        virtual ~InternalNode() {delete next;delete data;}
        virtual Node *insert(Data*data);
        virtual void show(){
            data -> show();
            data -> show();
        }

    private:
        Data *data;
        Node *next;
};

InternalNode::InternalNode(Data* newData, Node *newNext):
    data(newData), next(newNext){
    }
Node *InternalNode::insert(Data *otherData){

    int result = data->compare(*otherData);
    switch(result){
        case kIsSame:
        case kIsLarger:{
                           InternalNode *dataNode = new InternalNode(otherData, this);
                           return dataNode;
                       }
        case kIsSmaller:
                       next = next->insert(otherData);
                       return this;
    }
}

class TailNode:public Node{
    public:
        TailNode(){}
        virtual ~TailNode(){}
        virtual Node *insert(Data *data);
        virtual void show(){}
    private:
};

Node * TailNode::insert(Data *data){
    InternalNode *dataNode = new InternalNode(data,this);
    return dataNode;
}

class HeadNode:public Node{
    public:
        HeadNode();
        virtual ~HeadNode() {delete next;}
        virtual Node * insert(Data *data);
        virtual void show(){next ->show();}
    private:
        Node *next;
};

HeadNode::HeadNode(){
    next = new TailNode;
}

Node * HeadNode::insert(Data*data){
    next = next -> insert(data);
    return this;
}

class LinkedList{

    public:
        LinkedList();
        ~LinkedList(){delete head;}
        void insert(Data*data);
        void showAll() {head -> show();}
    private:
        HeadNode * head;

};

LinkedList::LinkedList(){
    head = new HeadNode;
}
void LinkedList::insert(Data* pData){
    head -> insert(pData);
}

int main(){
    Data * pData;
    int val;
    LinkedList ll;
    while(true){

        std::cout << "What value (0 to stop)?";
        std::cin >> val;

        if (!val)
            break;
        pData = new Data(val);
        ll.insert(pData);
    }
    ll.showAll();
    return 0;
}
共有 人打赏支持
粉丝 29
博文 468
码字总数 351660
×
MtrS
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: