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.
Arrival* current=new Arrival(); current=head;A memory leak right there. Please run it undervalgrindfirst and fix all errors it reports. Only then it will be meaningful for others to look at the code…Arrival * current = head;. Have you run this in a debugger? The problem happens in yourelse ifline where you refer tocurrent->next->arrAddress. Just becausecurrenthas a value does not meancurrent->nexthas a value.