1

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);
}
lamwaiman1988
  • 3,729
  • 15
  • 55
  • 87
  • `if( myString.compare(current->name) == 0 )` can be written as `if( myString == current->name )`. `current->count = current->count + 1;` could be written as `current->count++;` – user102008 Mar 19 '11 at 19:46

3 Answers3

3

Most obvious problem: use new and not malloc to allocate new objects: malloc doesn't call constructor and one design principle of C++ is that constructors are called before any other operations on an object.

BTW, you code look like C using only the most basic C++ features. It would never be written like this by someone knowing C++ (word would have a constructor and private member, even for people using C++ as a "better C").

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
3

You should be using the new operator instead of malloc. See the difference here. Also why use structs and typedefs when c++ allows you to make a class

Here is my version of your code, its not bug free yet but it should illustrate how to use new and classes. I will try to fix it fully and update you.

Also note that within the class structure of c++ you automatically get a this pointer with member functions that acts as a pointer to the class, so you no longer have to pass in word* theList

Edit: I updated with working code, the only thing that doesn't work is the count aspect to the list. Otherwise notice that there are two classes, List interfaces with word to create a linked list, I have not included any memory management aspects to the code (which would not be so hard using the c++ destructor, if you need such facilities please indicate so in the comments, and I will be sure to add.

#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 = 0; }

class List { public: bool isEmpty; word* theHead;

List();
List(word* aHead);
void print();
void add(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; while(current->next != NULL) { 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; } } }

int main(void) { List theList = List(); string str1 = "Hello"; string str2 = "world"; theList.add(str1); theList.add(str2); theList.print(); }

Edit: Here is the destructor to free the allocated memory, make sure to add the prototype ~List() in the class declaration:

List::~List()
{
    if (!isEmpty)
    {
        word* prev = NULL;
        word* current = theHead;
        while(current !=  NULL)
        {
            prev = current;
            current = current->next;
            delete prev;
        }
    }
}

Hope this helps.

Community
  • 1
  • 1
Jordan
  • 4,928
  • 4
  • 26
  • 39
1

For one, in Add function

if( theList->empty == false )
 {
     theList->empty = true;
     theList->name = myString;
     theList->next = 0;
 }

should be opposite - if list->empty == true, then set it to false.

As for the unhandled exception, a simple step by step 5 minute debugging session will both help you find your errors and will make you like and use the debugger. I mean it. DO TRY DEBUGGING!!!

my2c

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • how to use the Visual Studio 2010 debugger? – lamwaiman1988 Feb 27 '11 at 13:31
  • 1
    @gunbuster363: on any line in your code, put a breakpoint by pressing F9. Then press F5, which will start the app in Debug mode. One step forwaerd is F10, going inside functions is F11. Use the watch to track variable values – Armen Tsirunyan Feb 27 '11 at 13:33