我的代码怎么了?

时间:2011-02-27 12:59:07

标签: c++ pointers linked-list

我是C ++的新手。我真的很困惑C和C ++。我是C和Java的熟人,但不是C ++。今天我要用C ++编写一个链表程序。但是我的代码发生了什么?           感谢。

雷蒙德

结果: 09550978d.exe中0x00412656处的未处理异常:0xC0000005:访问冲突写入位置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);
}

3 个答案:

答案 0 :(得分:3)

最明显的问题:使用new而不是malloc来分配新对象:malloc不调用构造函数,C ++的一个设计原则是在对象上的任何其他操作之前调用构造函数。

BTW,您只使用最基本的C ++功能编写C代码。知道C ++的人永远不会这样写(单词会有一个构造函数和私有成员,即使是使用C ++作为“更好的C”的人)。

答案 1 :(得分:3)

您应该使用new运算符而不是malloc。查看差异here。另外,当c ++允许您制作struct

时,为什么要使用typedefclass

以下是我的代码版本,它不是免费的,但它应说明如何使用newclasses。我会尝试完全解决它并更新你。

另请注意,在c ++的类结构中,您自动获得一个带有成员函数的this指针,该成员函数充当指向该类的指针,因此您不必再传入word* theList

编辑:我使用工作代码更新,唯一不起作用的是列表的计数方面。否则请注意有两个类,List接口与word来创建一个链表,我没有在代码中包含任何内存管理方面(使用c ++析构函数不会那么难,如果你需要这样的设施请在评论中注明,我一定会补充。

#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(); }

编辑:这是释放已分配内存的析构函数,请确保在类声明中添加原型word (string name);

List();
List(word* aHead);
void print();
void add(string myString);

希望这有帮助。

答案 2 :(得分:1)

例如,在添加功能

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

应该相反 - 如果list-&gt; empty == true,则将其设置为false。

对于未处理的异常,一个简单的一步一步的5分钟调试会话将帮助您找到错误并让您喜欢并使用调试器。我是认真的。 尝试调试 !!!

MY2C