getword and linked lists

This is a discussion on getword and linked lists within the C Programming forums, part of the General Programming Boards category; so i have a funtion to read words and put thm all in lowercas leteering.how do i insert the wordsv ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    56

    getword and linked lists

    so i have a funtion to read words and put thm all in lowercas leteering.how do i insert the wordsv into a link list ?i have declared the link list and wrote a function to add the words in place.i jus cant figure out how to link the two so when the word is "lower cased" i need to add it in place into a link list

    //code for gettin word from file
    Code:
    int getword(FILE*in,char string[]){
                         char ch;
                         int n=0;
                         //read over blank space
                       while(!isalpha(ch=getc(in)) && ch!='\0')
                         if(ch==EOF) return 0;
                         string[n++]=tolower(ch);
                         while(isalpha(ch=getc(in)) && ch!='\0')
                         if(n<20)string[n++]=tolower(ch);
                         string[n]='\0';
                         return 1;
                         }
    and this is myaddinplace functions which supposed to add wordsread into a link list in ascending order.
    Code:
    Nodeptr addinplace(Nodeptr top,char string[]){
            
            Nodeptr str,curr,prev,makenode(char[]);
         
            
            
            str=makenode(string);
            prev=NULL;
            curr=top;
    while(curr!=NULL && strcmp(curr->word,string)<0){
            prev=curr;
            curr=curr->next;
            }
    if(prev==NULL){
                   str->next=top;
                   return str;
                   }
             str->next=curr;
             prev->next=str;
             return top;
             }
    im not sure how to implement them

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    The word's inserted into a struct element whose size is fixed or dynamic. Dealing with variable length words means it's best to go with a dynamically sized struct member.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    i use dynamic storage maloc
    i have a function that created the node
    Code:
    Nodeptr makenode(char string[]){
          
            Nodeptr str=(Nodeptr)malloc(sizeof(Node));
            strcpy(str->word,string);
            str->next=NULL;
            return str;
            }

    the problem is how to store the words into the linklist?

    i was thinkin of using this call to the functions in main i declaredstart as a pointer to my structure
    Code:
    typedef struct node{
                        char word[20];
                        int count;
                        struct node *next;
                        }Node,*Nodeptr;
                        
    main...
    
    Nodeptr start;
    
     while(getword(in,wod)!=0){
                                  addinplace(start,wod);
                                  }
    i have a tested working fn that prints lists ,but when i call it with start as the arument it doesnt print anything.it seems as though start wasnt touched in the addinplace fn so im wondering if i am using it properly.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Allocate the very first node of the list, its *next points to NULL.
    Next node malloc()'ed is placed at the head of the list, its *next points to the first node, so on and so forth.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    i understand all that but what i dont get it how to put the words into the linked list
    i know this is a bit cubersome but here it is.it runs and compiles but there is nothing in my start list.i dont know where i am going wrong


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    
    FILE*in;
    
    
    typedef struct node{
                        char word[20];
                        int count;
                        struct node *next;
                        }Node,*Nodeptr;
                        
                        
    //FUNCTIONS=======================================================================================================
                        
     Nodeptr addinplace(Nodeptr ,char[] ); 
     Nodeptr makenode(char[] );                  
     void printlist(Nodeptr );
    
    int getword(FILE*,char[]);
     Nodeptr search(Nodeptr,char[] );
     
    //================================================================================================================
    int main(){
        in=fopen("shims.txt","r");
        if(in==NULL){
                     printf("Filenot found!! \n");-
                     system("pause");
                     exit(1);
                     }
        char wod[20],string[20];
        int cmp,x=1;
        Nodeptr start=NULL,srch;
      
      
        while(getword(in,wod)!=0){
                                
                                addinplace(start,wod);  
       
     printf("%s ",wod);
        
        
    }
         
        
        printf("!\n");
        printlist(start);            
       printf("5");
        printf("\n");
        fclose(in);                    
        system("pause");
        return 0;
    }
    //making a node to hold the word
    Nodeptr makenode(char string[]){
          
            Nodeptr str=(Nodeptr)malloc(sizeof(Node));
            strcpy(str->word,string);
            str->next=NULL;
            return str;
            }
     //adds words inorder into th3 list       
     
    Nodeptr addinplace(Nodeptr top,char string[]){
            
            Nodeptr str,curr,prev,makenode(char[]);
         
            
            
            str=makenode(string);
            prev=NULL;
            curr=top;
    while(curr!=NULL && strcmp(curr->word,string)<0){
            prev=curr;
            curr=curr->next;
            }
    if(prev==NULL){
                   str->next=top;
                   return str;
                   }
             str->next=curr;
             prev->next=str;
             return top;
             }
             
             
     //prints linked structures                         
    void printlist(Nodeptr top){
         while(top!=NULL){
                          printf("%s\n",top->word);
                          top=top->next;
                          }
                          }
    
    
                     
                                     
     
        
    //stores the next word if any,in wrd;word is converted to lowercase 
    //return 1 if  found  0 otherwise.  
    int getword(FILE*in,char string[]){
                         char ch;
                         int n=0;
                         //read over blank space
                       while(!isalpha(ch=getc(in)) && ch!='\0')
                         if(ch==EOF) return 0;
                         string[n++]=tolower(ch);
                         while(isalpha(ch=getc(in)) && ch!='\0')
                         if(n<20)string[n++]=tolower(ch);
                         string[n]='\0';
                         return 1;
                         }
                          
      //searches through the list 
      
     Nodeptr search(Nodeptr top,char string[]){
             while(top!=NULL && (strcmp(top->word,string)!=0))
             top=top->next;
             return top;
             }

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Format and indent your code before posting as it's impossible to make out what it's doing the way it's now.

  7. #7
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    There are some good guidelines for indentation here, if you need them.

    When you say "what i don't get is how to put words into the linked list", I believe that itCbitC describes it perfectly well. A basic overview of a linked list:

    Code:
    /* data type for the example linked list */
    struct example_linked_list_t {
        int data;
        struct example_linked_list_t *next;
    } *head;
    /* Note that global variables are auto-initialized to 0, or NULL. If this is a local type, head should be initialized to NULL. */
    . . .
    /* Insertion section, trying to insert some_variable into the last position of the linked list. */
    
    /* First check if the head of the linked list is unallocated. If it is, then put the variable into the first position of the list. */
    if(head == NULL) {
        /* . . . allocate a new example_linked_list_t for the head element . . . */
        head = malloc(sizeof(struct example_linked_list_t));
        /* . . . set the next to NULL . . . */
        head->next = NULL;
        /* . . . set the data. */
        head->data = some_variable;
    }
    else { /* else there is some data in the linked list already */
         struct example_linked_list_t *p;
         /* let's start at the first element and work through to find the end . . . */
         p = head;
         /* NOTE: if head->next is NULL, this will still work . . . just assign p to head. */
         while(p->next != NULL) p = p->next;
         /* allocate the next element and set the next and data elements. */
         p->next = malloc(sizeof(struct example_linked_list_t));
         p->next->next = NULL;
         p->next->data = some_variable;
    }
    Make sense?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    yeah ithink i got it.thnx guys!

Popular pages Recent additions subscribe to a feed

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