Thread: help for Linked list needed, please

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    18

    help for Linked list needed, please

    I have a file contains the words and meaning such as below:

    eat meaning1
    run meaning2

    My codes read only first word into linked list. and I want to put the meaning of the word in each at the end of the char of word.
    For example,

    nodePtr
    |
    v
    ----- -----
    |'a'| | 'i' |
    | 1 | | 2 |
    | ----> | 0 |
    | | | | | |
    --|-- --|--
    | |
    v v
    ----- -----
    |'n'| | 't'|
    | 3 | | 4 |
    | 0 | | 0 |
    | | | | 0 |
    --|-- -----
    |
    v
    -----
    |'t'|
    | 5 |
    | 0 |
    | 0 |
    -----

    1 -> "meaning for "a""
    2 -> "meaning for "i""
    3 -> "meaning for "an""
    4 -> "meaning for "it""
    5 -> "meaning for "ant""

    These five nodes represent five words in our dictionary: "a", "an",
    "ant", "i", "it".

    and here is my code.. How can i improve this to implement above requirements? Please.

    #include <stdio.h>
    #include <malloc.h>
    #include<stdlib.h>
    void print_words();

    typedef struct DTNtag {
    char data;
    char* meaning;
    struct DTNtag* otherLetter;
    struct DTNtag* nextLevel;
    } DictonaryTreeNode;

    void main(void)
    {
    FILE *filein;
    int ch;
    DictonaryTreeNode *current, *DictionaryTreeNodePtr;

    if ((filein = fopen("info.txt", "r")) == NULL)
    { fprintf(stderr, "ERROR: Cannot open input file. ");
    exit(1); }

    DictionaryTreeNodePtr = current = (DictonaryTreeNode *) malloc(sizeof(DictonaryTreeNode) );

    while ((ch=getc(filein)) != EOF)
    {
    printf("%c\n", ch);
    current->data = ch;

    if (( current->nextLevel = (DictonaryTreeNode *) malloc(sizeof(DictonaryTreeNode))) == NULL)
    {
    fprintf(stderr, "Out of memory !\n");
    return;
    }

    current = current->nextLevel;
    }

    current->nextLevel = NULL;

    print_words(DictionaryTreeNodePtr);
    fclose(filein);


    }

    void print_words(ptr)
    DictonaryTreeNode *ptr;
    {
    while (ptr != NULL) {
    printf("Words are %c.", ptr->data);
    ptr = ptr->nextLevel;
    }
    printf("\n");
    }

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    18

    ??

    There should be no square brackets around the code tags.
    I don't know how those are appeared in message box.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    18
    Is this sound too complicated for you guys?

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    18
    Code:
    #include <stdio.h> 
    #include <malloc.h> 
    #include<stdlib.h> 
    void print_words(); 
    
    typedef struct DTNtag { 
    char data; 
    char* meaning; 
    struct DTNtag* otherLetter; 
    struct DTNtag* nextLevel; 
    } DictonaryTreeNode; 
    
    void main(void) 
    { 
    FILE *filein; 
    int ch; 
    DictonaryTreeNode *current, *DictionaryTreeNodePtr; 
    
    if ((filein = fopen("info.txt", "r")) == NULL) 
    { fprintf(stderr, "ERROR: Cannot open input file. "); 
    exit(1); } 
    
    DictionaryTreeNodePtr = current = (DictonaryTreeNode *) malloc(sizeof(DictonaryTreeNode) ); 
    
    while ((ch=getc(filein)) != EOF) 
    { 
    printf("%c\n", ch); 
    current->data = ch; 
    
    if (( current->nextLevel = (DictonaryTreeNode *) malloc(sizeof(DictonaryTreeNode))) == NULL) 
    { 
    fprintf(stderr, "Out of memory !\n"); 
    return; 
    } 
    
    current = current->nextLevel; 
    } 
    
    current->nextLevel = NULL; 
    
    print_words(DictionaryTreeNodePtr); 
    fclose(filein); 
    
    
    } 
    
    void print_words(ptr) 
    DictonaryTreeNode *ptr; 
    { 
    while (ptr != NULL) { 
    printf("Words are %c.", ptr->data); 
    ptr = ptr->nextLevel; 
    } 
    printf("\n"); 
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Like so

    Code:
    #include <stdio.h> 
    //!! #include <malloc.h>  malloc is in stdlib.h
    #include <stdlib.h> 
    
    typedef struct DTNtag { 
        char data; 
        char* meaning; 
        struct DTNtag* otherLetter; 
        struct DTNtag* nextLevel; 
    } DictonaryTreeNode; 
    
    // use proper prototypes, not pre-ANSI K&R style
    void print_words( DictonaryTreeNode *ptr);
    
    int main(void) //!! yes, main returns an int
    { 
        FILE *filein; 
        int ch; 
        DictonaryTreeNode *current, *DictionaryTreeNodePtr; 
    
        if ((filein = fopen("info.txt", "r")) == NULL) 
        {
            fprintf(stderr, "ERROR: Cannot open input file. "); 
            exit(1); 
        } 
    
    //!!    DictionaryTreeNodePtr = current = malloc(sizeof(DictonaryTreeNode) ); 
    //!! start with an empty list
        DictionaryTreeNodePtr = NULL;
        current = NULL;
    
        while ((ch=getc(filein)) != EOF) 
        { 
            printf("%c\n", ch);
            current = malloc(sizeof(DictonaryTreeNode));    //!! allocate a new node
            if ( current != NULL ) {
                // fill in the data, and add to the list
                current->data = ch;
                current->nextLevel = NULL;
                if ( DictionaryTreeNodePtr == NULL ) {
                    DictionaryTreeNodePtr = current;    // first member of the list
                } else {
                    DictonaryTreeNode   *temp = DictionaryTreeNodePtr;
                    // find the end of the list
                    while ( temp->nextLevel != NULL ) {
                        temp = temp->nextLevel;
                    }
                    // append the new node to the list
                    temp->nextLevel = current;
                }
            }
            else
            { 
                fprintf(stderr, "Out of memory !\n"); 
                return; 
            } 
        } 
    
        print_words(DictionaryTreeNodePtr); 
        fclose(filein); 
        return 0;
    } 
    
    void print_words( DictonaryTreeNode *ptr) 
    { 
        while (ptr != NULL) { 
            printf("Words are %c.", ptr->data); 
            ptr = ptr->nextLevel; 
        } 
        printf("\n"); 
    }
    Couple of points
    1. if you need malloc.h, then your compiler is really old and you should really consider upgrading to something which is ANSI compatible.
    2. You don't need to cast the result of malloc in ANSI-C. If you think you do, check that you're not using a C++ compiler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  2. Doubly-Linked List
    By jgs in forum C Programming
    Replies: 7
    Last Post: 04-18-2005, 01:39 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Linked List help needed
    By EDL in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2002, 10:01 AM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM