Zmiana listy jednokierunkowej na dwukierunkowa

2015-01-06 11:17
0

Witam, mam prosbe czy moglibyscie mi pomóc, jak zmienic ta liste jednokierunkowa na dwukierunkowa wiem ze trzeba dodac node<T> *ogon; tylko nie wiem jak go zaimplementowac w funkcjach prosze o pomoc :)

 #include <iostream>
using namespace std;

template <class T>
struct node
{
    T data;
    node<T> *next;
    node<T> *prev;
};

template <class T>
class Kontener
{

    public:
        //constructs a new empty Kontener
        Kontener()
        {
            glowa = new node<T>;
            glowa->next = glowa;
            glowa->prev = glowa;
        };

        //constructs a new jp_list that is a copy of an existing list
        Kontener(const Kontener<T>& rt_side)
        {
            glowa = new node<T>;
            glowa->next = glowa;
            glowa->prev = glowa;

            node<T> *crt_ptr = rt_side.glowa->next;
            while(crt_ptr != rt_side.glowa)
            {
                push_back(crt_ptr->data);
                crt_ptr = crt_ptr->next;
            }
        };

        //adds a data node to the front of the list
        void push_front(T nw_data)
        {
            node<T> *temp = new node<T>;
            temp->data = nw_data;

            temp->next = glowa->next;
            glowa->next->prev = temp;

            temp->prev = glowa;
            glowa->next = temp;
        };

        //adds a data node to the end of the list
        void push_back(T nw_data)
        {
            node<T> *temp = new node<T>;
            temp->data = nw_data;

            glowa->prev->next = temp;
            temp->prev = glowa->prev;

            temp->next = glowa;
            glowa->prev = temp;
        };

        //removes the first node and returns the data
        T pop_front()
        {
            node<T> *temp = glowa->next;
            T temp_data = glowa->next->data;

            glowa->next = temp->next;
            temp->next->prev = glowa;

            delete temp;

            return temp_data;
        };

        //removes the last node and returns the data
        T pop_back()
        {
            node<T> *temp = glowa->prev;
            T temp_data = glowa->prev->data;

            glowa->prev = temp->prev;
            temp->prev->next = glowa;

            delete temp;

            return temp_data;
        };

        //resturns the size of the list
        int size()
        {
            int size = 0;
            node<T> *crt_ptr; //pointer to current node

            crt_ptr = glowa->next;
            while(crt_ptr != glowa)
            {
                size += 1;
                crt_ptr = crt_ptr->next; //advance to the next node then loop
            }

            return size;
        };

        //prints out all the data in the list
        void display_all()
        {
            node<T> *crt_ptr = glowa->next;

            for(int i = 0; crt_ptr != glowa; i++)
            {
                cout << "Node " << (i+1) << ": " << crt_ptr->data << endl;

                crt_ptr = crt_ptr->next;
            }
        };

        Kontener& operator= (const Kontener& rt_side)
        {
            if(this == &rt_side)
                return *this;
            node<T> *crt_ptr = glowa->next;

            //empty this list so the rt_side can be coppied in
            while(crt_ptr != glowa)
            {
                crt_ptr = crt_ptr->next;
                pop_front();
            }

            crt_ptr = rt_side.glowa->next;

            while(crt_ptr != rt_side.glowa)
            {
                push_back(crt_ptr->data);
                crt_ptr = crt_ptr->next;
            }

            return *this;
        };

        virtual ~Kontener()
        {
            int list_size = size();

            for(int i = 0; i < list_size; i++)
            {
                pop_front();
            }

            delete glowa;
        };

    private:
        node<T> *glowa;

};

#endif
edytowany 2x, ostatnio: furious programming, 2015-01-06 18:11

Pozostało 580 znaków

2015-01-06 11:34
0

ile placisz zeby to zrobic za Ciebie?

Już się ciesz ;P - furious programming 2015-01-06 18:11
a to czemu :)? - fasadin 2015-01-06 19:35

Pozostało 580 znaków

Liczba odpowiedzi na stronę

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0