Thread: Linked List problem

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    1

    Linked List problem

    I'm rather new to C and my previous experience with linked lists are limited and this is driving me nuts.

    Below I have posted all parts of the program that are troubling me. I believe that the function enterBoats() works perfectly (but if you happen to notice errors please let me know). The problem lies with createMaster() I believe it is a pointer problem but I've been looking at this crap so long I've gone nuts.

    What the program is meant to do is grab a bunch of strings from a file store them in a linked list. Then at this point it creates another file a prints them into it.

    If you need any more information please ask, this is very quickly becoming an emergance to write. I know it looks like a big problem but I think it's problem something simple. Any help and suggestions would be fantastic.

    Thanks in advance

    Kris

    /** HEADER FILE **/

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>

    #define MaxRaces 5
    #define MaxName 30
    #define maxHours 999
    #define maxMinutes 59
    #define maxSeconds 59
    #define inPutFile argv[1]
    #define opFile argv[2]
    #define masterFile "rec"

    FILE *masterFp;

    enum BoatType {Gwen, Skiff16, Thorpe12};
    typedef enum BoatType BoatType;

    typedef struct {
    unsigned short Hours;
    unsigned short Minutes;
    unsigned short Seconds;
    } Time;

    typedef char Digit; /* only contains '0'..'9' */
    typedef Digit Sail[3+1];
    typedef char Name[MaxName+1];

    typedef struct RaceTime {
    Time Duration;
    struct RaceTime *Next;
    } RaceTime, *timePtr;

    typedef struct Boat {
    Sail SailNum;
    Name BoatName;
    RaceTime Elapsed;
    } Boat;

    typedef unsigned PlaceArray[MaxRaces];

    typedef struct ResultRec {
    Boat Competitor;
    BoatType Class;
    PlaceArray Place;
    } ResultRec;

    typedef struct boatList {
    ResultRec boatDetails;
    struct boatList *next;
    } boatList, *boatPtr;

    timePtr newTimePtr, timeCurrent, timeHead, newTPtr;
    boatPtr firstNode, lastNode;

    /******* Function Prototypes *******/

    int enterBoats (FILE *);
    void operate (FILE *, int);
    void initialise (boatPtr *);
    boatPtr allocate (void);
    void print (boatPtr, int);
    void createMaster (int);
    void printMaster (int);
    void updateRace (char *);
    void seriesStandings ();
    void queryStandings (char *, int);
    void errorMsg (int);
    int makeInt (char *);

    BoatType convertMake (char *);
    /*char * upCase (char *);*/


    /** FUNCTIONS **/

    #include "boatDetails.h"

    int main(int argc, char *argv[])
    {
    FILE *inPutFp, *opFp; /* Assign file pointers */
    int records;

    firstNode = lastNode = NULL;

    inPutFp = fopen (inPutFile, "r");
    records = enterBoats (inPutFp);
    close (inPutFp);
    fflush (stdin);

    createMaster(records);

    return 0;
    }

    boatPtr allocate ( void ) {
    boatPtr ptr;
    ptr = (boatPtr)malloc(sizeof(boatList));
    ptr->next = 0;
    ptr->boatDetails.Competitor.Elapsed.Next = 0;
    return ptr;
    }

    timePtr allocateTime ( void ) {
    timePtr ptr;
    ptr = (timePtr)malloc(sizeof(RaceTime));
    ptr->Next = NULL;
    return ptr;
    }

    int enterBoats (FILE * inPutFp)
    {
    Sail inSail;
    Name inName;
    timePtr pTime;
    char inBoatType[16];
    int i, records = 0;

    while(!feof(inPutFp))
    {
    if (fscanf (inPutFp, "%s %s %s", inSail, inName, inBoatType) == 3)
    {
    records++;
    lastNode = allocate();

    printf("Boats in variables = %s %s %s\n",inSail, inName, inBoatType);

    strcpy(lastNode->boatDetails.Competitor.SailNum, inSail);
    strcpy(lastNode->boatDetails.Competitor.BoatName, inName);

    if (strcmp (inBoatType, "Gwen") == 0) lastNode->boatDetails.Class = Gwen;
    else if (strcmp (inBoatType, "Skiff16") == 0) lastNode->boatDetails.Class = Skiff16;
    else lastNode->boatDetails.Class = Thorpe12;

    printf ("Boats in linked list = %s\n\n", lastNode->boatDetails.Competitor.BoatName);

    pTime=&(lastNode->boatDetails.Competitor.Elapsed);
    pTime->Duration.Hours = maxHours;

    for (i = 0; i < 4; i++) {
    pTime=pTime->Next=allocateTime();
    pTime->Duration.Hours = i;
    }
    if (firstNode == NULL)
    firstNode = lastNode;
    }
    }
    return records;
    }

    void createMaster (int records) {
    boatPtr head;
    Sail inSail;
    Name inName;
    timePtr pTime;
    char inBoatType[16];
    boatPtr current, boatptr;
    timePtr newPtr;
    RaceTime racePtr;
    int j, i; char boatNum[20], boatName[20], boatType[20];

    masterFp = fopen (masterFile, "w");

    current = firstNode;

    while (current != NULL) {
    if (current->boatDetails.Class == 0)
    strcpy (boatType, "Gwen");
    else if (current->boatDetails.Class == 1)
    strcpy (boatType, "Skiff16");
    else if (current->boatDetails.Class == 2)
    strcpy (boatType, "Thorpe12");
    fprintf(masterFp, "%s ", boatType);
    fprintf(masterFp, "%s ",current->boatDetails.Competitor.SailNum);
    fprintf(masterFp, "%s ",current->boatDetails.Competitor.BoatName);

    for (i = 0; i < 5; i++) {
    fprintf(masterFp, " %d ", current->boatDetails.Competitor.Elapsed.Duration.Hours);
    fprintf(masterFp, "%d ", current->boatDetails.Competitor.Elapsed.Duration.Minutes );
    fprintf(masterFp, "%d", current->boatDetails.Competitor.Elapsed.Duration.Seconds );
    current->boatDetails.Competitor.Elapsed.Next;
    }
    fprintf(masterFp, "\n");
    printf("Boats in create master = %s ",current->boatDetails.Competitor.SailNum);
    printf("%s \n",current->boatDetails.Competitor.BoatName);
    current = current->next; */
    }
    Last edited by spec85; 06-14-2002 at 02:54 AM.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Some tips:

    1. Always check return values (fopen, malloc)
    Code:
    masterFp = fopen (masterFile, "w");
    
    if(masterFp == NULL)
    {
      printf("error opening file\n");
      return -1;
    }
    
    ptr = (boatPtr)malloc(sizeof(boatList)); 
    if(ptr == NULL)
    {
      /* do something */
    }
    2. Use code tags: &#91CODE&#93 /* your code */ &#91/CODE&#93

    3. fflush(stdin) is wrong. Try somethink like this:
    Code:
    scanf("%c", &c);
    while (getchar() != '\n');
    Or even better: don't use scanf/fscanf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM