【问题标题】:Stuck on a linked list multiple class implementation卡在链表多类实现上
【发布时间】:2015-02-09 23:57:08
【问题描述】:

我已经为这个项目工作了几天。该项目包含 3 个类。第一个是存储 DNA 对象的 DNA 类。第二个是读取文件并解析命令和数据并相应地处理它的数据库类。最后一个是 DNA 列表类,它是具有指向 DNA 对象的指针的节点的链表。

我已经完成了我的链表构建方法。它必须是在列表末尾添加节点的 push_back 方法。当我尝试在列表中搜索某个节点时出现了我的问题。如果列表中存在具有 id 的 DNA 对象,这必须是返回 DNA* 的方法;否则返回 NULL。

我的计划是使用这种方法打印并删除节点。我似乎无法使这种方法起作用。显然,我对指针有点不稳定。我花了几个小时来实现我的 push_back 方法。这是我的代码。任何指导或帮助表示赞赏。

DNA.h

#ifndef DNA_H
#define DNA_H

#include <string>

class DNA{  
public:
   // overloaded constructor for DNA class
   DNA(std::string, int, std::string, int, int);
   // print function
   void print();
   int getID();

private:
   std::string m_label;      // variable to hold label
   int m_id;                    // variable to hold id
   std::string m_sequence;  // variable to hold sequence
   int m_length;                // variable to hold length
   int m_index;             // variable to hold index
};
#endif

DNA 实施

#include "DNA.h"
#include <iostream>
#include <string>

using namespace std;

DNA::DNA(string label, int id, string sequence, int length, int index){
    m_label = label;
    m_id = id;
    m_sequence = sequence;
    m_length = length;
    m_index = index;
}

void DNA::print(){
     cout << "DNA:" << '\t' << "label: " << m_label << '\t' << "ID: " << m_id << '\t' << "Sequence: " << m_sequence << endl << "Length: " << m_length << '\t' << "cDNAStartIndex: " << m_index << endl << endl;
}

int DNA::getID(){
    return m_id;
}

数据库类

#ifndef SEQUENCEDATABASE_H
#define SEQUENCEDATABASE_H

#include <string>
#include <fstream>
#include "DNA.h"
#include "DNAList.h"

class SequenceDatabase {
public:
    SequenceDatabase();
    // function to import the data file, parse the data, and perform the required output
    void importEntries(std::string);
private:
    DNAList list;

};
#endif 

数据库实现

#include "SequenceDatabase.h"
#include "DNA.h"
#include "DNAList.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

SequenceDatabase::SequenceDatabase(){

    DNAList list;
}
// function reads in the filename creates a data stream and performs the requested actions
void SequenceDatabase::importEntries(string inputFile){
    ifstream dnaFile(inputFile);
    char command;
    string label, sequence;
    int id, length, index;
    while(dnaFile >> command){
        DNA* p;
        if(command == 'D'){
            dnaFile >> label >> id >> sequence >> length >> index;
            DNA data(label, id, sequence, length, index);
            p = new DNA(label, id, sequence, length, index);
            list.push_back(p);
        }
        if(command == 'P'){
        dnaFile >> id;
        cout << "Printing " << id << " ..." << endl << endl;
        p = list.findId(id);
        if(p == nullptr)
            cout << "Can not find item " << "(" << id << ")!" << endl << endl;
        else
        p-> print();
        }
    }
    dnaFile.close();
}

最后是我的列表类

#ifndef DNALIST_H
#define DNALIST_H

#include "DNA.h"
#include "sequenceDatabase.h"

struct DNANode{
    DNA* data;
    DNANode* next;
    DNANode* prev;
};


class DNAList{
public:
    DNAList();
    DNAList(DNA* newDNA);
    void push_back(DNA* newDNA);
    DNA* findId(int);
    void obliterate(int id);
    int size();

private:
    DNANode* head;
    int list_size;

};
#endif

列表实现

#include "DNA.h"
#include "sequenceDatabase.h"
#include "DNAList.h"
#include <iostream>

using namespace std;

DNAList::DNAList(){
    head = new DNANode;
    head->next = nullptr;
    list_size = 0;

}

DNA* DNAList::findId(int id){    // this function is my problem
    DNANode* current;
    current = head;
    while(current->next != nullptr){
        if(current->data->getID() == id){
            return current->data;
        }
        current = current->next;
    }
    return nullptr;
}

int DNAList::size(){
    return list_size;

}

void DNAList::push_back(DNA* newDNA){
    DNANode* current;
    DNANode* last;
    DNANode* p;
    p = new DNANode;
    p->data = newDNA;
    last = nullptr;
    current = head;
    cout << "Adding " << newDNA->getID() << " ..." << endl << endl;
    while(current != nullptr){
        last = current;
        current = current->next;
    }
    if(current == head->next){
        p->next = nullptr;
        p->prev = head;
        head->next = p;
    }
    else{
        p->next = current;
        p->prev = last;
        last->next = p;
    }
    list_size++;
}

我不确定是否应该发布整个代码,但我觉得需要它来理解问题。当我尝试调用 find 函数来打印节点中的数据时,我的问题就出现了。

【问题讨论】:

  • 通过仔细阅读代码,看起来您的 findId 方法确实会返回与 id 对应的 DNA 对象。是你不清楚的删除部分吗?您可能想阅读 Aho 等人的“数据结构和算法”。这包含了您需要了解的有关链表的所有信息。
  • 您的程序面临的具体问题是什么?
  • 您可能想使用std::list 而不是自己创建。
  • 这是一个真实的项目还是一个有人为限制的学校项目,例如没有std::liststd::find
  • @NeilKirk 你的意思是std::list::find

标签: c++ class linked-list


【解决方案1】:

DNAListhead 成员变量被初始化为 new DNANode。由于DNANode 没有显式构造函数,其编译器生成的构造函数不会初始化指针datanextprevnext 在下一行初始化,但 data 被保留为垃圾值。

findId内部,执行了这一行:

if (current->data->getID() == id){

然而,第一次循环时,current 指向head。这意味着您正在尝试查看可能会崩溃的垃圾值。

一种解决方案是将findId函数更改为从head-&gt;next开始,另一种是将head中的data指针初始化为nullptr,并在你之前检查data不是nullptr访问它。

更好的解决方案可能是将head 作为nullptr 开始,而不是在顶部设置一个虚拟的DNANode。这将涉及更改push_back 中的一些代码,但可能会更容易理解。

【讨论】:

  • 你先生真了不起。这解决了问题。我想我需要用 obliterate 函数做同样的事情?
  • 如果您将 head 作为占位符,那么任何遍历列表的函数都必须知道应该跳过 head 元素。如果您将 head 更改为 nullptr,则函数将需要检查 nullptr(无论如何它们都会在循环中执行此操作)。
【解决方案2】:

啊哈。我认为导致问题的原因是,在您的 SequenceDatabase::importEntries() 方法结束时,您正在设置 if(p=nullptr) 而不是进行比较 if(p == nullptr)。这无疑会导致您看到的错误。这是一个常见的错误。

【讨论】:

  • 所以我注意到的另一件事是你的 print 方法是 DNA 类的成员,但 findID 返回的是 DNAnode,而不是 DNA 我想你想要 p成为DNAnode,然后使用p-&gt;data-&gt;print 而不是p-&gt;print
  • 另外,您可能根本不应该使用 p。只需通过调用您的 findId 方法传递所需的对象,即 if(list.findId(id) == nullptr)list.findId(id)-&gt;data-&gt;print()
猜你喜欢
  • 2019-06-23
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
相关资源
最近更新 更多