Question

I'm working on a school project, and what I need to do is to sort the elements (strings) in a circular singly linked list alphabetically.

I thought that I could create a dynamic array and fill it with the elements from the list and send the elements back to the list after the array is sorted. It works great when I first ask the program to sort it. However, when I call the sort function again it gives an error. The error occurs on the line temp[i] = head->info. It shows me the xstring header file and says "unhandled exception".

I would be so happy if you can help me. I think what I'm missing here is a very basic thing, and I'll get better if I could learn what's wrong with this code. Thank you.

Edit:

template<class T>
class Node
{
    private:
        T Name;
        T Surname;
        T Email;
        T PhoneNumber;
    public:
        Node();
        Node(T Name, T Surname, T Email, T PhoneNumber);
        Node(Node& copy);
        ~Node();
        T getName();
        T getSurname();
        T getEmail();
        T getPhoneNumber();
        void setName(T Name);
        void setSurname(T Surname);
        void setEmail(T Email);
        void setPhoneNumber(T PhoneNumber);
        Node<T>& operator= (const Node&);
};

This is the code for the function

template <class T>
void LinkedList<T>::sort()
{
    int sizeoflist = this->size();
    Node<T> *temp = new Node<T>[sizeoflist];
    for(int i=0; i<sizeoflist; i++)
    {
        temp[i] = head->info;
        this->removeFromHead();
    }

    //BUBBLE SORT
    for(int i=0; i<sizeoflist; i++)
    {
        for(int k=0; k<sizeoflist -1 -i; k++)
        {
            if(temp[k].getSurname() > temp[k+1].getSurname())
            {
                Node<T> temp2 = temp[k];
                temp[k] = temp[k+1];
                temp[k+1] = temp2;
            }
        }
    }

    //FILLING THE LIST
    for(int i=0; i<sizeoflist; i++)
    {
        this->addToTail(temp[i]);
    }
    delete[] temp;
}

Assignment operator overloading code for the Node class.

template<class T>
Node<T>& Node<T>::operator= (const Node<T>& newNode)
{
    if(this == &newNode)
        return *this;
    Name = newNode.Name;
    Surname = newNode.Surname;
    Email = newNode.Email;
    PhoneNumber = newNode.PhoneNumber;
    return *this;
}

Edit:

I realized that there is something wrong with my addToTail function. It does add new elements to tail.

However, when I use addToHead instead of addToTail in my sort function it works perfectly after the first run too.

Here is my addToTail function

template <class T>
void LinkedList<T>::addToTail(Node<T> newInfo)
{
    LinkedList<T> *node = new LinkedList<T>;
    node->info = newInfo;
    if(head==NULL)
    {
        head = node;
        tail = node;
        tail->next = head;
    }
    else
    {
        LinkedList<T> *temp = head;
        while(temp->next != head)
        {
            temp = temp->next;
        }
        temp->next = node;
        node->next = head;
    }
}

And addToHead function

template <class T>
void LinkedList<T>::addToHead(Node<T> newinfo)
{
    LinkedList<T>* element = new LinkedList<T>;
    LinkedList<T>* temp = new LinkedList<T>;
    element->info = newinfo;
    if(head==NULL)
    {
        head = element;
        tail = element;
        tail->next = head;
    }
    else
    {
        temp = head;
        head = element;
        head->next = temp;
        tail->next = head;
    }
}
Était-ce utile?

La solution

There is an error in your addToTail function. You don't update the tail member variable to the new node. Also searching for the tail node using while is unnecessary because you have direct access to it via tail.

There also is an error in your addToHead function but it's not important here. You are allocating a new temp node which will always leak.

Perhaps fixing these errors is already enough. If it isn't you should show your removeFromHead. I guess it messes with the head variable and will leave it in some "uninitialized or stale" state, so that the next access to head->next crashes.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top