I am new to C++. I am really confused between C and C++. I am familair with C and java, but not C++. Today I am going to write a linked list program using C++. But to what happened to my code ??? Thanks.
Raymond
the result: Unhandled exception at 0x00412656 in 09550978d.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.
#include <iostream>
#include <string>
using namespace std;
struct word
{
bool empty;
string name;
int count;
word* next;
};
typedef struct word word;
word* create(word* theList)
{
word* head = (word*)malloc(sizeof(word));
head->empty = false;
head->name = "";
head->next = 0;
return head;
}
void print(word* theList)
{
word* current = theList;
while(current!=0)
{ cout << current->name << " : " << current->count << " \n" ;
current = current->next;
}
}
void add(string myString, word* theList)
{
//word* newWord = (word*)malloc(sizeof(word));
if( theList->empty == false )
{
theList->empty = true;
theList->name = myString;
theList->next = 0;
}
else
{
word* current = theList;
while(current->next!=0)
{
current = current->next;
}
word* newWord = (word*)malloc(sizeof(word));
newWord->empty = true;
newWord->name = myString;
newWord->next = 0;
current->next = newWord;
}
}
int main(void)
{
word* theList = 0;
theList = create(theList);
add("Hello", theList);
//add("world", theList);
}
#include <iostream>
#include <string>
using namespace std;
class word
{
public:
string name;
int count;
word *next;
word (string name);
};
word::word (string myName)
{
name = myName;
next = NULL;
count = 1;
}
class List
{
public:
bool isEmpty;
word* theHead;
List();
List(word* aHead);
void print();
void add(string myString);
void search(string myString);
};
List::List()
{
isEmpty = true;
}
List::List(word* aHead)
{
isEmpty = false;
theHead = aHead;
}
void List::add(string myString)
{
word* newWord = new word(myString);
if (isEmpty == true)
{
isEmpty = false;
theHead = newWord;
}
else
{
word* current = theHead;
if ( current->next == NULL)
{
if( myString.compare(current->name) == 0 )
{
current->count = current->count + 1;
return;
}
}
else
{
while ( current->next != NULL )
{
if( myString.compare(current->name) == 0 )
{
current->count = current->count + 1;
return;
}
current = current->next;
}
}
current->next = newWord;
}
}
void List::print ()
{
if (isEmpty)
{
cout << "nothing in the list";
}
else
{
word* current = theHead;
while(current != NULL)
{
cout << current->name << " : " << current->count << " \n" ;
current = current->next;
}
}
}
void List::search(string myString)
{
if (isEmpty)
{
cout << "The word : " << myString << " is not in the List.\n";
}
else
{
word* current = theHead;
while( current != NULL )
{
if( myString.compare(current->name) == 0 )
{
cout << "The word : " << myString << " is in the List.\n";
return;
}
else
{
current = current->next;
}
}
cout << "The word : " << myString << " is not in the List.\n";
}
return;
}
int main(void)
{
List theList = List();
string str1 = "Hello";
string str2 = "world";
theList.add(str1);
theList.add(str2);
theList.add(str1);
theList.search("Hello");
theList.search("You");
theList.print();
int i;
scanf("%d", &i);
}