Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

-can someone help me with this code, when testing it and entering a string less

ID: 674660 • Letter: #

Question

-can someone help me with this code, when testing it and entering a string less than 6 character long for each word the program stops and does not output the combinations of all letters, where as 6 and greater it works fine, i cant figure out why.

sample output desired:

enter word 1:

apple

enter word 2:

ball

letter list one:

a 1

e 1

l 1

p 2

letter list two:

a 1

b 1

l 2

both lists :

a 2

b 1

e 1

l 3

p 2

___________________

#include <iostream>
#include <string>
using namespace std;

struct sortedList
{
   char letter;
   int occurrences;
   sortedList *next;
};

sortedList *fromString(string word)
{
   sortedList *head = NULL;

   for (int i = 0; i < word.length(); i++)
   {
       sortedList *newNode = new sortedList;
       newNode->letter = word[i];
       newNode->occurrences = 1;
       newNode->next = NULL;

       if (head == NULL)
       {
           head = newNode;
       }
       else
       {
           sortedList *current = head;
           sortedList *prev = NULL;

           while (current != NULL && current->letter < newNode->letter)
           {
               prev = current;
               current = current->next;
           }

           if (current != NULL && current->letter == newNode->letter)
           {
               current->occurrences = current->occurrences + 1;
           }
           else
           {
               if (prev == NULL)
               {
                   newNode->next = head;
                   head = newNode;
               }
               else if (current == NULL)
               {
                   prev->next = newNode;
               }
               else
               {
                   prev->next = newNode;
                   newNode->next = current; }}}} return head; }


void print(sortedList *list)
{
   sortedList *current = list;
   while (current != NULL)
   {
       cout << current->letter << " " << current->occurrences << endl;
       current = current->next;
   }
   cout << endl;
}

sortedList *operator+(sortedList &list1, sortedList &list2)
{
   sortedList *newList = NULL;
   sortedList *temp1 = &list1;
   sortedList *temp2 = &list2;

while (temp1 != NULL && temp2 != NULL)
   {
       sortedList *newNode = new sortedList;

       if (temp1->letter < temp2->letter)
       {

           newNode->letter = temp1->letter;
           newNode->occurrences = temp1->occurrences;
           temp1 = temp1->next;
       }
       else if (temp1->letter > temp2->letter)
       {
           newNode->letter = temp2->letter;
           newNode->occurrences = temp2->occurrences;
           temp2 = temp2->next;
       }
       else
       {

           newNode->letter = temp1->letter;
           newNode->occurrences = temp1->occurrences + temp2->occurrences;
           temp1 = temp1->next;
           temp2 = temp2->next;
       }

       if (newList == NULL)
           newList = newNode;
       else
       {
           sortedList *current = newList;
           while (current->next != NULL)
           {
               current = current->next;
           }

           current->next = newNode;
       }
   }

   if (temp1 != NULL)
   {
       if (newList == NULL)
           newList = temp1;
       else
       {
           sortedList *current = newList;
           while (current->next != NULL)
           {
               current = current->next;
           }

           current->next = temp1;
       }
   }

   if (temp2 != NULL)
   {       if (newList == NULL)
           newList = temp2;       else
       {
           sortedList *current = newList;
           while (current->next != NULL)
           {
               current = current->next;
           }

           current->next = temp2;
       }
   }

   return newList;
}


int main()
{
string word1,word2;
   sortedList *list1;
   sortedList *list2;
   sortedList *list3;
cout<<"enter word 1: "<<endl;
cin>>word1;
cout<<"enter word 2: "<<endl;
cin>>word2;
list1=fromString(word1);
cout<<"letter list one: "<<endl;
print(list1);
list2=fromString(word2);
cout<<"letter list two: "<<endl;
print(list2);

   list3 = operator+(*list1, *list2);
cout<<"both lists: "<<endl;
print(list3);
   return 0;
}

Explanation / Answer

There was an error in pointing variable. That's the reason it was going to infinite loop.

#include <iostream>
#include <string>
using namespace std;
struct sortedList
{
char letter;
int occurrences;
sortedList *next;
};
sortedList *fromString(string word)
{
sortedList *head = NULL;
for (int i = 0; i < word.length(); i++)
{
sortedList *newNode = new sortedList;
newNode->letter = word[i];
newNode->occurrences = 1;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
}
else
{
sortedList *current = head;
sortedList *prev = NULL;
while (current != NULL && current->letter < newNode->letter)
{
prev = current;
current = current->next;
}
if (current != NULL && current->letter == newNode->letter)
{
current->occurrences = current->occurrences + 1;
}
else
{
if (prev == NULL)
{
newNode->next = head;
head = newNode;
}
else if (current == NULL)
{
prev->next = newNode;
}
else
{
prev->next = newNode;
newNode->next = current; }
               }
               }
               } return head;
               }

void print(sortedList *list)
{
sortedList *current = list;
while (current != NULL)
{
cout << current->letter << " " << current->occurrences << endl;
current = current->next;
}
cout << endl;
}
sortedList *operator+(sortedList &list1, sortedList &list2)
{
sortedList *newList = NULL;
sortedList *temp1 = &list1;
sortedList *temp2 = &list2;
while (temp1 != NULL && temp2 != NULL)
{
sortedList *newNode = new sortedList;
if (temp1->letter < temp2->letter)
{
newNode->letter = temp1->letter;
newNode->occurrences = temp1->occurrences;
temp1 = temp1->next;
}
else if (temp1->letter > temp2->letter)
{
newNode->letter = temp2->letter;
newNode->occurrences = temp2->occurrences;
temp2 = temp2->next;
}
else
{
newNode->letter = temp1->letter;
newNode->occurrences = temp1->occurrences + temp2->occurrences;
temp1 = temp1->next;
temp2 = temp2->next;
}
if (newList == NULL)
newList = newNode;
else
{
sortedList *current = newList;
while (current->next != NULL)
{
current = current->next;
}
current->next = newNode;
}
}
if (temp1 != NULL)
{
if (newList == NULL)
newList = temp1;
else
{
sortedList *current = newList;
while (current->next != NULL)
{
current = current->next;
}
current->next = temp1;
}
}
if (temp2 != NULL)
{ if (newList == NULL)
newList = temp2; else
{
sortedList *current = newList;
while (current->next != NULL)
{
current = current->next;
}
current->next = temp2;
}
}
return newList;
}

int main()
{
string word1,word2;
sortedList *list1;
sortedList *list2;
sortedList *list3;
cout<<"enter word 1: "<<endl;
cin>>word1;
cout<<"enter word 2: "<<endl;
cin>>word2;
list1=fromString(word1);
cout<<"letter list one: "<<endl;
print(list1);
list2=fromString(word2);
cout<<"letter list two: "<<endl;
print(list2);
list3 = operator+(*list1, *list2);
cout<<"both lists: "<<endl;
print(list3);
return 0;
}