链接列表未正确显示

时间:2015-12-13 01:46:34

标签: c++ linked-list

我创建了一个链接列表,每个节点都包含一个CarPart对象。我知道问题是我没有正确地取消引用指针,它只显示指针而不是实际值...问题是我无法弄清楚如何正确显示汽车零件项目到控制台。

根据请求我删除了任何不会影响我尝试的结果的代码。

Main.cpp的

#include "stdafx.h"
#include "List.h"

int main()
{


    /*cout << new Node(new CarPart("Hello", "World", 99.00));*/

    List partsList;

    partsList.push_front(new CarPart("FL2016", "Oil Filter", 18.95));
    partsList.push_front(new CarPart("RS12YC", "Spark Plug", 4.15));
    partsList.push_front(new CarPart("D5941", "Digital Tire Guage", 12.15));
    partsList.push_back(new CarPart("G19216", "Car Wash Solution", 8.15));

    partsList.display();
    cout << "now we are going to remove the first item in the list" << endl;
    system("PAUSE");

    partsList.pop_front();

    partsList.display();

    system("PAUSE");
    cout << "now we are going to remove the LAST item from the list" << endl;

    partsList.pop_back();

    partsList.display();

    system("PAUSE");
    return 0;
}

List.h

#pragma once
#include "node.h"
class List
{

private:
    int listSize;
    Node* n;
    Node* temp;
    Node* head;
    Node* tail;

public:
    List();
    void push_front(CarPart*);
    void push_back(CarPart*);
    void pop_front();
    void pop_back();
    void display();

    ~List();
};

List.cpp

#include "stdafx.h"
#include "List.h"


List::List()
{
}
void List::display()
{
    Node* test = head;
    for (int i = 0; i < listSize; i++) {
        cout << test;
    }
}

Node.h

#pragma once
#include "CarPart.h"
class Node
{
private:
    CarPart* data;
    Node* next;
    Node* previous;

public:
    Node();
    CarPart* getData();
    void setData(CarPart*);
    void setNext(Node*);
    void setPrevious(Node*);
    Node* getPrevious();
    Node* getNext();
    void display();
    ~Node();
};

Node.cpp

#include "stdafx.h"
#include "Node.h"


Node::Node()
{
}

void Node::display()
{
    cout << data;
}

CarPart.h

#pragma once
#include <iostream>
#include <string>
using namespace std;
class CarPart
{

private:
    string partNumber;
    string description;
    double price;
public:
    CarPart();
    CarPart(string, string, double);
    string getPartNumber();
    string getDescription();
    double getPrice();
    void display();
    ~CarPart();
    friend ostream& operator<<(ostream& os, CarPart* dt);
};

CarPart.cpp

#include "stdafx.h"
#include "CarPart.h"


CarPart::CarPart()
{
}

CarPart::CarPart(string n, string d, double p)
{
    partNumber = n;
    description = d;
    price = p;
}

string CarPart::getPartNumber()
{
    return partNumber;
}

string CarPart::getDescription()
{
    return description;
}

double CarPart::getPrice()
{
    return price;
}


ostream& operator<<(ostream& os, CarPart* dt)
{
    os << dt->getPartNumber() << endl << dt->getDescription() << endl << dt->getPrice() << endl;
    return os;
}

1 个答案:

答案 0 :(得分:1)

您的代码存在多个问题,但它们都有相同的根问题,因此我只是解释第一个问题,在确定如何修复它之后,您应该能够解决其余的问题。你自己:

void List::display()
{
    Node* test = head;
    for (int i = 0; i < listSize; i++) {
        cout << test;
    }
}

正如您所观察到的,所有这一切都是打印指针的值。检查标题文件的内容后,您的班级Node似乎有一个名为display()的方法。

您现在显示了display()方法,但鉴于我在List::display()中看到的内容,我认为Node::display()的目的是合理的相似,所以你可能打算这样做,而不是:

void List::display()
{
    Node* test = head;
    for (int i = 0; i < listSize; i++) {
        test->display();
    }
}

但这也不对。所有这一切都将一遍又一遍地调用你的头Node display()方法。如果您的List有五个Node,则您将获得第一个Node display()ed的内容五次。您只需更改此循环即可遍历链接列表。

现在,您的Node::display()方法遇到与上述相同的问题,但现在您应该可以自行修复。