By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

problem with making a linked list

P: 1
Hi~

I need to write a program which accept command line parameter which is the text file, load the employee file into a linked list and print it.

the employee file is like:

012452 Wong Andrew 23.56
784552 McDonald Adele 32.4

Please help me to debug it, the output of the program is odd and doesn't do what it should do.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> 
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. struct employeeRecords
  7. {
  8.    int empNum;
  9.    char lastName[31];
  10.    char firstName[31];
  11.    double hourRate;
  12.    struct employeeRecords *next;
  13. };
  14. typedef struct employeeRecords empRecord;
  15.  
  16. FILE* openfile(char *filename, char *mode);
  17. empRecord* createNode();
  18. void freeList(empRecord **start);
  19. void printRecord(empRecord *start);
  20. void readfile(FILE *fin, empRecord *temp);
  21. void makeRecord(char *filename, empRecord **start);
  22. void addList(empRecord **start, empRecord *temp);
  23.  
  24. int main(int argc, char *argv[])
  25. {
  26.    empRecord *head = NULL;
  27.  
  28.    makeRecord(argv[1], &head);
  29.    printRecord(head);
  30.    freeList(&head);
  31.  
  32.    return 0;
  33. }
  34.  
  35. FILE* openfile(char *filename, char *mode)
  36. {
  37.    FILE *file = fopen(filename, mode);
  38.  
  39.    if (file == NULL)
  40.    {
  41.       printf("ERROR - Unable to access %s\n", filename);
  42.       exit(0);
  43.    }
  44.    return file;
  45. }
  46.  
  47. void makeRecord(char *filename, empRecord **start)
  48. {
  49.    empRecord *temp;
  50.  
  51.    FILE *fin = openfile(filename, "r");
  52.  
  53.    /* while not end of file, repeat the following */
  54.    while (!feof(fin))
  55.    {
  56.       temp = createNode();
  57.       readfile(fin, temp);
  58.       addList(&(*start), temp);
  59.    }
  60.    fclose(fin);
  61. }
  62.  
  63. void addList(empRecord **start, empRecord *temp)
  64.    empRecord *tail = *start;
  65.  
  66.    /* if the list is empty, make temp as the first node
  67.       in the list */
  68.    if (tail == NULL) *start = tail = temp;
  69.    else
  70.    {
  71.       tail->next = temp;
  72.       tail = temp;
  73.    }
  74. }
  75.  
  76. void readfile(FILE *fin, empRecord *temp)
  77. {
  78.    int status = fscanf(fin,"%d %s %s %lf", &(temp->empNum),
  79.                        temp->lastName, temp->firstName, 
  80.                        &(temp->hourRate));
  81.  
  82.    /* if the file is corrupted, display error message,
  83.       free the memory and exit the program */
  84.    if (status == 0)
  85.    {
  86.       printf("ERROR - Corrupted record in file\n");
  87.       free(temp);
  88.       exit(0);
  89.    }
  90. }  
  91.  
  92. empRecord* createNode()
  93. {
  94.    empRecord *node;
  95.  
  96.    /* build empty node */
  97.    node = (empRecord *) malloc(sizeof(empRecord));
  98.  
  99.    /* if no memory can be allocated, terminate the program */
  100.    if (node == NULL) exit(0);
  101.  
  102.    /* set node pointing to null */
  103.    node->next = NULL;
  104.  
  105.    return node;
  106. }
  107.  
  108. void freeList(empRecord **start)
  109. {
  110.    empRecord *node;
  111.  
  112.    while (*start != NULL)
  113.    {
  114.       node = *start;
  115.       *start = node->next;
  116.       free(node);
  117.    }
  118. }
  119.  
  120. void printRecord(empRecord *start)
  121. {  
  122.    empRecord *curr;
  123.  
  124.    printf("\n");
  125.    printf("Number  Last Name            First Name           Rate\n");
  126.    printf("-------+--------------------+--------------------+-----\n");
  127.    for (curr = start; curr != NULL; curr = curr->next)
  128.    {
  129.       printf("%6d  %-20s %-20s %5.2f\n", curr->empNum, curr->lastName,
  130.               curr->firstName, curr->hourRate);
  131.    }
  132.    printf("\n");   
  133. }
  134.  
Nov 3 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
Please help me to debug it, the output of the program is odd and doesn't do what it should do.
What should it do?
What does it do?


P.S. do you realise that

&(*start) === start

P.P.S. The error (or an error at least) is in addList. You variable name tail suggests you want to add each new record to the end of the list, however you never move the tail pointer down the list before you write the next pointer so you end up with a list of 2 items consisting of the first and last records read.
Nov 3 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.