使用类的单链表的问题

时间:2011-12-07 22:33:15

标签: class singly-linked-list

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

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

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

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

Node.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

Node.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;
}

如果你能帮我解决我的问题,我会非常感激。

1 个答案:

答案 0 :(得分:2)

您的问题出在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;