使用类打印单链表

时间:2011-12-07 23:01:35

标签: class printing singly-linked-list

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

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

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

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

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

//
//  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 :(得分: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();
}

如果它已经存在,这将创建头部。

您使用的是调试器吗?如果你这样做(对你而言)会更容易。