help with structs in list

This is a discussion on help with structs in list within the C Programming forums, part of the General Programming Boards category; I am trying to sort a linked list of structs and keep getting the error "expected declaration or statement at ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    help with structs in list

    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);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    All I did was type Ctrl-F in my browser and it reported 11 hits for open-curly-brace and 10 hits for close-curly-brace. Therefore....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 12:05 AM
  2. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21