need help on linked list

cpp Zimbabwe
  • 14 years ago

    need help on function in bold causing program to crash:

    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>




    struct doc_words
    {
     int numWords;
     char words[30];
    struct doc_words *next;
    };





    char boolean = 'F';

    struct doc_words *head=NULL;;
    struct doc_words *current;
    //struct doc_words *check;
    struct doc_words* newNode;


    //char *calloc();

    struct doc_words *getmem()
    {
    struct doc_words *p;
    //p = malloc(sizeof(struct doc_words));
    p = (struct doc_words*)malloc(sizeof(struct doc_words));
    if(!p)
    {printf("out of memory");
    return(0);
    }
    else
    return(p);
    }










     


    void AddWords( char text[30])
    {
     


     

                 newNode=getmem();
                 strncpy(newNode->words,text, 29);
                 newNode->numWords=0;
                 newNode->next=head;
                 head=newNode;



     

    }

    void check (char word[30])
    {
    if (boolean =='F')
        {
         AddWords( word);
         boolean ='T';
         goto s;
        }






    current=head;
    // char temp[30];
     //char a[30];
    // strncpy(temp,word,29);
    // strncpy( a,current->words,29);
          printf("the word current is: %s and count = %d",current->words,current->numWords);
          do             
          {






            if ((strcmp(current->words,word))==0)
           //if ((strcmp(a,temp))==0)
              {
                current->numWords++;
                break;
              }
           else
               {
                current=current->next;
               }
          }while (current!=NULL);









          if(current->next==NULL)
             {
               AddWords( word);
             }
    s:
    printf("");
    }





    void extract_words_from_file()
    {

    char word[30];

    FILE *ifp;

    if ((ifp=fopen("C:\\collins_tirivamwe\\DSA\\text.txt","r"))!=NULL)
    {

    while (fscanf(ifp,"%s",word)!=EOF)

    {
    printf("%s",word);
    check(word);
    }
    fclose(ifp);
    }
    else
    printf("error");
    }







    void read()
    {
    printf("words in list");
    struct doc_words *current= head;
    while (current!=NULL)
    {




    printf("%s\n",current->words);
    current=current->next;
    }

    }

    int getCount()
    {
    int count=0;
    struct doc_words *current= head;
    while (current!=NULL)
    {
    count++;
    current=current->next;






    }

    printf("\n %d",count);
    return (count);
    }

    /*
    int getCount(char word[30])
    {
    int count=0;
    struct doc_words *p = head;
    while (p!=NULL)
    {
    if (strcmp(p->word,word)==0)
    {
    count++;
    p=p->next;
    }
    else
    {
    p=p->next;
    }
    }
    return(count);
    }

















     

    */

    int main()
    {
    int p=0;
    extract_words_from_file();


    p=getCount();
    // printf("\nnumber of words are %d",p);
    read();
    system("PAUSE");
    return 0;



    }

  • 14 years ago

    Your error is in setting the current pointer to the current next pointer. If you tell the program to print this value, you will notice that you are setting the current to point at 0, which it cannot do.

  • 14 years ago

    //#include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #define NUL 0




    struct doc_words
    {
     int numWords;
     char words[30];
    struct doc_words *next;
    };




    char boolean = 'F';

    struct doc_words *head=0;
    struct doc_words *tail=0;
    //head=tail;
    struct doc_words *current;
    //struct doc_words *check;
    struct doc_words* newNode;




    //char *calloc();

    struct doc_words *getmem()
    {
    struct doc_words *p;
    //p = malloc(sizeof(struct doc_words));
    p = (struct doc_words*)malloc(sizeof(struct doc_words));
    if(!p)
    {printf("out of memory");
    return(0);
    }
    else
    return(p);
    }










     


    void AddWords( char text[30])
    {
     


     

                 newNode=getmem();
                 strncpy(newNode->words,text, 29);
                 newNode->numWords=1;
                 newNode->next=head;
                 head=newNode;
                 //tail=newNode;





    }

    void check (char word[30])
    {
    if (boolean =='F')
        {
         AddWords( word);
         boolean ='T';
         goto s;
        }
    else
    {
    current=head;
    // char temp[30];
     //char a[30];
    // strncpy(temp,word,29);
    // strncpy( a,current->words,29);
       //   printf("the word current is: %s and count = %d\n",current->words,current->numWords);
          do             
          {
















            if ((strcmp(current->words,word))==0)
           //if ((strcmp(a,temp))==0)
              {
                current->numWords++;
                break;
              }




              
                current=current->next;
              
          }while (current!=0);


          //if(current->next==0)
            // {
            //   AddWords( word);
           //  }
    }      
    s:
    printf("");
    }






    void extract_words_from_file()
    {

    char word[30];

    FILE *ifp;

    if ((ifp=fopen("C:\\collins_tirivamwe\\DSA\\text.txt","r"))!=0)
    {

    while (fscanf(ifp,"%s",word)!=EOF)

    {
    printf("%s",word);
    check(word);
    }
    fclose(ifp);
    }
    else
    printf("error");
    }







    void read()
    {
    printf("words in list");
    struct doc_words *current= head;
    while (current!=tail)
    {




    printf("%s\n",current->words);
    current=current->next;
    }

    }

    int getCount()
    {
    int count=0;
    struct doc_words *current= head;
    while (current!=0)
    {
    count++;
    current=current->next;






    }

    printf("\n %d",count);
    return (count);
    }

    /*
    int getCount(char word[30])
    {
    int count=0;
    struct doc_words *p = head;
    while (p!=0)
    {
    if (strcmp(p->word,word)==0)
    {
    count++;
    p=p->next;
    }
    else
    {
    p=p->next;
    }
    }
    return(count);
    }

















     

    */

    int main()
    {
    int p=0;
    extract_words_from_file();


    p=getCount();
    // printf("\nnumber of words are %d",p);
    read();
    system("PAUSE");
    return 0;



    }

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Linux is only free if your time has no value” - Jamie Zawinski