Like Tree1Likes

C programming-Linked list problem

This is a discussion on C programming-Linked list problem within the C Programming forums, part of the General Programming Boards category; Paste your entire complete program as it currently stands....

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Paste your entire complete program as it currently stands.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  2. #17
    Registered User
    Join Date
    May 2005
    Posts
    12
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define SIZE 120
    
    struct charRecord
    {
       char letter;
       struct charRecord *nextNode;
    };
    
    typedef struct charRecord charNode;
    
    FILE* openFile(char *filename, char *mode);
    void loadChar(char *filename, charNode **start);
    void applyRules(charNode **start);
    void printList ( charNode *start);
    void freeList(charNode **start);
    charNode* createNode(char nodeLetter);
    
    int main(int argc, char *argv[])
    {
       charNode *start =NULL;
      
       /* Check command line arguments */
       if (argc != 2)
       {
          printf("\nSyntax : euro file\n");
          return 0;
       }
    
       loadChar(argv[1], &start);
    
       printList(start);
       applyRules(&start);
       printList(start);
    
       freeList(&start);
    
       return 0;
    }
    
    /*
    Your program will get name of the file containning the english
    to be converted as a command line argument
    */
    
    FILE* openFile(char *filename, char *mode)
    {
       /* open a FILE stream to filename with given mode */
    
       FILE *file = fopen(filename, mode);
    
       if (file == NULL)
       {
          printf("ERROR - unable to read %s\n", filename);
          exit(0);
       }
    
       return file;
    }
    
    /* it will tren read the file and build up the English message,
    character by character in a SINGLE linked list.
    Each Node in the list will contain a single chracter.
    */
    
    void loadChar(char *filename, charNode **start)
    {
       char c;
    
       /* Pointer to the file. FILE is a  structure  defined in <stdio.h>*/
       charNode *temp, *tail = *start;
    
       /* Open the file - no error checking done */
       FILE *fin = openFile(filename, "r");   
       
       /* Read one character at a time, checking */
       while ((c = fgetc(fin)) != EOF)
       {
          temp = createNode(c) ;
          if(tail == NULL)
          {
             *start = tail = temp ;
          }
          else
          {
             tail->nextNode = temp;
             tail = temp;
          }
       }
    
       if (!feof(fin))
       {
          printf("ERROR - unable to read lines in %s\n", filename);
          exit(0);
       }
       fclose(fin);
    }
    
    /*
    you will need to apply the rules to the message contained in the link list.
    */        
    
    void applyRules(charNode **start)
    {
    
       charNode *temp;
    
       temp = *start;
    
       while (temp != NULL && temp->nextNode != NULL )
       {
          if(temp->letter== 'w')
          {
             temp->letter ='v';
          }
          else if(temp->letter== temp->nextNode->letter)
          {
             temp->nextNode->letter =NULL;
          }
          else if(temp->letter== 'p' && temp->nextNode->letter == 'h')
          {
             temp->letter ='f';
             temp->nextNode->letter =NULL;
          }
          else if(temp->letter== 't' && temp->nextNode->letter == 'h')
          {
             temp->letter ='z';
             temp->nextNode->letter =NULL;
          }
          else if(temp->letter== 'o' && temp->nextNode->letter == 'u')
          {
             temp->letter ='u';
             temp->nextNode->letter =NULL;
          }
          else if(temp->letter== 'e' && temp->nextNode->letter == 'a')
          {
             temp->letter ='e';
             temp->nextNode->letter = NULL;
          }
          else if(temp->letter== 'c')
          {
             if(temp->letter== 'c' && (temp->nextNode->letter == 'e'
                                  || temp->nextNode->letter == 'i'
                                  || temp->nextNode->letter == 'y'))
             {
                temp->letter ='s';
             }
             else 
             {
                temp->letter ='k';
             }
          }
          else if( temp->letter== 'e' && temp->nextNode->letter==' ')
          {
             temp->letter=NULL;
          }
          else if(temp->letter== 'e' && temp->nextNode->letter == 'd'
                                     && temp->nextNode->nextNode->letter == ' ')
          {
             temp->letter = 'd';
             temp->nextNode->letter = NULL;
          }
    
          temp = temp->nextNode;
       }
    }
    
    /* the program should print out the the Euro english */
    
    void printList ( charNode *start)
    {
       /*
         print out the contents of the linked list
         start with the first item added    
       */
    
       while (start != NULL)
       {
          printf("%c", start->letter);
          start = start->nextNode;
       }
       printf("\n");
    }
    
    /* the programm should free all the memory used in the linked list */
    
    void freeList(charNode **start)
    {
    
       /* free up all word records in linked list */
    
       charNode *temp;
    
       while (*start != NULL)
       {
          temp = *start;
          *start = temp->nextNode;
          free(temp);
       }
    }
    
    /* the program allocate memory for the linked list */
    
    charNode* createNode(char nodeLetter)
    {
    
       /* dynamicaly allocate memory for an charNode
          make sure it is initialised correctly
       */
    
       charNode *temp= (charNode *) malloc(sizeof(charNode));
    
       if (temp == NULL)
       {
          printf("WARNING - Memory allocation error\n");
          exit(EXIT_FAILURE);
       }
    
       temp->letter = nodeLetter;
       temp->nextNode = NULL;
      
       return temp;
    }
    here is my current code
    now i need to ignore digits and delete the e at the end of the word whenit is 3 chracters or more

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well a few fixes - watch for the !! comments
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define SIZE 120
    
    struct charRecord
    {
       char letter;
       struct charRecord *nextNode;
    };
    
    typedef struct charRecord charNode;
    
    FILE* openFile(char *filename, char *mode);
    void loadChar(char *filename, charNode **start);
    void applyRules(charNode **start);
    void printList ( charNode *start);
    void freeList(charNode **start);
    charNode* createNode(char nodeLetter);
    
    int main(int argc, char *argv[])
    {
       charNode *start =NULL;
    
       /* Check command line arguments */
       if (argc != 2)
       {
          printf("\nSyntax : euro file\n");
          return 0;
       }
    
       loadChar(argv[1], &start);
    
       printList(start);
       applyRules(&start);
       printList(start);
    
       freeList(&start);
    
       return 0;
    }
    
    /*
    Your program will get name of the file containning the english
    to be converted as a command line argument
    */
    
    FILE* openFile(char *filename, char *mode)
    {
       /* open a FILE stream to filename with given mode */
    
       FILE *file = fopen(filename, mode);
    
       if (file == NULL)
       {
          printf("ERROR - unable to read %s\n", filename);
          exit(0);
       }
    
       return file;
    }
    
    /* it will tren read the file and build up the English message,
    character by character in a SINGLE linked list.
    Each Node in the list will contain a single chracter.
    */
    
    void loadChar(char *filename, charNode **start)
    {
       int c;/*!! needs to be an int, to be able to store all chars AND EOF */
    
       /* Pointer to the file. FILE is a  structure  defined in <stdio.h>*/
       charNode *temp, *tail = *start;
    
       /* Open the file - no error checking done */
       FILE *fin = openFile(filename, "r");
    
       /* Read one character at a time, checking */
       while ((c = fgetc(fin)) != EOF)
       {
          temp = createNode(c) ;
          if(tail == NULL)
          {
             *start = tail = temp ;
          }
          else
          {
             tail->nextNode = temp;
             tail = temp;
          }
       }
    
       if (!feof(fin))
       {
          printf("ERROR - unable to read lines in %s\n", filename);
          exit(0);
       }
       fclose(fin);
    }
    
    /*
    you will need to apply the rules to the message contained in the link list.
    */
    
    void applyRules(charNode **start)
    {
    
       charNode *temp;
    
       temp = *start;
    
       while (temp != NULL && temp->nextNode != NULL )
       {
          if(temp->letter== 'w')
          {
             temp->letter ='v';
          }
          else if(temp->letter== temp->nextNode->letter)
          {
             temp->nextNode->letter ='\0';  /*!! not NULL */
          }
          else if(temp->letter== 'p' && temp->nextNode->letter == 'h')
          {
             temp->letter ='f';
             temp->nextNode->letter ='\0';  /*!! not NULL */
          }
          else if(temp->letter== 't' && temp->nextNode->letter == 'h')
          {
             temp->letter ='z';
             temp->nextNode->letter ='\0';  /*!! not NULL */
          }
          else if(temp->letter== 'o' && temp->nextNode->letter == 'u')
          {
             temp->letter ='u';
             temp->nextNode->letter ='\0';  /*!! not NULL */
          }
          else if(temp->letter== 'e' && temp->nextNode->letter == 'a')
          {
             temp->letter ='e';
             temp->nextNode->letter = '\0';  /*!! not NULL */
          }
          else if(temp->letter== 'c')
          {
             if(temp->letter== 'c' && (temp->nextNode->letter == 'e'
                                  || temp->nextNode->letter == 'i'
                                  || temp->nextNode->letter == 'y'))
             {
                temp->letter ='s';
             }
             else 
             {
                temp->letter ='k';
             }
          }
          else if( temp->letter== 'e' && temp->nextNode->letter==' ')
          {
             temp->letter='\0';  /*!! not NULL */
          }
          else if(temp->letter== 'e' && temp->nextNode->letter == 'd'
                                     && temp->nextNode->nextNode->letter == ' ')
          {
             temp->letter = 'd';
             temp->nextNode->letter = '\0';  /*!! not NULL */
          }
    
          temp = temp->nextNode;
       }
    }
    
    /* the program should print out the the Euro english */
    
    void printList ( charNode *start)
    {
       /*
         print out the contents of the linked list
         start with the first item added
       */
    
       while (start != NULL)
       {
          /*!! check for deleted characters */
          if ( start->letter != '\0' ) printf("%c", start->letter);
          start = start->nextNode;
       }
       printf("\n");
    }
    
    /* the programm should free all the memory used in the linked list */
    
    void freeList(charNode **start)
    {
    
       /* free up all word records in linked list */
    
       charNode *temp;
    
       while (*start != NULL)
       {
          temp = *start;
          *start = temp->nextNode;
          free(temp);
       }
    }
    
    /* the program allocate memory for the linked list */
    
    charNode* createNode(char nodeLetter)
    {
    
       /* dynamicaly allocate memory for an charNode
          make sure it is initialised correctly
       */
    
       charNode *temp= /*!! no casting malloc in C - see FAQ */malloc(sizeof(charNode));
    
       if (temp == NULL)
       {
          printf("WARNING - Memory allocation error\n");
          exit(EXIT_FAILURE);
       }
    
       temp->letter = nodeLetter;
       temp->nextNode = NULL;
    
       return temp;
    }
    
    
    Example output
    ./a.out results.txt
    #include <stdio.h>
    int main ( int argc, char *argv[] ) {
      char buff[BUFSIZ];
      while ( fgets ( buff, BUFSIZ, stdin ) != NULL ) {
        fputs ( buff, stdout );
      }
      return 0;
    }
    
    #inklud <stdio.h>
    int main ( int argk, khar *argv[] ) {
     khar buf[BUFSIZ];
     vhil ( fgets ( buf, BUFSIZ, stdin ) != NUL ) {
      fputs ( buf, stdut );
     }
     return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    Registered User
    Join Date
    May 2005
    Posts
    12
    Thanz alot for your help.
    Ana likes this.

Page 2 of 2 FirstFirst 12
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, 01:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21