开发者

Implementation of Deque in C++

开发者 https://www.devze.com 2023-02-14 23:58 出处:网络
I\'m writing an implementation of Deque as a programming exercise and it\'s not going too well at all. I\'m missing a few key function that are needed to make the test main program I was given functio

I'm writing an implementation of Deque as a programming exercise and it's not going too well at all. I'm missing a few key function that are needed to make the test main program I was given function correctly.

Here is my code so far:

#include <vector>
#include <iostream>
#include <cassert>

using namespace std;

template <class T> class DequeIterator;

template <class T>
class Deque {
public:
    typedef DequeIterator<T> iterator;

    Deque(): vecOne(), vecTwo() { }
    Deque(unsigned int size, T& initial): vecOne(size/2, initial), vecTwo(size-(size/2), initial) { }
    Deque(Deque<T> & d): vecOne(d.vecOne), vecTwo(d.vecTwo) { }

    T & operator[](unsigned int);
    T & front();//
    T & back();//
    bool empty(){ return vecOne.empty() && vecTwo.empty(); }
    iterator begin() { return iterator(this,0); }
    iterator end() { return iterator(this, size ()); }
    void erase(const iterator &);
    void erase(const iterator &, const iterator &);
    void insert(const iterator &, const T &);
    int size() { return vecOne.size() + vecTwo.size(); }
    void push_front(const T & value) { vecOne.push_back(value); }
    void push_back(const T & value) {vecTwo.push_back(value); }
    void pop_front();
    void pop_back();
protected:
    vector<T> vecOne;
    vector<T> vecTwo;
};

template <class T>//
T & Deque<T>::front()//returns the first element in the deque
{
    if (vecOne.empty())
        return vecTwo.front();
    else
        return vecOne.back();
}

template <class T>//
T & Deque<T>::back()//returns the last element in the deque
{
    if (vecOne.empty())
        return vecTwo.back();
    else
        return vecOne.front();
}

template <class T>//
T & Deque<T>::operator[] (unsigned int index)
{
    int n = vecOne.size();
    if (index < n)
        return vecOne [ (n-1) - index ];
    else
        return vecTwo [ index - n ];
}

template <class T>//
Deque<T>::iterator DequeIterator<T>::operator ++ (int)
{
    Deque<T>::iterator clone(theDeque, index);
    index++;
    return clone;
}


template <class T>//
void Deque<T>::pop_front()
{

}

templ开发者_StackOverflowate <class T>//
void Deque<T>::pop_back()
{

}

template <class T>//
void Deque<T>::erase (const iterator & itr)
{
    int index = itr.index;
    int n = vecOne.size();
    if (index < n)
        vecOne.erase (vecOne.begin() + ((n-1) - index));
    else
        vecTwo.erase (vecTwo.begin() + (n - index));
}

template <class T>//
void Deque<T>::erase (const iterator &, const iterator &)
{

}

template <class T>//
void Deque<T>::insert(const iterator &, const T &)
{

}

template <class T>
class DequeIterator {
    friend class Deque<T>;
    typedef DequeIterator<T> iterator;
public:
    DequeIterator(): theDeque(0), index(0) { }
    DequeIterator(Deque<T> * d, int i): theDeque(d), index(i) { }
    DequeIterator(const iterator & d): theDeque(d.theDeque), index(d.index) { }

    T & operator*() { return (*theDeque)[index]; }
    iterator & operator++(int) { ++index; return *this; }
    iterator operator++();
    iterator operator--(int) { --index; return *this; }
    iterator & operator--();
    bool operator==(const iterator & r) { return theDeque == r.theDeque && index == r.index; }
    bool operator!=(const iterator & r) { return theDeque == r.theDeque && index != r.index; }
    bool operator< (const iterator & r) { return theDeque == r.theDeque && index < r.index; }
    T & operator[](unsigned int i) { return (*theDeque) [index + i]; }
    iterator operator=(const iterator & r) { theDeque = r.theDeque; index = r.index; }
    iterator operator+(int i) { return iterator(theDeque, index + i); }
    iterator operator-(int i) { return iterator(theDeque, index - i); }
protected:
    Deque<T> * theDeque;
    int index;
};
main()
{
    Deque<int> d;

    d.push_back(10);
    d.push_back(20);
    assert(d.front() == 10);
    assert(d.back() == 20);

    d.push_front(1);
    d.push_front(2);
    d.push_front(3);
    assert(d.front() == 3);
    assert(d.back() == 20);

    d.pop_back();
    d.pop_back();
    d.pop_back();
    assert(d.front() == 3);
    assert(d.back() == 2);

    d.push_back(1);
    d.push_back(0);

    Deque<int>::iterator i;
    int counter = 3;
    for (i = d.begin(); i != d.end(); i++)
        assert(*i == counter--);

    for (counter = 0; counter < d.size(); counter++)
        assert(d[counter] == d.size()-counter-1);

    i = d.begin() + 3;
    Deque<int>::iterator j(i), k;
    k = j = i - 2;
    assert(*k == 2);

    for (i = d.begin(); not(i == d.end()); ++i)
        cout << *i << " ";
    cout << endl;

    d.erase(d.begin()+3);
    //d.erase(d.begin(), d.begin()+2);
    assert(d.size() == 1);
    assert(d[0] == 1);

    Deque<int> c(d);
    c.front() = 3;
    assert(c.back() == 3);

    c.push_front(1);
    c.insert(c.begin(), 0);
    c.insert(c.begin()+2, 2);

    for (i = c.begin(); not(i == c.end()); ++i)
        cout << *i << " ";
    cout << endl;

    for (counter = 0; counter < c.size(); counter++)
        assert(c[counter] == counter);

    cout << "SUCCESS\n";
}

I was wondering if someone could tell me my function from line 66 is returning:

expected constructor, destructor, or type conversion before 'DequeIterator'

Because I'm not sure what I'm doing wrong in it. Also, if someone would be kind enough to give me an example of the pop_front() function so that I can use it to create the pop_back() function as well, that would be great. Lastly, I have on of the erase functions completed but I am not sure how to go about creating the second one, which basically erases a value within the range of two iterators, it is referenced in line 176.

Any help would be greatly appreciated. Thank you in advance.


As for the error, you probably need a typename before Deque<T>::iterator on that line.

typename Deque<T>::iterator DequeIterator<T>::operator++(int)


I think it is a great programming exercise to implement deque. But a prerequisite is to implement vector and list. deque is one of the most complicated std::containers to implement. You should start with one of the simpler ones (vector and list).


Well, you get the error on line 65 because you return an object of a class that hasn't been defined. You only have the forward declaration (prototype) for class DequeIterator, not the implementation.


void pop_back() {
  vecTwo.pop_back();
}

void pop_front() {
  vecOne.pop_back();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号