Linked list of struct

This is a discussion on Linked list of struct within the C Programming forums, part of the General Programming Boards category; I can't get my code to run right at all. This is C programming. I need to also figure out ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Concord, California, United States
    Posts
    5

    Linked list of struct

    I can't get my code to run right at all. This is C programming. I need to also figure out where to plug in to prompt user for words and make it store them. Help please. This is the assignment guidelines:
    Write a program that will do the following:
    Continuously prompt the user for words and store the words in a linked list, keeping the words in alphabetical order. Do not use an array to store the words or in any way pre-allocate any memory. You can, and should, use an array when initially reading the word from the user. Use malloc() to request memory for each word after it is read and then store the node into the linked list in its proper location.
    Use a function GetWord to read a single word from the user.
    As each word is entered into the list, print the contents of the list using a function PrintList. If the list is empty the function should print “List is empty”. The output of the PrintList function should be on the right side of the screen as shown on the sample output.
    When the user indicates that there are no more words to enter, print the final contents of the list.
    Then delete the last node in the list repeatedly, until the list is empty, calling the PrintList function after each word is removed from the list.
    Your program should allow for the possible storage of any number of words. Each word should allow a maximum size of 15 letters. Your program should make certain that this word length is not exceeded so that the program does not crash if longer words are entered by the user. If a longer word is entered it should be discarded.
    A sample run is shown below. Your data entry prompts should be on the left side of the screen as shown. Your output should look EXACTLY like the example, except for the actual words entered.
    Sample Run
    List is empty
    Enter a word: four
    four

    Do you have more words? yes or no: y
    Enter a word: score
    four
    score

    Do you have more words? yes or no: y
    Enter a word: and
    and
    four
    score

    Do you have more words? yes or no: y
    Enter a word: seven
    and
    four
    score
    seven

    Do you have more words? yes or no: y
    Enter a word: years
    and
    four
    score
    seven
    years

    Do you have more words? yes or no: y
    Enter a word: ago
    ago
    and
    four
    score
    seven
    years

    Do you have more words? yes or no: n
    Final list is:
    ago
    and
    four
    score
    seven
    years

    Deleting nodes one at a time
    ago
    and
    four
    score
    seven

    ago
    and
    four
    score

    ago
    and
    four

    ago
    and

    ago

    List is empty
    Press any key to continue

    This is the code I have so far:
    Code:
    #include "stdafx.h"
    #include 
    #include "stdlib.h"
    #include "ctype.h"
    #include "stdio.h"
    #define MAX_WORD 16
    /*function prototype*/
    void GetWord (char *word,FILE *ffptr);
    void PrintList (struct node *head);
    struct node
    {
    char word[MAX_WORD];
    struct node *next;
    };
    
    int main(void)
    {
    struct node *list = NULL;     /*points to the first word in the list */
    struct node *tmpPtr = NULL;     /*used to scan through the list*/
    struct node *prevPtr = NULL;    /*points to previous word while scanning*/
    struct node *newPtr = NULL;     /*points to new node*/
    char tempWord[MAX_WORD * 2];
    FILE *ffptr;
    if((ffptr = fopen("File Data.txt", "r")) == NULL)
    {
    printf("File could not be opened\n");
    return 1;
    }
    
    PrintList (list);
    
    GetWord(tempWord,ffptr);
    list = (struct node *)malloc(sizeof(struct node));
    strcpy(list->word, tempWord);
    list->next = NULL;
    PrintList (list);
    
    
    while(!feof(ffptr))
    { /* get next word and build node*/
    GetWord(tempWord,ffptr);
    newPtr = (struct node *)malloc(sizeof(struct node));
    strcpy(newPtr->word, tempWord);
    newPtr->next = NULL;
    tmpPtr = list;
    prevPtr = NULL;
    while (strcmp(newPtr->word, tmpPtr->word) > 0)
    {
    prevPtr = tmpPtr;
    tmpPtr = tmpPtr->next;
    if (tmpPtr == NULL)
    break;
    }
    
    if (tmpPtr == NULL)
    { 
    prevPtr->next = tmpPtr;
    }
    else if (prevPtr == NULL)
    {    
    newPtr->next = tmpPtr;
    list = newPtr;
    }
    else
    { 
    prevPtr->next = newPtr;
    newPtr->next = tmpPtr;
    }
    PrintList(list);
    fclose(ffptr);
    
    
    }
    
    printf("\t\t\t\t\t\tFinal list is:\n\n");
    PrintList(list);
    
    printf("\t\t\t\t\t\tDeleting nodes one a a time");
    while (list !=NULL)
    {
    if (list->next ==NULL)
    {    
    free (list);
    list = NULL;
    }
    else
    {
    
    tmpPtr = list;
    while (tmpPtr->next != NULL)
    {
    prevPtr = tmpPtr;
    tmpPtr = tmpPtr->next;
    }
    prevPtr->next = NULL;
    free (tmpPtr);
    }
    PrintList(list);
    }
    getch();
    
    return 0;
    }
    void GetWord(char *word, FILE *ffptr)
    {    
    
    fscanf(ffptr,"%s", word);
    while (strlen(word) > MAX_WORD-1 )
    {
    
    fscanf(ffptr,"%s", word);
    }
    }
    void PrintList (struct node *head)
    {
    struct node *curPtr;
    printf("\n\n");
    if (head == NULL)
    printf("\t\t\t\t\t\tList is empty");
    else
    {
    curPtr = head;
    printf("\t\t\t\t\t\t%s\n", curPtr->word);
    while (curPtr->next != NULL)
    {
    curPtr = curPtr->next;
    printf("\t\t\t\t\t\t%s\n", curPtr->word);
    }
    }
    printf("\n\n");
    }

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Ugh! Indent the code, please.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Concord, California, United States
    Posts
    5
    How can I edit what I've already done?

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,187
    O_o

    And also take the time to explain what "I can't get my code to run right at all." means for us.

    Pointing out mistakes is significantly easier when we know where to look for those mistakes.

    [Edit]
    If you don't have an "Edit Post" label beneath your post, for whatever reason, just post the fixed code in a new post.

    This, exactly this, being one of the occasions when making another post is acceptable.
    [/Edit]

    Soma
    Last edited by phantomotap; 10-10-2012 at 04:57 PM.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Location
    Concord, California, United States
    Posts
    5
    Well, I'm running a C++ compiler and it's giving errors, but I know the c code is wrong too.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Location
    Concord, California, United States
    Posts
    5
    The class I'm in is C++, but the teacher has extensive knowledge and background in C, so he wanted the project done in C. I'm not sure how to convert. This is the best I've got.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,187
    O_o

    The notion of "running" is entirely different than "compiling".

    If the code doesn't compile it can't run.

    *shrug*

    Anyway, C is a very ordered language; it has to know about things before you can go about using them. You have a parameter type of `node' before telling the compiler that `node' is a thing. Start with fixing that and see what happens next.

    Soma

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,187
    The class I'm in is C++, but the teacher has extensive knowledge and background in C, so he wanted the project done in C.
    Do us all a favor and point him towards this forum.

    Many of us would very much like to relieve him of that sort of mindset.

    Soma

  9. #9
    Registered User
    Join Date
    Oct 2012
    Location
    Concord, California, United States
    Posts
    5
    Oh sorry, I meant it's not compiling.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Well, for crying out loud, what errors are you getting already?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  2. Struct/Linked List Problems
    By valaris in forum C Programming
    Replies: 13
    Last Post: 08-01-2008, 10:25 AM
  3. doubly linked list tail pointer in struct
    By bazzano in forum C Programming
    Replies: 15
    Last Post: 06-11-2007, 12:31 PM
  4. Tree of Struct with Linked List
    By jjeanie in forum C Programming
    Replies: 4
    Last Post: 05-03-2007, 06:36 PM
  5. Linked list and Struct Help!!!
    By Dragoncaster131 in forum C Programming
    Replies: 1
    Last Post: 04-25-2004, 08:10 PM

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