【问题标题】:Add pointer of object to linked list c++将对象的指针添加到链表c ++
【发布时间】:2015-12-02 14:04:18
【问题描述】:

我正在寻求帮助来理解链表。我有这样一个任务: 类 DNAList:

  1. 此类是具有指向(不是副本)DNA 对象的指针的节点的链接列表,并且至少应包含:
    • 适当的构造函数和析构函数
    • 要存储的数据成员: 指向列表的头指针
  2. 一个 DNANode 结构或类,它包含一个指向 DNA 对象的指针和一个指向 DNANode 对象的“next”指针(如果您使用的是双向链表,还有一个“prev”指针)。
  3. 一种将节点添加到列表末尾的 push_back(DNA* newDNA) 方法
  4. 如果列表中存在具有 id 的 DNA 对象,则返回 DNA* 的 find(int id) 方法;否则返回NULL
  5. 一种 obliterate(int id) 方法,用于删除具有登录号 id 的 DNA 条目并移除相应的节点
  6. 返回列表中元素数量的 int size() 方法

首先,我尝试做 push_back(DNA* newDNA) 方法。 请帮忙?

谢谢。

DNAList.h

#ifndef DNALIST_H
#define DNALIST_H
#include <iostream>
#include <string>
#include "DNA.h"


class DNAList{
    //data members
    private:
        DNA* headPtr;
    public:
        DNAList();
        ~DNAList();
        struct DNANode;
        void push_back(DNA* newDNA);
        DNA* find(int id);
        void obliterate(int id);
        int size();
        DNA* getHeadPtr(){
            return headPtr;
        }

       void setHeadPtr(DNA* head){
            headPtr= head;
       }

};

#endif

DNAList.cpp

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

//constrictor
    DNAList::DNAList(){}
//destructor
    DNAList::~DNAList(){
        delete headPtr;
        headPtr = NULL;
    }
//struct that holds pointer to a DNA object  and a "next" pointer to a
DNANode object
    struct DNANode{
        DNA* dnaPtr;
        DNANode* next;
    };
//
    void push_back(DNA* newDNA){

        //dnaPtr = new DNANode;

    }

    DNA* find(int id){

    }
    void obliterate(int id){

    }
    int size(){
        return 0;
    }

【问题讨论】:

  • 这么多错误,很难知道从哪里开始,而且您没有在问题中显示 DNA.h 中的代码,因此更难提供帮助。
  • 第一点,为什么DNADNANode会混淆?使用其中一个,而不是两者都使用(作业说 DNANode)
  • 第二点,为什么getHeadPtr和setHeadPtr?任务没有要求他们,他们错了。想想使用你的对象的人。将项目添加到列表末尾的方式,他们想使用 id 查找项目,他们想删除项目。他们是想设置头指针,还是获取头指针?它对你班级的用户没有意义,所以它不应该在那里。
  • 第三点,DNANode object在做什么?这看起来像是一个编译器错误。
  • 第四点,你的方法实现push_back、find、obliterate和size都没有在DNAList类中定义。应该是int DNAList::size() { return 0; }

标签: c++ pointers linked-list


【解决方案1】:

拥有DNA* 链表的最简单方法是使用标准&lt;list&gt; 容器并使用list&lt;DNA*&gt;。并且为了避免内存泄漏,即使是list&lt;shared_ptr&lt;DNA&gt;&gt;

但您的任务似乎是学习链表的练习。所以这里有一些提示给你自己的push_back()

void push_back(DNA* newDNA)
{
    DNANode *element = new DNANode;// create a new node
    element->dnaPtr = newDNA;      // set it up  
    element->next = nullptr;       // it has no next element now

    if (headPtr==nullptr)          // hoping you have initalized the head at construction
         headPtr = element;        // either it's first element of empty list
    else {                         // or you need to find the last node
        DNANode *last = headPtr;   // starting at head
        while (last->next)         // and going from node to node
            last = last->next;  
        last->next = element;      // here you are
    }
}

然后,您可以从中启发自己编写您的 find()size()(如果您不保持数据元素中的大小),甚至是 obliterate()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2019-05-05
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多