Thread: What comes after else?

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    26

    What comes after else?

    Hello!

    This is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "invent.h"
    
    
    void insert (LISTNODEPTR *, char *, long);
    void delete (LISTNODEPTR *, char *);
    int isEmpty (LISTNODEPTR);
    void printList(LISTNODEPTR);
    
    main()
    {
       LISTNODEPTR startPtr = NULL;
       FILE *fp;
       struct invent item;
       long recoff;
       
    
       if ((fp = fopen ("Invent.dat", "rb")) != NULL)
         {
           recoff = ftell (fp);
           fread(&item, sizeof(struct invent), 1, fp);
         }
           do
             {
                if (!feof(fp))
                   {
                   insert(&startPtr, item.part, recoff);
                   recoff = ftell(fp);
                   fread(&item, sizeof(struct invent), 1, fp);
                   }
              }
            while(!feof (fp));
         }
         else
          perror ("Unable to open input file");
          printList(startPtr);
          printf("End of run.\n");
          
          return 0;
    }
    
    /*Insert a new value into the list in sorted order */
    
    void insert (LISTNODEPTR * sPtr, char *value, long roffst)
    {
          LISTNODEPTR newPtr, previousPtr, currentPtr;
    
          newPtr = (LISTNODEPTR) malloc(sizeof(LISTNODE));
    
         if (newPtr != NULL
         {
            STRCPY(newPtr->part, value);
            newPtr->offset = roffst;
            newPtr->nestPtr = NULL; 
    
            previousPtr = NULL;
            currentPtr = *sPtr;
    
            while (currentPtr != NULL && strcmp (currentPtr->part, value) < 0)
                   {
                     previousPtr = currentPtr;
                     currentPtr = currentPtr->nextPtr;
                    }
    
                    if (previousPtr == NULL)
                    {
                       newPtr->nextPtr = *sPtr;
                       *sPtr = newPtr;
                       }
                     else
                     {
    I can't figure out what comes after else statement. I need to set the value of nextPtr in the node prior to the node being inserted, and set nextPtr of the new node to point to next node in list. If somebody could give me some hint about this how to finish it.

    Thanks.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    for starters why is there an if statement here?
    Code:
    do
             {
                if (!feof(fp))
                   {
                   insert(&startPtr, item.part, recoff);
                   recoff = ftell(fp);
                   fread(&item, sizeof(struct invent), 1, fp);
                   }
              }
            while(!feof (fp));
    shouldnt it be
    Code:
    while(!feof (fp));
    {
        insert(&startPtr, item.part, recoff);
        recoff = ftell(fp);
        fread(&item, sizeof(struct invent), 1, fp);
    }
    i may be wrong, im still a n00b at this.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    127
    Assuming everything went well, if previousPtr is not null after the loop then currentPtr points to the new item's next link and previousPtr points to the link that leads to the new item. All you need to do is fix the links by setting previousPtr->next, who points to currentPtr, to newPtr, then newPtr->next to currentPtr to splice the new node into your list.
    Code:
    previousPtr->next = newPtr;
    newPtr->next = currentPtr;

Popular pages Recent additions subscribe to a feed