提问者:小点点

C++链表覆盖以前的数据


所以我在做一个项目,但我不知道我的问题是什么。 我刚开始使用和学习指针,所以这可能是一些显而易见的东西,我只是没有看到,但是当我运行print方法时,它会显示列表中的每一项,并且与最后一项的值相同。 我不明白为什么它不仅增加了列表,而且覆盖了以前的条目。 (我还没有完成其余的函数,所以我没有包含它们,main下的所有内容都是作为模板给出的。目前我只是想克服这个障碍,因为我似乎在网上找不到答案。)

#include <algorithm>
#include <iostream>
#include <time.h>

#include "CSVparser.hpp"

using namespace std;

double strToDouble(string str, char ch);

struct Bid {
    string bidId; // unique identifier
    string title;
    string fund;
    double amount;
    Bid() {
        amount = 0.0;
    }
    Bid* nextNode = 0;
};

class LinkedList {

private:
    Bid* head;
    Bid* tail;
    int size;

public:
    LinkedList();
    virtual ~LinkedList();
    void Append(Bid* bid);
    void PrintList();
};

LinkedList::LinkedList() {
    this->head = 0;
    this->tail = 0;
    this->size = 0;
    return;
}

LinkedList::~LinkedList() {
}

void LinkedList::Append(Bid* bid) {
    if (this->head == 0)
    {
        this->head = bid;
        this->tail = bid;
    }
    else
    {
        this->tail->nextNode = bid;
        this->tail = bid;
    }
    size += 1;
    return;
}

void LinkedList::PrintList() {
    Bid* currBid = this->head;
    
    for (int i = 0; i < size; i++)
    {
        cout << currBid->bidId << ": " << currBid->title << " | " << currBid->amount << " | " << currBid->fund << endl;
        currBid = currBid->nextNode;
    }
    return;
}

int main(int argc, char* argv[]) {

    string csvPath, bidKey;
    switch (argc) {
    case 2:
        csvPath = argv[1];
        bidKey = "98109";
        break;
    case 3:
        csvPath = argv[1];
        bidKey = argv[2];
        break;
    default:
        csvPath = "eBid_Monthly_Sales_Dec_2016.csv";
        bidKey = "98109";
    }

    clock_t ticks;

    LinkedList bidList;

    Bid bid;

    int choice = 0;
    while (choice != 9) {
        cout << "Menu:" << endl;
        cout << "  1. Enter a Bid" << endl;
        cout << "  2. Load Bids" << endl;
        cout << "  3. Display All Bids" << endl;
        cout << "  4. Find Bid" << endl;
        cout << "  5. Remove Bid" << endl;
        cout << "  9. Exit" << endl;
        cout << "Enter choice: ";
        cin >> choice;

        switch (choice) {
        case 1:
            bid = getBid();
            bidList.Append(&bid);
            displayBid(bid);

            break;

        case 2:
            ticks = clock();

            loadBids(csvPath, &bidList);

            cout << bidList.Size() << " bids read" << endl;

            ticks = clock() - ticks; // current clock ticks minus starting clock ticks
            cout << "time: " << ticks << " milliseconds" << endl;
            cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;

            break;

        case 3:
            bidList.PrintList();

            break;

        case 4:
            ticks = clock();

            bid = bidList.Search(bidKey);

            ticks = clock() - ticks; // current clock ticks minus starting clock ticks

            if (!bid.bidId.empty()) {
                displayBid(bid);
            } else {
                cout << "Bid Id " << bidKey << " not found." << endl;
            }

            cout << "time: " << ticks << " clock ticks" << endl;
            cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;

            break;

        case 5:
            bidList.Remove(bidKey);

            break;
        }
    }

    cout << "Good bye." << endl;

    return 0;
}

样本输出


共1个答案

匿名用户

main函数中只有一个名为bidbid对象。 每次将&bid传递给append函数时,传递的都是同一个对象的地址。 所以你的链表包含了一堆指向同一个内存的指针。

解决此问题的一种方法是在append中分配新内存,如下所示:

void LinkedList::Append(Bid* bid_arg) {
  Bid *bid = new Bid{*bid_arg};   // allocate memory with the value 
                                  // pointed at by the pointer that is passed in
  // append bid to the linked list
}

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|链表|覆盖|数据)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?