Originally Posted by
Tonto
I don't think so. The function operates on an already allocated WORD node and then allocates space for the next one and calls itself recursively. This is also an issue, when you initially call the function, your head node should be previously allocated, and also in your base case you should set the end of the list making record->next = NULL;
I have some issue in the display. I have made the changes but when I execute, the output file is blank, even the header output does not print.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct wordnode {
char *word;
int length;
int freq;
struct wordnode *next} WORD;
main()
{
FILE *fin,*fout;
WORD *start;
void create(WORD *pt,FILE *fin);
void display(WORD * pt,FILE *fout );
fin=fopen("cp50610.dat","r");
fout=fopen("cp50610.out","w");
fprintf(fout,"**********************\n");
fprintf(fout," OUTPUT\n");
fprintf(fout,"**********************\n");
start=malloc(sizeof(WORD));/* get space for first node*/
create(start,fin);
display(start,fout);
fclose(fin);
fclose(fout);
return 0;
}/* end main*/
void create(WORD *record, FILE *fin) /*to create the list */
/* argument points to current node; function is recursive */
/* strings are input until string END is encountered */
{char temp[30]; /*temporary storage for string */
int k; /*length of current string */
fscanf(fin," %[^ ,.;?\n]",temp); /*input into temp to get length */
k = strlen(temp)+1; /*need room for null character */
record->word=malloc(k*sizeof(char)); /*get address for permanent storage of string */
strcpy(record->word,temp); /*put string there */
record->next = malloc(sizeof(WORD));
create(record->next,fin);
} /*end of create */
void display(WORD * record,FILE *fout) /*to print linked list */
/*argument points to start of list */
{
fprintf(fout,"%s\n",record->word); /* print this one */
while(record->next != NULL) /* & walk down list */
{ record = record->next;
fprintf(fout,"%s\n",record->word); /* print this one */
}
} /*end of display */