I need help with being able to combine these 2 linked lists with the overloaded
ID: 3594595 • Letter: I
Question
I need help with being able to combine these 2 linked lists with the overloaded operator+.
I dont understand on how im suppose to implement it.
(more comments if possible as well, this whole linked list thing is still very confusing to me)
#include <iostream>
#include <string.h>
using namespace std;
typedef struct wordNode //word node
{
char letter;
int occurrences;
struct wordNode * next;
}sortedListNode; // typedef
sortedListNode * SearchLetter(sortedListNode ** head, char ch)
{
sortedListNode * temp = *head; // head give to temp
while (temp != NULL)
{
if ((temp->letter) == ch)
{
return temp; // if letter found return address
}
temp = temp->next; // go to next node
}
return (sortedListNode *)NULL; // or pass null
}
void AddNode(sortedListNode ** head, char ch)
{
sortedListNode * temp = NULL;
sortedListNode * temp1 = NULL;
sortedListNode * temp2 = *head;
temp = new sortedListNode;
temp->letter = ch;
temp->next = NULL;
temp->occurrences = 1;
if (*head == NULL) // if head
{
*head = temp; // give temp to it
}
else
{
temp1 = SearchLetter(head, ch); // search first
if (temp1 != NULL) //if letter is there give +1
{
temp1->occurrences = temp1->occurrences + 1;
cout << temp1->letter << " occurrences " << temp1->occurrences << " " << temp1 << endl;
delete temp; // delete allocated memory
}
else
{
while (temp2->next) //go to last
{
temp2 = temp2->next;
}
temp->next = temp2->next; // add at last
temp2->next = temp;
}
}
}
void printlist(sortedListNode * head) // print letters
{
sortedListNode * temp = head;
if (head == NULL)
{
cout << "No word" << endl;
}
while (temp != 0) //print out to end
{
cout << "Letter : " << temp->letter << " Occurance :" << temp->occurrences << endl;
temp = temp->next;
}
}
sortedListNode * fromString(char * letter) // call this function in startin
{
sortedListNode * head = NULL;
int i = 0;
for (i = 0; i<strlen(letter); i++)
{
AddNode(&head, letter[i]); // call add node with every letter
}
return head;
}
int main(void)
{
char word1[10] = { 0 }, word2[10] = { 0 };
sortedListNode *list1; // take as a pointer because memory will be dynamic
sortedListNode *list2;
sortedListNode *list3;
cout << "Enter first word: ";
cin >> word1;
cout << "Enter second word: ";
cin >> word2;
list1 = fromString(word1); // word one to list
cout << "Letter list from word one:" << endl;
printlist(list1);
list2 = fromString(word2); // word one to list
cout << "Letter list from word two:" << endl;
printlist(list2);
list3 = list1 + list2;
cout << "Letter list from both words: ";
printlist(list3);
system("pause");
return 0;
}
Explanation / Answer
Operator overloading is defined for classes and strucure and not
with pointers.So if you want to have + opertor of list, then they should
belong to some class or structure. What can be done is we can define a
class who is having a member of type pointer to sortedListNode. Then you
can create two objects list1 and list2 with word1 and word2. Then you can
add two lists as required in the program.
e.g
class List {
public:
sortedListNode *p;
List(sortedListNode *a){
p = a;
}
List operator + (List a, List b){
create a list by combining a and b
create a object from that list and return it.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.