خرید بک لینک

Vote count: -1

So I have my linked list working as whole, I had to add an iterator in to step through the linked list and print each element. The issue is it crashes when it reaches the last element saying that the retu data in the function "getData()" is a nullptr.

Main.cpp

int main()
{

    MyList<int> listTest;
    MyList<std::string> listTest2;

    listTest.testPrint();
    int i = 0;
    for (i =0; i < 10; i++)
    {
        listTest.push_back(i);
    }

    listTest.testPrint();

    for (i; i < 20; i++)
    {
        listTest.push_front(i);
    }
    listTest.testPrint();


//Here is the iterator
    for (MyList<int>::iterator iter = listTest.begin(); iter != listTest.end(); iter++)
    {
        std::cout << (*iter) << " ";

    }
}

This is my Header file.

#ifndef MYLIST_H
#define MYLIST_H
#include <iostream>


template <class T>
class Node {

    private:
        T data;
        Node<T>* next;
    public:
        Node<T>();
        Node<T>(Node<T> *p, T&);
        void setNext(Node<T> *p);
        void setData(T&);
        T getData();
        Node<T> * getNext();
};

template <class T>
class MyList {

    private:
        Node<T> *head;
        Node<T> *tail;
    public:
        MyList();
        //MyList(MyList&);
        ~MyList();
        void push_front(T&);
        void push_back(T&);
        void pop_front();
        void pop_back();
        bool isEmpty();
        void testPrint();

        class iterator;
        friend class iterator;
        class iterator {

            private:
                Node<T> *current;
                Node<T> *last;
            public:
                iterator()
                {
                    current = NULL;
                    last = NULL;    
                }
                iterator(Node<T>* nCurr, Node<T>* nLast)
                {
                    current = nCurr;
                    last = nLast;
                }
                const T operator*() const
                {
                    retu current->getData();
                }
                iterator& operator++(int)
                {
                    iterator res = *this;
                    if (current)
                    {
                        current = current->getNext();
                    }
                    retu res;
                }
                bool operator!=(iterator& itr)
                {
                    if (current != itr.current)
                    {
                        retu true;
                    }
                    else
                    {
                        retu false;
                    }
                }


        };
        iterator& begin()
        {
            retu iterator(head, tail);
        }
        iterator& end()
        {
            retu iterator(NULL, tail);
        }


};


template<class T>
Node<T>::Node()
{
    next = NULL;
}

template<class T>
Node<T>::Node(Node<T>* p, T & nData)
{
    data = nData;
    next = p;
}

template<class T>
void Node<T>::setNext(Node * p)
{
    next = p;
}

template<class T>
void Node<T>::setData(T & nData)
{
    data = nData;
}

template<class T>
T Node<T>::getData()
{
    //this is where the debugger points to saying its a nullptr
    retu data;
}

template<class T>
Node<T> * Node<T>::getNext()
{
    retu next;
}

//MyList Functions//

template<class T>
MyList<T>::MyList()
{
    head = NULL;
    tail = NULL;
}

/*template<class T>
MyList<T>::MyList(MyList &)
{

}*/

template<class T>
MyList<T>::~MyList()
{
    Node<T>* ptr = head;
    Node<T>* temp;
    while (ptr != NULL)
    {
        temp = ptr->getNext();
        delete ptr;
        ptr = temp;
    }

}

template<class T>
void MyList<T>::push_front(T & newData)
{
    Node<T> *temp = new Node<T>;
    temp->setData(newData);
    temp->setNext(NULL);
    if (isEmpty() == true)
    {

        temp->setNext(head);
        head = temp;
        tail = head;
    }
    else
    {

        temp->setNext(head);
        head = temp;
    }

}

template<class T>
void MyList<T>::push_back(T & newData)
{
    Node<T> *temp = new Node<T>;
    temp->setData(newData);
    temp->setNext(NULL);
    if (isEmpty() == true)
    {
        temp->setNext(head);
        head = temp;
        tail = head;
    }
    else
    {
        tail->setNext(temp);
        tail = temp;
    }

}

template<class T>
void MyList<T>::pop_front()
{

    if (isEmpty() == true)
    {
        std::cout << "Nothing to pop" << std::endl;
    }
    else
    {
        Node<T> *temp = head;
        head = temp->getNext();
        delete temp;

    }
}

template<class T>
void MyList<T>::pop_back()
{
    if (isEmpty() == true)
    {
        std::cout << "Nothing to pop" << std::endl;
    }
    else if (head == tail)
    {
        head = tail = NULL;
    }
    else
    {
        Node<T> *temp = head;

        while (temp->getNext() != tail)
        {
            temp = temp->getNext();
        }
        temp->setNext(NULL);
        delete tail;
        tail = temp;
    }

}

template<class T>
bool MyList<T>::isEmpty()
{

    if (head == NULL && tail == NULL)
    {
        retu true;
    }
    else
    {
        retu false;
    }
}

template<class T>
void MyList<T>::testPrint()
{

    Node<T> *temp;

    temp = head;
    if (head == NULL)
    {
        std::cout << "List is empty" << std::endl;
        retu;
    }
    std::cout << "Here is der rinked rist ";
    while (temp != NULL)
    {
        std::cout << temp->getData() << " ";
        temp = temp->getNext();
    }

    std::cout << std::endl;
    retu;
}

#endif

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 26 ارديبهشت 1395 ساعت: 19:02

صفحه بندی