I am trying to sort a linked list of structs and keep getting the error "expected declaration or statement at end of input." Ive looked for the usual problems such as undeclared variables and missing braces, but I still cant find the source of error. Hopefully my first attempt at code tags will work. Please help..thanks in advance.
Code:
/*Program to sort people by id number
Language: C (gcc target)
*/
#include <stdio.h>
#include <stdlib.h> //needed for malloc
#include <string.h>
#define NUM_IDS 3
typedef struct idnumber
{char firsname[10]; /*holds first name*/
char lastname[10]; /*last name*/
int idnum; /*id number*/
struct idnumber *next; /*link to next record*/
}IDSORT;
IDSORT* insertit(IDSORT *first, char namefirst[], char namelast[], int numid);
int main(void)
{
IDSORT *head, /*pointer to head of linked list*/
*p; /*pointer to traverse list*/
char firstnamemain[10]; /*first name*/
char lastnamemain[10]; /*last name*/
int idnummain, /*id number*/
j, /*counter*/
k; /*counter - list entry number*/
char ch, /*holder*/
nl; /*place to keep newline character*/
head=NULL;
for(j=0;j<10;j++) /*blank out name arrays*/
{firstnamemain[j]=' ';
lastnamemain[j]=' ';
}
for(k=0;k<NUM_IDS;k++) /*input info loop*/
{printf("Enter first name: \n");
gets(firstnamemain);
printf("Enter last name: ");
gets(lastnamemain);
printf("Enter id number: ");
scanf("%d", &idnummain);
scanf("%c", &nl);
head=insertit (head,firstnamemain,lastnamemain,idnummain); /*blank out the main arrays again*/
for (j=0;j<10;j++)
{firstnamemain[j]=' ';
lastnamemain[j]=' ';
} /*list full*/
p=head; /*point p to the beginning of the list*/
while (p!=NULL) /*prints list*/
{printf("The name of the person is: %s ", p->firsname);
printf("The time of the appointmetnt is: %d:%d\n",p->lastname, p->idnum);
p=p->next;
}
printf("Hit any character to continue");
scanf("%c",&ch);
}
IDSORT* insertit (IDSORT *first, char namefirst[], char namelast[], int numid)
/* function to sort array by id number
*/
{ IDSORT *p,
*q,
*newp;
int found,
len,
i;
found=0;
q=first;
p=first;
/*find the appropriate position for the new entry*/
while ((p!=NULL) && (!found))
{ if (p->idnum <= numid)
{q=p;
p=p->next;
}
else
found=1;
}
newp=(IDSORT*) malloc(sizeof(IDSORT)); /*create new record ptd by newp*/
newp->idnum=numid;
strncpy(newp->firsname, namefirst, 10);
strncpy(newp->lastname, namelast, 10);
/*adjust pointers*/
newp->next=p;
if(q!=p)
{q->next=newp;
}
else /*insertion is at the beginning*/
{first=newp;
}
return(first);
}