如何在C中转换此出列C ++代码? [英] How Do I Convert This Dequeue C++ Code In C ?

查看:66
本文介绍了如何在C中转换此出列C ++代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <exception>
#include <functional>
#include <iostream>
#include <stdexcept>
#include <string>

template<typename T>
class DEQeue
{
public:

    enum class Iterate { BackToFront, FrontToBack };

    DEQeue()
    {
        first_ = NULL;
        last_ = NULL;
    }

    void push_front(const T& item)
    {
        if (first_ == NULL)
        {
            first_ = new Node();
            first_ -> item = item;
            first_ -> prev = NULL;
            first_ -> next = NULL;
            last_ = first_;
        }
        else
        {
            Node* oldFirst = first_;
            first_ = new Node();
            first_ -> item = item;
            first_ -> prev = NULL;
            first_ -> next = oldFirst;
            oldFirst -> prev = first_;
        }
    }

    void push_back(const T& item)
    {
        if (last_ == NULL)
        {
            last_ = new Node();
            last_ -> item = item;
            last_ -> prev = NULL;
            last_ -> next = NULL;
            first_ = last_;
        }
        else
        {
            Node* oldLast = last_;
            last_ = new Node();
            last_ -> item = item;
            last_ -> prev = oldLast;
            last_ -> next = NULL;
            oldLast -> next = last_;
        }
    }

    T pop_front() 
    {
        if (isEmpty()) throw std::out_of_range("OMFG! The DEQeue was empty.");
        T item = first_ -> item;
        first_ = first_ -> next;
        if (first_ == NULL) last_ = NULL;
        return item;
    }

    T pop_back() 
    {
        if (isEmpty()) throw std::out_of_range("OMFG! The DEQeue was empty.");
        T item = last_ -> item;
        last_ = last_ -> prev;
        if (last_ == NULL) first_ = NULL;
        return item;
    }

    bool isEmpty()
    {
        return first_ == NULL;
    }

    void each(std::function<void(T)> function, Iterate it = Iterate::FrontToBack)
    {
        if (it == Iterate::FrontToBack)
        {
            Node* node = first_;
            while (node != NULL)
            {
                function(node -> item);
                node = node -> next;
            }
        }
        else 
        {
            Node* node = last_;
            while (node != NULL)
            {
                function(node -> item);
                node = node -> prev;
            }
        }
    }

private:
    struct Node
    {
        T item;
        Node* prev;
        Node* next;
    };

    Node* first_;
    Node* last_;
};

int main(int argc, char const *argv[])
{
    enum class Latice { sc, bcc, fcc, hcp };
    DEQeue<Latice> deqi;

    for (int i = 10000000; i > 0; i --)
        deqi.push_back(Latice::sc);

    deqi.each([](Latice lt){
        switch (lt)
        {
            case Latice::sc:
                std::cout << "sc";
                break;
            case Latice::fcc:
                std::cout << "fcc";
                break;
            case Latice::bcc:
                std::cout << "bcc";
                break;
            case Latice::hcp:
                std::cout << "hcp";
                break;
        }
        std::cout << std::endl;
    });

    return 0;
}

推荐答案

黄金法则是,类获取结构,类函数获取this参数首先添加,其中给出了一个指向结构的指针。



模板无法解决。所以你必须使用指针。也许工会有帮助。



放弃这个功能不是最好的主意,但也许你需要它。
The golden rule is, that a class gets a struct and the class functions getting a "this" parameter at first added, in which a pointer to the structure is given.

The template cant be solved. So you must use a pointer. Maybe an union helps.

It isnt the best idea to loose that feature, but maybe you need it.


这篇关于如何在C中转换此出列C ++代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆