Thread: read file into Linked list, please

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

    Lightbulb read file into Linked list, please

    I have a file contains words and meaning. for example,
    one meaning1
    two meaning2

    How would I read the file into linked list in such a way that
    I would like to have each char of the word into each separate node and put the meaning into the last node which contains the last char of the word.

    For example, for above file, i would have 6 nodes that contains
    each char of the word. The node which has char "e" of the word one will also have
    the meaning in it. And the node which has char "o" of the word
    two will contains the meaning.
    How would I read file into nodes for above requirements?

    Here is my structure for creating a linked list:

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

    DictonaryTreeNode *current, *DictionaryTreeNodePtr;

    Please let me know.
    thanks.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    you've already asked this exact same question with much less information this type yet the question is still extremly trivial, how is it a dictionary if you have a meaning for "an", how about posting your code you've done so far for your homework as we'll as an example dictionary file, etc.

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

    my 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("%c", ptr->data);
    ptr = ptr->nextLevel;
    }
    printf("\n");
    }

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    18
    More description about project

    http://www.bisso.com/ggu/cis313/hw/assignment05.html

    you're not inserting a key and a value. Each node in your tree is
    associated with a letter (and a possible meaning). Given a pointer to
    a DTNode, you should be able to navigate thru the tree by looking at
    the otherLetter field and the nextLevel field until you reach a
    meaning (the meaning field).

    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".

    Let's assume our user wants to add the word "and" to the dictionary.
    After the call to addWord() our tree would look like this:

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

    With 6 -> "meaning of "and""

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Help!
    By mbk in forum C Programming
    Replies: 3
    Last Post: 01-31-2008, 03:54 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. 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