linked list program -> data is not being read in correctly?
(the program is supposed to insert, delete, print, and print all members of a link list)
Please help me sort this out. This is my program which is supposed to read from a file: command, waypoint number, x coord, y coord. It calls a function based on the incoming command.
The program compiles and runs without a formal error. However, it hangs on the prnt function.
My data is not being read in correctly. If the data is not being read in correctly, then it would account for junk piling up in the list. (incorrect pointer use?)
and/or
My data is either not being stored or referenced correctly in the linked lists. (incorrect pointer use?)
Please tell me where the problem is and offer an explicit solution. Thank you.
=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2004.02.25 17:05:32 =~=~=~=~=~=~=~=~=~=~=~=
cat gps.c
#include
#include
#include
//macros for distanc and heading calculations//
#define distance(a,b,c,d) sqrt( ((a-c)*(a-c))+((b-d)*(b-d)))
#define heading(a,b,c,d) atan((b-d)/(a-c))
typedef struct tnode
{
int wayptnum;
float x;
float y;
float heading;
float distance;
struct tnode *next;
} gpsnode;
int insert(gpsnode **head,gpsnode data);
int prnt(gpsnode **head,gpsnode data);
int prntall(gpsnode **head);
int delte(gpsnode **head,gpsnode data);
main (int argc, char *argv[])
{
FILE *fileptr;
int waypt;
float x,y;
char cmd;
gpsnode data;
gpsnode *head;
/*Check to see if the corect syntax was used.*/
if ( argc != 2 )
{
fprintf(stderr,"%s",": Incorrect number of arguments.n");
fprintf(stderr,": Usage: GPS
return(1);
}
/*Check to see if the input file exists.*/
/*Also open file for input*/
if (((fileptr=(fopen(argv[1],"r")))==NULL))
{
fprintf(stderr,": The file '%s",argv[1]);
fprintf(stderr,"' does not exist. Pleasen");
fprintf(stderr,"check that the file name is spelled correctly,n");
fprintf(stderr,"exists, and is in the directory specified.n");
return(2);
}
/*The fscanf returns pointers to the data objects*/
while ( fscanf(fileptr,"%c %i %f %fn",&cmd,&waypt,&x,&y) != EOF )
{
data.wayptnum=waypt;
data.x=x;
data.y=y;
/* a launches the insert function */
/* P launches the prntall function */
/* p launches the prnt function */
/* d launches the delte function */
/*take this out before turngin in...*/
printf("%c command received! Wheee!nn",cmd);
switch (cmd)
{
case 'a': insert(&head,data);
break;
case 'P': prntall(&head);
break;
case 'p': prnt(&head,data);
break;
case 'd': delte(&head,data);
break;
default: printf("%c was not a valid option.",cmd);
break;
}
}
fclose(fileptr);
return(0);
}
int insert(gpsnode **head, gpsnode data)
{
gpsnode *temp,*hold,*newspace;
newspace=(gpsnode *)malloc(sizeof(gpsnode));
if (newspace==NULL)
{
printf("Fatal Error: Unable to allocate spacen");
exit(-1);
}
newspace->wayptnum=data.wayptnum;
newspace->x=data.x;
newspace->y=data.y;
newspace->heading=0.0;
newspace->distance=0.0;
if (*head==NULL)
{
*head=newspace;
newspace->next=NULL;
newspace->heading=0.0;
newspace->distance=0.0;
newspace->distance=0.0;
return(0);
};
temp=*head;
hold=NULL;
while ((data.wayptnum>temp->wayptnum) && (temp->next!=NULL) )
{
hold=temp;
temp=temp->next;
}
if (data.wayptnum==temp->wayptnum)
{
printf("Duplicate entry WP# %in",data.wayptnum);
free (newspace);
return(1);
}
if (data.wayptnum <= temp->wayptnum)
{
*head=newspace;
newspace->next=temp;
/*calculate distancee*/
newspace->distance=distance(newspace->x,temp->x,newspace->y,temp->y);
/*check for divide by zero in the atan function.*/
if ( (temp->x-temp->y)==0 )
{
fprintf(stderr,"Heading calculation error. Heading set to 0.0.");
newspace->heading=0.0;
return(-3);
}
newspace->heading=heading(newspace->x,temp->x,newspace->y,temp->y);
/*degrees to radians*/
newspace->heading=((newspace->heading)*(3.14/180));
}
/*else decode next position*/
}
int prntall(gpsnode **head)
{
gpsnode *temp;
if (*head==NULL)
{
fprintf(stderr,"There is no list.n");
return(-1);
};
printf("Way point #x,yheadingdistancen");
while (temp->next!=NULL)
{
printf("%i %.2f,%.2f %.2f .2%fnn",temp->wayptnum,temp->x,temp->y,temp->heading,temp->distance);
temp=temp->next;
}
/*temp next should be null at this point*/
return(0);
}
int prnt(gpsnode **head,gpsnode data)
{
gpsnode *temp;
if (*head==NULL)
{
fprintf(stderr,"There is no list.nn");
return(-1);
}
temp=*head;
while (temp->next!=NULL)
{
if (data.wayptnum==temp->wayptnum)
{
printf("Way point #%i is at (%f,%f) with a ",temp->wayptnum,temp->x,temp->y);
printf("distance of %f and heading of %f.nn",temp->distance,temp->heading);
return(0);
}
}
fprintf(stderr,"The waypoint was not found in the list.nn");
return (-1);
}
int delte(gpsnode **head, gpsnode data)
{
gpsnode *delete;
gpsnode *hold;
if (*head==NULL)
{
printf("There is no list.n");
return (-1);
}
delete=*head;
hold=NULL;
/*Check to see if delete request is the head and process accordingly.*/
if ((delete->wayptnum=data.wayptnum) && (delete==*head))
{
*head=delete->next;
free(delete);
return(0);
}
/*Go through the list, if you find the deletion request, process accordingly.*/
while (delete->next!=NULL)
{
if (delete->wayptnum==data.wayptnum)
{
hold->next=delete->next;
free(delete);
return(0);
}
}
/*At this point, either the requested item to delete is not in the list, or we're at the tail.*/
/*Check for tail and process accordingly*/
if (delete->next==NULL)
{
hold->next=delete->next;
hold->distance=0.0;
hold->heading=0.0;
free(delete);
return(0);
}
/*The item was not in the list.*/
fprintf(stderr,"The requested waypt to be deleted was not in the list.nn");
return(-2);
}
>cat gps.dat
a 1 0.0 0.0
P 0 0.0 0.0
p 1 0.0 0.0
p 0 0.0 0.0
p 2 0.0 0.0
d 1 0.0 0.0
P 0 0.0 0.0
p 1 0.0 0.0
a 4 -123.5 554.2
a 6 3022.4 893.2
a 5 574.23 123.22
P 0 0.0 0.0
p 5 0.0 0.0
d 5 0.0 0.0
P 0 0.0 0.0
d 4 0.0 0.0
d 6 0.0 0.0
a 1 0.0 0.0
P 0 0.0 0.0
a 2 0.0 1.0
P 0 0.0 0.0
a 3 1.0 1.0
P 0 0.0 0.0
a 4 0.0 0.0
P 0 0.0 0.0
a 5 1.0 1.0
P 0 0.0 0.0
a 6 1.0 0.0
P 0 0.0 0.0
a 7 0.0 0.0
P 1 1.0 1.0
d 1 0.0 0.0
P 1 0.0 0.0
d 7 0.0 0.0
P 0 0.0 0.0
d 4 0.0 0.0
d 5 0.0 0.0
P 0 0.0 0.0
>gps gps.dat
a command received! Wheee!
P command received! Wheee!
Way point #x,yheadingdistance
229816 -254483880074847996449356336619673616384.00,-254562089046279677290511077220632494080.00 -254484204593401654876083119775694192640.00 .2-254562413564833335717237860376653070336.000000
p command received! Wheee!
Way point #1 is at (0.000000,0.000000) with a distance of Inf and heading of 0.027402.
p command received! Wheee!
^X
^C
By OTA: David Grant, MASc
OTA Rating: 4.8/5
Your Price: $2.19 (original value ~$23.94)
What's included:
Page generated in 0.013 seconds