【发布时间】: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::list和std::find? -
@NeilKirk 你的意思是
std::list::find?
标签: c++ class linked-list