-1

Here is a linked list:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct Arrival
{
    string arrAddress;
    double distance;
    string roadCategory;
    struct Arrival* next;
};


class ArrivalList
{
    private:
        struct Arrival* head;

    public:

        bool addArrival(string oneAddress, double distance, string roadCategory);


};


bool ArrivalList::addArrival(string oneAddress, double distance, string roadCategory)
{
    Arrival* temp;
    Arrival* current=new Arrival();
    current=head;
    temp=new Arrival();
    temp->arrAddress=oneAddress;
    temp->distance=distance;
    temp->roadCategory=roadCategory;
    int f=0;
    while(1)
    {
        if(current==NULL)
        {
            temp->next=NULL;
            head=temp;
            f=1;
            break;

        }
        else if(oneAddress>=current->arrAddress && oneAddress<current->next->arrAddress)
        {
            temp->next=current->next;
            current->next=temp;
            f=1;
            break;
        }
        else
        {
            current=current->next;
        }
    }
    cout<<head->arrAddress;
   if(f)
   {
       return true;
   }
   else
   {
       return false;
   }
}


int main()
{
    ArrivalList li;
    li.addArrival("jjjjj",0.8999,"I");
    li.addArrival("aaaaa",0.888,"k");


}

I want the value of head persist within the multiple call to the function addArrival.But it is changing.I want to to start the loop from 'current' value which should be the value of head after the first call but it is changing and starting from NULL everytime,I suppose.

I want that everytime I call the function it should get the value of updated head and start looping,but the value is not saving for head after the first call.

13
  • 1
    Arrival* current=new Arrival(); current=head; A memory leak right there. Please run it under valgrind first and fix all errors it reports. Only then it will be meaningful for others to look at the code… Commented Nov 8, 2023 at 18:59
  • How to solve the memory leak,brother? Commented Nov 8, 2023 at 19:02
  • 1
    You say Arrival * current = head;. Have you run this in a debugger? The problem happens in your else if line where you refer to current->next->arrAddress. Just because current has a value does not mean current->next has a value. Commented Nov 8, 2023 at 19:05
  • 1
    You or someone else in your class asked the same question here. Commented Nov 8, 2023 at 20:07
  • 1
    Not only is it the same question, it's even the same buggy code! Commented Nov 8, 2023 at 20:46

1 Answer 1

1

Your ArrivalList class is not initializing its head member, nor is it freeing the list when finished using it, thus leaking the nodes.

And, your addArrival() method has several other problems as well, including:

  • 1 guaranteed memory leak
  • 1 potential memory leak if the new node is not added to the list.
  • several more memory leaks when you accidentally wipe out your entire list if the iteration reaches the end of the list.
  • dereferencing a null pointer if the iteration reaches the last node in the list.

Try something more like this instead:

#include <iostream>
#include <string>

using namespace std;

class ArrivalList
{
    private:
        struct Arrival
        {
            string arrAddress;
            double distance;
            string roadCategory;
            Arrival* next;
        };

        Arrival* head;

        ArrivalList(const ArrivalList&) {}
        ArrivalList& operator=(const ArrivalList&) { return *this; }

    public:

        ArrivalList();
        ~ArrivalList();

        bool addArrival(string oneAddress, double distance, string roadCategory);
};

ArrivalList::ArrivalList() : head(NULL) {}

ArrivalList::~ArrivalList()
{
    Arrival* current = head;
    while (current)
    {
        Arrival* temp = current->next;
        delete current;
        current = temp;
    }
}

bool ArrivalList::addArrival(string oneAddress, double distance, string roadCategory)
{
    Arrival** current = &head;

    while ((*current != NULL) && (oneAddress >= (*current)->arrAddress))
        current = &((*current)->next);

    Arrival* temp = new Arrival;
    temp->arrAddress = oneAddress;
    temp->distance = distance;
    temp->roadCategory = roadCategory;
    temp->next = *current;

    *current = temp;

    cout << head->arrAddress << endl;

    return true;
}

int main()
{
    ArrivalList li;
    li.addArrival("jjjjj",0.8999,"I");
    li.addArrival("aaaaa",0.888,"k");
    li.addArrival("lllll",0.888,"k");
    li.addArrival("kkkkk",0.888,"k");
}

Online Demo

  • On the first call, the list is empty, so jjjjj gets added as the new head.

  • On the second call, aaaaa is "less than" jjjjj, so it gets added as the new head and jjjjj becomes the 2nd node in the list.

  • On the third call, lllll is "more than" jjjjj, so it gets added as the last node in the list.

  • On the fourth call, kkkkk is "more than" jjjjj and "less than" lllll, so it added between those nodes in the list.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.