Hi, I'm currently having a problem in my program which is based on linked list. I'm not sure if its my display function or the storage method.
Its suppose to read in a sample file which contains a string.
Problem : displays the first character infinitely
Here's a sample of my program.
Code:
/* Header Files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* Constants */
#define MAXSIZE 100
typedef struct wordStruct *wordListStruct;
typedef struct word *wordR;
/*Take first character*/
typedef struct word
{
char alphabet;
struct word* next;
} wordType;
/*Take first line*/
typedef struct wordStruct
{
wordType* head;
int line;
struct wordStruct* next;
} wordStructType;
/*Header*/
typedef struct sentenceStruct
{
wordStructType* head;
} FileADT;
/* Function prototypes */
void systemInit(FileADT*);
int FileOpen(FileADT*, char*);
int FileDisplay (FileADT*);
int main(int argc, char *argv[])
{
FileADT sentence;
systemInit(&sentence);
FileOpen(&sentence,argv[1]);
if(FileOpen)
printf("Loading Successful");
FileDisplay(&sentence);
if(FileDisplay)
printf("Printing Successful");
return 0;
}
void systemInit(FileADT* sentence)
{
/*Initialise list*/
sentence->head = NULL;
}
int FileOpen(FileADT* wList, char* textWord)
{
FILE *textData;
/*Structure to take in first line*/
wordListStruct lineList, currLine;
/*Structure to take in characters*/
wordR word, currWord;
int sSize = 0;
char sentance[MAXSIZE];
wList->head = NULL;
if((textData = fopen(textWord, "r"))==NULL){
fprintf(stderr,"***> Open error input file %s", textWord);
exit(-1);
}
while(fgets(sentance,sizeof(sentance),textData) != NULL){
lineList = malloc(sizeof(wordStructType));
if(lineList == NULL){
return 0;
}
word = malloc(sizeof(wordType));
if(word == NULL){
return 0;
}
sSize = strlen(sentance) -1;
while(sSize >= 0){
word->alphabet = sentance[sSize];
if(sSize == strlen(sentance) -1)
{
word->next = NULL;
currWord = word;
}
else{
word->next = currWord;
currWord = word;
}
lineList->head = word;
sSize--;
}
if(wList->head == NULL){
wList->head = lineList;
currLine = lineList;
}
else{
currLine->next = lineList;
currLine = lineList;
}
}
fclose(textData);
return 1;
}
int FileDisplay(FileADT* wList)
{
wordStructType *current;
wordType *dAlpha;
current = wList->head;
while(current != NULL)
{
dAlpha = current->head;
while(dAlpha != NULL)
{
printf("%c", dAlpha->alphabet);
dAlpha = dAlpha->next;
}
current = current->next;
}
return 1;
}
Sample text file:
Expected output