0

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

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

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

我认为我的插入可能是问题,因为我的 searchNode() 也不能正常工作,它总是说找不到节点。

这是我的代码:(我无法更改 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

链表.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) {};

链表.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;
    }
    cout << "Node added" << endl;
    printList();
}

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;
    }
    cout << "Node removed" << endl;
    printList();
}

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

    while(tmp != NULL)
    {
        if(tmp->data == data)
        {
            cout << "Node found" << endl;
            printList();
            return true;
        }
        tmp = tmp->next;
    }
    cout << "Node not found" << endl;
    printList();
    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
//  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;
}

如果你能帮我找出我的问题,我将不胜感激。

4

1 回答 1

1

您没有添加任何节点。如果headNULL,您的添加节点变为:

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

    Node *tmp = head;

    if(tmp != NULL)
    { 
        //this never gets executed
    }
    cout << "Node added" << endl;
    printList();
}

您需要处理这种情况(第一次插入):

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;
    }
    else
    {
        head = newNode;
    }
    cout << "Node added" << endl;
    printList();
}

如果头部不存在,这将创建头部。

你在使用调试器吗?如果你这样做了,(对你来说)会容易得多。

于 2011-12-07T23:22:34.230 回答