卡在链表多类实现上

Stuck on a linked list multiple class implementation

本文关键字:实现 链表      更新时间:2023-10-16

我已经为这个项目工作了好几天了。这个项目包含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函数来打印节点中的数据时,我的问题就出现了。

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

findId内部,执行以下行:

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

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

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

一个更好的解决方案可能是从head开始作为nullptr,而不是在顶部有一个伪DNANode。这将涉及更改push_back中的一些代码,但可能会使其更易于理解。

啊哈。我认为造成问题的原因是,在SequenceDatabase::importEntries()方法接近尾声时,您正在设置if(p=nullptr),而不是进行比较if(p == nullptr)。毫无疑问,这是造成您所看到的错误的原因。这是一个常见的错误。