A. Ite
A. Ite

Reputation: 193

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. I'm implemented Stack class for implementing List class:

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. UIntegerP type is required for Variable class. How i can implement this two functions?

Upvotes: 0

Views: 118

Answers (1)

svick
svick

Reputation: 244757

Assuming your stack exposes only the Push and Pop functions, then you can't efficiently implement list with indexing on top of that.

If you're programming in normal C++ style, then the basic data structure would be a dynamic array or a linked list. 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 Ts. 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.

Upvotes: 1

Related Questions