Link to home
Start Free TrialLog in
Avatar of hwg193
hwg193

asked on

HELP wit linked list insert

I can't fugure this out.  When I insert a second item this program crashes.  Any help would be useful.

What it is supposed to do is have a user enter a word.
1.) If the word exists add one to the count
2.) If the word does not exist, add the word and add one to the count.


#include <iostream>
#include <cstddef> // FOR NULL USE
#include <string>

using namespace std;

struct WordList
{     int count;
     WordList *link;
     char item[];
};
typedef WordList* NodePtr;

void head_insert(NodePtr& head, char inWord[]);
void add_word (NodePtr& head);
void show_list(NodePtr& head);
void delete_list(NodePtr& head);

char inWord[20];
int aNumber=0;

int main()
{     NodePtr head=NULL;

     cout<<"Enter a word in lower case "<<endl;
     cout<<"Enter done when finished"<<endl;
     cin>>inWord;
     if(strcmp(inWord,"done")!=0)
          head_insert(head, inWord);
         
     add_word(head);
     show_list(head);
     delete_list     (head);

     return 0;
}
//*******************************************************
void add_word (NodePtr& head)
{     NodePtr currPtr=head;
     NodePtr prevPtr;
     NodePtr newNodePtr;
     
     
     cout<<"Enter a word in lower case "<<endl;
     cout<<"Enter done when finished"<<endl;
     cin>>inWord;
     if(strcmp(inWord,"done")!=0)
          {     if (strcmp(inWord,currPtr->item),0)
                    currPtr->count+1;
               else
                    {newNodePtr=new WordList;
                    strcpy(newNodePtr->item,inWord);
                    prevPtr=NULL;
                    currPtr=head;
                    while (currPtr!=NULL&&inWord>currPtr->item)
                    {     prevPtr=currPtr;
                         currPtr=currPtr->link;
                    }
     
                    newNodePtr->link=currPtr;
                    if(prevPtr==NULL)
                         head-newNodePtr;
               else
                    prevPtr->link=newNodePtr;
          }
          }
}
//*******************************************************
void head_insert(NodePtr& head, char inWord[])
// INITIALIZES LINK LIST, SETS HEAD TO NULL
{     NodePtr temp_ptr;
     temp_ptr=new WordList;

     strcpy(temp_ptr->item,inWord);
     temp_ptr->count=1;
     
     temp_ptr->link=head;
     head=temp_ptr;
}
//*******************************************************
void show_list(NodePtr& head)
// DISPLAYS LIST TO SCREEN
{     NodePtr here=head;
     char tmpout;
         
     while (here!=NULL)
     {    
          if (tmpout!=here->item[0])
               tmpout=here->item[0];
          else
               {cout<<"Letter "<<tmpout<<" words"<<endl;
               cout<<here->item<<" - "<<endl;
               cout<<here->count<<endl;
               here=here->link;
               }
     }
}
//*******************************************************
void delete_list(NodePtr& head)
// DELETES LIST TO RESTORE MEMORY
{     NodePtr here=head;
     NodePtr current=head;
     
     while (current !=NULL)
     {     current=current->link;
          delete here;
          here=current;
     }
}
//*******************************************************
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Juan Carlos
#include <iostream>
#include <cstddef> // FOR NULL USE
#include <string.h>

//using namespace std;

struct WordList
{     int count;
    WordList *link;
    char *item;
};
typedef WordList* NodePtr;

void head_insert(NodePtr* head, char inWord[]);
void add_word (NodePtr* head);
void show_list(NodePtr& head);
void delete_list(NodePtr* head);

char inWord[20];
int aNumber=0;

int main()
{     NodePtr head=NULL;

    cout<<"Enter a word in lower case "<<endl;
    cout<<"Enter done when finished"<<endl;
    cin>>inWord;
    if(strcmp(inWord,"done")!=0)
         head_insert(&head, inWord);
         
    add_word(&head);
    show_list(head);
    delete_list     (&head);

    return 0;
}
//*******************************************************
void add_word (NodePtr *head)
{    
      NodePtr currPtr=NULL;
    NodePtr prevPtr;
    NodePtr newNodePtr;
      while(1)
           {
           currPtr=*head;
           cout<<"Enter a word in lower case "<<endl;
           cout<<"Enter done when finished"<<endl;
           cin>>inWord;
           if(strcmp(inWord,"done")!=0)
                    {
                    while (currPtr)
                         {
                              if (strcmp(inWord,currPtr->item)==0)
                                   {
                                   currPtr->count++;
                                   break;
                                   }
                              else
                                   {
                                   currPtr = currPtr->link;
                                   }
                         }
                    if (currPtr==NULL)
                         {
                         newNodePtr=new WordList;
                      newNodePtr->item = new char[strlen(inWord) + 1];
                      newNodePtr->count = 1;
                      newNodePtr->link = NULL;
                      strcpy(newNodePtr->item,inWord);
                          currPtr  = newNodePtr;
                         currPtr->link = *head;
                         *head=currPtr;
                      //(*head)->link = currPtr;
                    }
                }
           else
                break;
           }
}
//*******************************************************
void head_insert(NodePtr *head, char inWord[])
// INITIALIZES LINK LIST, SETS HEAD TO NULL
     {    
      if (*head==NULL)
           {
           *head=new WordList;
               (*head)->item = new char[strlen(inWord) + 1];
               strcpy((*head)->item,inWord);
               (*head)->count=1;
               (*head)->link =NULL;
           }
     }

//*******************************************************
void show_list(NodePtr& head)
// DISPLAYS LIST TO SCREEN
{     NodePtr here=head;
    char tmpout;
         
    while (here!=NULL)
    {    
         if (tmpout!=here->item[0])
              tmpout=here->item[0];
         else
              {cout<<"Letter "<<tmpout<<" words"<<endl;
              cout<<here->item<<" - "<<endl;
              cout<<here->count<<endl;
              here=here->link;
              }
    }
}
//*******************************************************
void delete_list(NodePtr* head)
// DELETES LIST TO RESTORE MEMORY

     {
          NodePtr here=*head;
          NodePtr current=*head;
   
    while (current !=NULL)
    {     current=current->link;
         delete here;
         here=current;
    }
}
//*******************************************************
Avatar of Salte
Salte

jcgd,

I believe what hwg needs is not just a dump of a program that works, he had so many problems that what I think is more valuable to him is some explanation why his code has problems and what your code does to address those problems.

Please post some comments and/or notes here and there along with your code.

Alf
Avatar of hwg193

ASKER

Thanks for the quick response Alf.

After making the change, char item[20], and fixing those typos I had in the program, I was able to get it to work without crashing.

I know I didnt have the loop working in the add_word function, and with it crashing on the sedond word entry, it was hard to work on the logic of the rest of the problem, such as making sure the insert was being sorted correctly.

The C++ class that I am in is just lecture with no lab time. So when i have a problem, I e-mail the instructor and it takes a day or two for him to e-mail me back.