sorting out C linked list/pointer errors
This is a program to insert, delete, print, printall to a list of waypoints using linked lists and pointers in C.
My biggest problem is prototyping a function that has a cast that be will typedefined in Main... how do I do it?
I've resorted to trial and error to try and find the correct syntax but that's gotten me anywhere.
Please point out any obvious syntatical errors in the code.
Insert() is not finished and I am having conceptual troubles with delete(). I'd like to get the bugs worked out thusfar before I finish insert and do the delete routine.
Thank you.
---
// //
#include
#include
#include
//dummy macros until I can get the rest figured out//
#define distance(a,b) a+b
#define heading(a,b) a+b
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;
double x,y;
char cmd;
typedef struct tnode
{
int wayptnum;
double x;
double y;
double heading;
double distance;
struct tnode *next;
}gpsnode;
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);
}
while ( fscanf(fileptr,"%c%i%f%f",cmd,waypt,x,y) != EOF )
{
switch (cmd)
{
case 'a': insert(head,data);
case 'P': prntall(head);
case 'p': prnt(head,data);
case 'd': delte(head,data);
default: printf("%c",cmd," was not a valid option.");
}
}
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=newdata.wayptnum;
newspace->x=newdata.x;
newspace->heading=0.0;
newspace->distance=0.0;
if (*headptr==NULL)
{
*headptr=newspace;
newspace->next=NULL;
newspace->heading=0.0;
newspace->distance=0.0;
newspace->distance=0.0;
return(0);
};
temp=*headptr;
hold=NULL;
while ((newdata.wayptnum>temp->wayptnum) && (temp->next!=NULL) )
{
hold=temp;
temp=temp->next;
}
if (newdata.wayptnumber==temp->wayptnum)
{
printf("Duplicate entry WP# %dn",newdata.wayptnum);
free (newspace);
return(1);
}
if (newdata.wayptnumber <= temp->wayptnum)
{
*headptr=newspace;
newspace->next=temp;
newspace->distance=distance(newspace,temp);
newspace->heading=heading(newspace,temp);
}
/else decode next position
}
int prntall(gpsnode *head)
{
temp=*headptr;
if (temp==NULL)
{
fprintf(stderr,"There is no list.n");
return(-1);
};
printf("Way point #x,yheadingdistancen");
while (temp->next!=NULL)
{
printf(temp->wayptnum," ",temp->x,", ",temp->y," ",temp->heading," ",temp->distance,"n");
temp=temp->next;
}
return(0);
}
int prnt(gpsnode *head,gpsnode data)
{
temp=*headptr;
if (temp==NULL)
{
fprintf(stderr,"There is no list.n");
return(-1);
}
while (temp->next!=NULL)
{
if (strcmp(data.city,temp->city)==0)
{
printf("way point #",temp->wayptnum," at (",temp->x,", ",temp->y,") is at");
printf("direction ",temp->direction," and heading ",temp->heading,"n");
return(0);
}
fprintf(stderr,"The waypoint was not found in the list.");
return (-1);
}
int delte(gpsnode *head, gpsnode data)
{
int dummy;
};
>gcc -o gps c gps.c
gps.c:12: parse error before `*'
gps.c:13: parse error before `*'
gps.c:14: parse error before `*'
gps.c:15: parse error before `*'
gps.c:77: parse error before `*'
gps.c: In function `insert':
gps.c:79: `gpsnode' undeclared (first use in this function)
gps.c:79: (Each undeclared identifier is reported only once
gps.c:79: for each function it appears in.)
gps.c:79: `temp' undeclared (first use in this function)
gps.c:79: `hold' undeclared (first use in this function)
gps.c:79: `newspace' undeclared (first use in this function)
gps.c:80: parse error before `)'
gps.c:88: `newdata' undeclared (first use in this function)
gps.c:93: `headptr' undeclared (first use in this function)
gps.c:129: parse error before `/'
gps.c: At top level:
gps.c:153: parse error before `*'
gps.c: In function `prnt':
gps.c:155: `temp' undeclared (first use in this function)
gps.c:155: `headptr' undeclared (first use in this function)
gps.c:165: `data' undeclared (first use in this function)
gps.c:175: parse error before `int'
>exit
logout
--Thank you.
By OTA: David Grant, MASc
OTA Rating: 4.8/5
Your Price: $2.19 (original value ~$11.97)
What's included:
Page generated in 0.0113 seconds