I'm wondering how I could improve my list's element-adding speed or just the whole list implementation.
The adding function first checks if the object is unique. If not, it will just check if the new data is better (the lower the value, the better) than the old one, then swap the values or just skip adding an element.
I have no idea how to make it work faster, but I know that something here is probably done terribly wrong. I need to use that list to add about 3 million elements in less than 4 seconds.
Note: Using std::string
in my project is unfortunately forbidden.
#include <iostream>
struct node
{
long shortestRoute;
char* name;
node *next;
node()
{
next = 0;
}
};
class List {
public:
node *first;
List() { first = 0 ;}
void add(long shortestRoute, char* name)
{
node *node = new node;
node->name = name;
node->shortestRoute = shortestRoute;
if(first == 0)
first = node;
else
{
node *temp = first;
while(temp)
{
if(!strcmp(temp->name, name) && temp->shortestRoute > shortestRoute)
{
temp->shortestRoute = shortestRoute;
return;
}
else if (!strcmp(temp->name, name))
return;
else if(temp->next)
temp = temp->next;
else
break;
}
temp->next = node;
node->next = 0;
}
}
int size()
{
node *temp = first;
int size;
if(!first)
size = 0;
else
size = 1;
while(temp->next)
{
++size;
temp = temp->next;
}
return size;
}
};
struct City
{
City() { fastConnection = true; }
int shortestRoute;
char* name;
Vector<char*> citiesSoFar;
bool fastConnection;
};