简体   繁体   English

我应该如何用所有不同的链表设置格式的命名空间?

[英]How should I format a namespace with all different linked lists?

I want make a very simple namespace 'list' kind of like 'std' with libraries like 'singly' and 'doubly'. 我想使用一个库(例如“ single”和“ doubly”)创建一个非常简单的命名空间“ list”(类似于“ std”)。 How can I modular the code so that I can only use variables I need to (circular vs non-circular)? 我该如何对代码进行模块化,以便仅使用需要的变量(循环与非循环)?

I imagine that I can just add a 'struct node* prev' to the struct that the list is using, but if the user decides to not do circular, it would always be 'nullptr' causing in an unnecessary variable. 我想我可以将“ struct node * prev”添加到列表使用的结构中,但是如果用户决定不进行循环,则它将始终为“ nullptr”,从而导致不必要的变量。 The second way I imagine is with having 2 different classes... 我想象的第二种方式是拥有两个不同的类...

namespace list{
    struct snode{
        int data;
        struct snode* next;
    };

    class singly{
        public:
            singly();
            singly(unsigned long long amount=0, bool circular=0, bool userCreated=0, bool empty=0);
            ~singly();

            void create(unsigned long long amount=0, bool circular=0, bool userCreated=0, bool empty=0);
            void display();
            void destroy();
        private:
            snode* HEAD;
            unsigned long long amount;

            bool empty;
            bool circular;
            snode* TAIL;
    };
}

I expect with this namespace to do the following.. 我希望使用此命名空间来执行以下操作。

include "singly.h"

using namespace list;

singly list;

list.create(5);

list.display();

list.destroy();

or by not using the namespace something else... 或者不使用命名空间...

list::singly list(7, 1, 0, 1);

list.display();

list.~list();

While this isn't an assignment or anything, I want this to be my go-to when I need to create a linked list ever in my life. 尽管这不是一项任务,也不是任何事情,但是当我需要创建一个链表时,我希望它成为我的首选。 Eventually, I want to create a namespace 'tree' and have further expansion with that. 最终,我想创建一个命名空间“树”,并进一步扩展它。 I almost want this to be as easy to use as the 'string' abstract data type. 我几乎希望它像“字符串”抽象数据类型一样易于使用。

short answer 简短答案

It's impossible 不可能

long answer 长答案

I lied(ish). 我撒谎了 Create a base strict containing data, then create snode that inherits the data, implementing the pointer of base class. 创建一个包含数据的严格条件,然后创建继承数据的snode,实现基类的指针。 When going through the list check whether it fails. 浏览列表时,检查是否失败。

struct A {
    int data;
    // make it polymorphic for the conversion
    virtual void function() {return;}
};

struct B : A {
    struct A* next;
};

int main()
{
    struct A end;
    struct B beginning;
    beginning.next = &end;
    struct B* buffer = &beginning;
    while(true) {
        buffer = dynamic_cast<struct B*>(buffer->next);
        if (buffer == nullptr) break;
    }
}

why it's pretty irrelevant 为什么很不相关

You only save at most (0 bytes) of memory per list. 每个列表最多只能保存(0字节)内存。 You save it using struct A, but struct A effectively has a function ptr/address aswell, which takes up space. 您可以使用struct A保存它,但是struct A有效地还有一个函数ptr / address,这会占用空间。 This, however may be fixed by the compiler. 但是,这可以由编译器解决。 And it takes longer to write worse code and the overall efficiency will be worse. 而且编写更差的代码需要更长的时间,并且总体效率会更差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM