Thread: Problem either on display method or structure..

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    Problem either on display method or structure..

    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:

    Code:
      This is a string
    Expected output

    Code:
      This is a string

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    The problem is that you aren't making a new node for each character, you're reusing a single node, so the list is infinitely circular. Try this:
    Code:
    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;
        }
    
        lineList->next = NULL;
        sSize = strlen(sentance) -1;
    
        while(sSize >= 0){
          word = malloc(sizeof(wordType));
          if(word == NULL){
            return 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;
    }
    I also fixed a bug where you don't terminate your list with NULL.
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    Hey thanks, it works right now. Gotta remember where I make my new node...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  2. Problem with custom structure
    By stickman in forum C++ Programming
    Replies: 9
    Last Post: 10-14-2006, 02:57 PM
  3. Caught a problem MS3D structure setup.
    By Shamino in forum Game Programming
    Replies: 3
    Last Post: 03-25-2006, 06:03 PM
  4. another structure problem
    By hostensteffa in forum C++ Programming
    Replies: 6
    Last Post: 05-06-2002, 03:25 AM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 07:54 AM