#Linked list treatment
8 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
Hi, Im having some problems with some code I made. I basically have to put information into a list
information is stored in an array called aux, and for every iteration in a while loop, the information will change, and the size of aux is always 4
so aux would look like this
["name","id","latitude","longitude"]
I want to make a list with that information
typedef struct station {
int id; //id de la estacion
char* name; //nombre de la estacion
unsigned int cantMembers; //guardamos cuantos viajes fueron hechos por miembros (para query 1)
unsigned int vecMeses[MONTHS]; //para guardar la cantidad de viajes por mes
unsigned int index; //guarda la posicion en la que esta en la lista, para facilitar acceso a matrix
struct station* tail; //puntero a la siguiente estacion
} station;
typedef station * tStation;
typedef struct bikesCDT
{
tStation firstStation; //puntero a la primera estacion (orden alfabetico)
unsigned int cantStations; //guardamos la cantidad total de estaciones para el temano el vector aToB
unsigned int** matrix; //matriz de adyacencia para marcar los viajes entre estaciones
tStation it; //para ir guardando la posicion cuando haga la query 2
} bikesCDT;``` the structs look like this
int addStation(bikesADT results, char * name, char * ID)
{
tStation newStation;
newStation = calloc(1, sizeof(struct station));
if(newStation == NULL)
return 1;
int id = atoi(ID);
newStation->id = id;
newStation->name = malloc(strlen(name)+1);
if(newStation->name == NULL)
return 1;
strcpy(newStation->name, name);
results->cantStations++;
results->firstStation = addToList(results->firstStation, newStation);
return 0;
}```this code will generate the single node with the information of the aux array in the main file
and then the problem comes when adding a new node to the list of nodes:
static tStation addToList(tStation lista, tStation node)
{
int c;
if(lista == NULL || (c = strcmp(lista->name,node->name) > 0))
{
node->tail = lista;
return node;
}
else if(c < 0) //se tiene que agregar mas adelante en la lista
{
lista->tail = addToList(lista->tail, node);
}
return lista;
}```
Ending...
> 996 - Nombre de la estacion: 10e avenue / Holt ID de la estacion: 868
> 996 - Nombre de la estacion: 16e avenue / St-Joseph ID de la estacion: 100
> 996 - Nombre de la estacion: 4e avenue / de Verdun ID de la estacion: 196
> 996 - Nombre de la estacion: Alexandre-DeSève / Ste-Catherine ID de la estacion: 285
> 996 - Nombre de la estacion: Alexis-Nihon / St-Louis ID de la estacion: 1037
> 996 - Nombre de la estacion: Bloomfied / St-Viateur ID de la estacion: 1170
> 996 - Nombre de la estacion: Chambord/ Beaubien ID de la estacion: 1150
> 996 - Nombre de la estacion: Clark / Rachel ID de la estacion: 338
> 996 - Nombre de la estacion: Fabre / Beaubien ID de la estacion: 586
> 996 - Nombre de la estacion: Gilford / de Brébeuf ID de la estacion: 267
> 996 - Nombre de la estacion: Sanguinet / de Maisonneuve ID de la estacion: 327
``` this is what the linked list will have, and no more items, but there are 996 items, it seems the addToList function isnt adding properly to the lis
can anyone spot the thing that breaks it all?