简体   繁体   English

使用Stack类实现List类

[英]Implementing List class with using Stack class

I'm trying to write a Interpreted programming language like Python, so i need a List class for storing 'address of' functions and variables. 我正在尝试编写像Python这样的解释型编程语言,因此我需要一个List类来存储“地址”函数和变量。 I'm implemented Stack class for implementing List class: 我实现了用于实现List类的Stack类:

typedef unsigned int UIntegerP; //This type for storing addresses
#define Free 0x0

template <typename T> class Stack{ 
    public:
        unsigned long UsedBSize; // You can use that like End Of Stack (EOS)

        Stack(void){
            this->BSize = 0; this->UsedBSize = 0;
            this->Buffer = new T;           
        }
        ~Stack(void){
            delete this->Buffer;
        }

        inline void Push(T Variable){ 
            if(this->UsedBSize == this->BSize){ 
                this->BSize++; 
            } this->Buffer[this->UsedBSize] = Variable; this->UsedBSize++;
        }    
        inline T Pop(bool IsProtected = false){ 
            if(IsProtected){
                return this->Buffer[this->UsedBSize]; 
            }else{
                this->UsedBSize--; T Element = this->Buffer[this->UsedBSize]; this->Buffer[this->UsedBSize] = Free; 
                return Element;
            }   
        }   
    private:
        T *Buffer;
        unsigned long BSize; 

};

And this is the class i want to implement: 这是我要实现的类:

class List{
    private:
        Stack<UIntegerP> *stack = new Stack<UIntegerP>; //A stack for storing variable addresses

    public:
        ~List(void){
            delete this->stack;
        }

        List(Stack<UIntegerP> Elements){ 
            while(Elements.UsedBSize != 0){
                this->stack->Push(Elements.Pop());
            }
        }

        List(Stack<UIntegerP> *Elements){
           while(Elements->UsedBSize != 0){
               this->stack->Push(Elements->Pop());
           }
        }

        UIntegerP Get(unsigned long Size); //Get Address with Index number
        UIntegerP Set(unsigned long Size, UIntegerP Address); //Set Address with Index number
};

I will use this List class for implementing Python like dictionaries. 我将使用此List类来实现像字典一样的Python。 UIntegerP type is required for Variable class. 变量类需要UIntegerP类型。 How i can implement this two functions? 我如何实现这两个功能?

Assuming your stack exposes only the Push and Pop functions, then you can't efficiently implement list with indexing on top of that. 假设您的堆栈仅公开PushPop函数,那么您将无法高效地实现带有索引的列表。

If you're programming in normal C++ style, then the basic data structure would be a dynamic array or a linked list . 如果您以普通的C ++风格进行编程,则基本数据结构将是动态数组链表 You can then build a stack on top of those. 然后,您可以在其上构建堆栈。 But note that indexing in a linked list is going to be slow (linear time complexity). 但是请注意,在链表中建立索引的速度会很慢(线性时间复杂度)。

If you're programming in a functional style, then the basic structure is "list", which is an immutable singly-linked list and it's effectively the same as immutable stack. 如果您以功能性风格进行编程,则基本结构是“列表”,它是一个不可变的单链接列表,实际上与不可变的堆栈相同。 But again, indexing with that is going to be slow. 但是同样,用它建立索引将很慢。


Also note that your Stack is implemented incorrectly: you're allocating memory for a single T , but then you assume you can use that for an unlimited number of T s. 还要注意,您的Stack实现不正确:您正在为单个T分配内存,但随后您假定可以将其用于无限多个T You either need to go the linked list route: allocate a new node for each item and connect the nodes with pointers; 您要么需要执行链表路由,要么为每个项目分配一个新节点,然后将这些节点与指针连接; or you need to go the dynamic array route: allocate an array of a certain size and when it gets too small, reallocate it. 或者您需要使用动态数组路线:分配一定大小的数组,当数组变得太小时,重新分配它。

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

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