【问题标题】:Problems with singly linked list using classes使用类的单链表问题
【发布时间】:2012-01-15 10:51:06
【问题描述】:

我正在为使用多个文件和类的单链表制作程序。

我必须有一个 Node.h、LinkedList.h、Node.cpp、LinkedList.cpp 和一个 main.cpp

我遇到了其他问题,但现在我的 printList() 函数只打印“List()”而不是“List(node 1, node2, etc...)”

这是我的代码:(我无法更改 Node.h 和 LinkedList.h 文件)

节点.h:

//
//  Node.h
//  Linked Lists
//

#ifndef Linked_Lists_Node_h
#define Linked_Lists_Node_h

class Node
{
public:
    Node(int data);
    int data;
    Node *next;

};

#endif

LinkedList.h:

//
//  LinkedList.h
//  Linked Lists
//

#ifndef Linked_Lists_LinkedList_h
#define Linked_Lists_LinkedList_h

#include "Node.h"

class LinkedList
{
private:
    Node *head;

public:
    LinkedList();
    void addNode(int data);
    void removeNode(int data);
    bool searchNode(int data);
    void printList();

};

#endif

节点.cpp

//
//  Node.cpp
//  Linked Lists
//

#include <iostream>
#include <cstdlib>

#include "LinkedList.h"
#include "Node.h"

using namespace std;

Node::Node(int data) {};

LinkedList.cpp

//
//  LinkedList.cpp
//  Linked Lists
//

#include <iostream>
#include <cstdlib>

#include "LinkedList.h"
#include "Node.h"

using namespace std;

LinkedList::LinkedList()
{
    head = NULL;
}

void LinkedList::addNode(int data)
{
    Node *newNode;
    newNode->data = data;
    newNode->next = NULL;

    Node *tmp = head;

    if(tmp != NULL)
    {
        while(tmp->next != NULL)
        {
            tmp = tmp->next;
        }

        tmp->next = newNode;
    }
}

void LinkedList::removeNode(int data)
{
    Node *tmp = head;

    if(tmp == NULL)
    {
        cout << "No node removed" << endl;
        return;
    }

    if(tmp->next == NULL)
    {
        delete tmp;
        head = NULL;
    }
    else
    {
        Node *previous;

        do
        {
            if(tmp->data == data)
            {
                break;
            }
            previous = tmp;
            tmp = tmp->next;
        }
        while(tmp != NULL);

        previous->next = tmp->next;

        delete tmp;
    }
}

bool LinkedList::searchNode(int data)
{
    Node *tmp = head;

    while(tmp != NULL)
    {
        if(tmp->data == data)
        {
            cout << "Node found" << endl;
            return true;
        }
        tmp = tmp->next;
    }
    cout << "Node not found" << endl;
    return false;
}

void LinkedList::printList()
{
    Node *tmp = head;

    if(tmp == NULL)
    {
        cout << "List()" << endl;
        return;
    }

    if(tmp->next == NULL)
    {
        cout << "List(" << tmp->data << ")";
    }
    else
    {
        do 
        {
            cout << "List(" << tmp->data;
            cout << ", ";
            tmp = tmp->next;
        } 
        while (tmp != NULL);

        cout << ")" << endl;
    }
}

main.cpp

//
//  main.cpp
//  Linked Lists
//

#include <iostream>
#include <cstdlib>

#include "LinkedList.h"
#include "Node.h"
#include "LinkedList.cpp"

using namespace std;

int main ()
{
    LinkedList list;

    int data;
    int choice;

    while(1) 
    {
        cout << " Select:" << endl;
        cout << "1 to add a node" <<endl;
        cout << "2 to remove a node" << endl;
        cout << "3 to search for a node" << endl;
        cout << "4 to exit" << endl;
        cout << endl;

        cin >> choice;

        switch(choice)
        {
            case 1: //insertion
                cout << "Enter node: ";
                cin >> data;
                list.addNode(data); //add a node
                break;
            case 2: //deletion
                cout << "Enter node: ";
                cin >> data;
                list.removeNode(data); //remove a node
                break;
            case 3: //search
                cout << "Enter node: ";
                cin >> data;
                list.searchNode(data); //search for a node
                break;
            case 4:
                exit(0); //exit the program
                break;
            default: //default case
                cout << "Please enter a valid choice (1 - 4)!" << endl;
                break;
        }
    }
    return 0;
}

如果您能帮我解决我的问题,我将不胜感激。

【问题讨论】:

    标签: class singly-linked-list


    【解决方案1】:

    你的问题在main.cpp:

    #include "LinkedList.cpp"
    

    去掉这一行,你只需要包含标题。

    包含cpp文件会导致再次编译,导致符号被定义两次,从而导致你的错误。

    奖金:

    LinkedList.cpp你还有一个错误

    Node *newNode;
    newNode->data = data;
    newNode->next = NULL;
    

    你需要在使用之前初始化newNode,比如:

    Node *newNode = new Node(data);
    newNode->data = data;
    newNode->next = NULL;
    

    【讨论】:

    • 谢谢。但是现在它实际上会运行,当我调用 insert 时它会抛出(gdb)。它显示LinkedList.cpp这一行的错误:newNode->data = data;
    • @Blake 这是一个不同的问题,需要代码和错误消息。
    • 我把它改成了那个,它说:“节点初始化没有匹配的构造函数”
    • 谢谢。修复了添加节点,但现在我的 printList() 搞砸了。
    • @Blake 好吧...做一些调试。当您遇到特定问题时,请发布另一个问题。遇到相同问题的其他人会更容易找到答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2013-05-25
    • 1970-01-01
    相关资源
    最近更新 更多