1

EI'm doing a project on data structure class, which have to implement different types of structures.
Such as arrays, linked, doubly linked, circular etc ...
Each of these structures uses a type, either stack, queue, or list.

Example:
VectorStructure.h

template<typename T>
class VectorStructure{
public:  
    int addOnPosition(T element, int pos);
    int addOnBeginning(T element);
    int add(T element);
    int addElementOrdered(T element);
    T removeFromPos(int pos);
    T removeFromBeginning();
    T remove();
    T removeElement(T element);
}

Each implementation of those types contains code exactly like the others.
Stack: is a LIFO structure, only uses methods: add(T element) and remove();
Queue: is a FIFO structure, only uses methods: add(T element) and removeFromBeginning();
List: is a dynamic array, can uses any of those methods and some extras.

My idea is: Implement all those functions on a base class, and make those types use only the needed methods of the base. I thought i could use inheritance, but then a stack could access an unallowed function from base class since its it "child" Also thought i could use abstract class, but, in order to compile i should implement all methods contained on the abstract class.

The types (they're only examples, there are some other methods that are the same on all types):
List.h

template<typename T>
class List{
public:
    int addOnPosition(T element, int pos);
    int addOnBeginning(T element);
    int add(T element);
    int addElementOrdered(T element);
    T removeFromPos(int pos);
    T removeFromBeginning();
    T remove();
    T removeElement(T element);

Stack.h

template<typename T>
class Stack{
public:
    int add(T element);
    T remove();

Queue.h

template<typename T>
class Queue{
public:
    int add(T element);
    T removeFromBeginning();

Is there is any way to implement this idea?

1 Answers1

1

I'm not sure I understand you question correctly, but I guess you're looking to avoid code duplication - how about passing underlying container types as template typenames, like the STL does with container adapters such as std::stack?

template<typename T> struct BaseContainer {
    void push_front(T);
    void push_back(T);
    T pop_front();
    T pop_back(); 
};

template<typename T, typename TUnderlying = BaseContainer> class Stack {
    TUnderlying baseContainer;
    public:
        void push(T item) { baseContainer.push_back(item); }
        T pop() { return baseContainer.pop_back(); }
};

template<typename T, typename TUnderlying = BaseContainer> class Queue {
    TUnderlying baseContainer;
    public:
        void enqueue(T item) { baseContainer.push_back(item); }
        T dequeue() { return baseContainer.pop_front(); }
};
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • See also: http://stackoverflow.com/questions/3873802/what-are-containers-adapters-c Basically, there are sequence containers (vector, deque, list, etc) and adapters (stack, queue, etc). For instance the stack uses any sequence container to provide a LIFO interface, etc. – Alexandre C. Sep 26 '13 at 18:55
  • yeah, i think that could work! in this case, where the variables should be instantiated, each one on its own class or on basecontainer? – Cristian Llorente Sep 26 '13 at 19:27