简体   繁体   English

将对象的指针添加到链接列表C ++

[英]Add pointer of object to linked list c++

I'm looking for a help to understand linked list. 我在寻找帮助来了解链表。 I have such a task: aclass DNAList: 我有这样一个任务:DNAList类:

  1. This class is a linked list of nodes that have pointers to (not copies of) DNA objects and at a minimum should contain: 此类是节点的链接列表,这些节点具有指向DNA对象(而不是其副本)的指针,并且至少应包含:
    • Appropriate constructor(s) and destructor 适当的构造函数和析构函数
    • Data members to store: Head pointer to list 要存储的数据成员:指向列表的头部指针
  2. A DNANode struct or class that holds a pointer to a DNA object and a "next" pointer to a DNANode object (and a "prev" pointer if you're using a doubly-linked list). 一个DNANode结构或类,其中包含指向DNA对象的指针和指向DNANode对象的“下一个”指针(如果使用双向链接列表,则为“上一个”指针)。
  3. A push_back(DNA* newDNA) method that adds a node to the end of the list 一个push_back(DNA * newDNA)方法,将一个节点添加到列表的末尾
  4. A find(int id) method that returns a DNA* if a DNA object with id exists in the list; 如果列表中存在具有id的DNA对象,则该find(int id)方法将返回DNA *; otherwise it returns NULL 否则返回NULL
  5. An obliterate(int id) method that deletes the DNA entry with an accession number id and removes the corresponding node 一种obliterate(int id)方法,该方法删除具有登录号id的DNA条目并删除相应的节点
  6. An int size() method that returns the number of elements in the list 一个int size()方法,该方法返回列表中的元素数

First of all, I try to do push_back(DNA* newDNA) method. 首先,我尝试做push_back(DNA * newDNA)方法。 Any help, please? 有什么帮助吗?

Thank you. 谢谢。

DNAList.h DNA列表

#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 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;
    }

The simplest way of having a linked list of DNA* , would be to use the standard <list> container and use list<DNA*> . 具有DNA*链接列表的最简单方法是使用标准<list>容器并使用list<DNA*> And to avoid memory leaks, even list<shared_ptr<DNA>> . 为了避免内存泄漏,甚至可以list<shared_ptr<DNA>>

But your task seems to be an exercise to learn about linked lists. 但是您的任务似乎是学习链接列表的一种练习。 So here some tips for your own push_back() : 所以这里是您自己的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
    }
}

You can then inspire yourself from this to write your find() and size() (if you don't maintain the size in a data element), and even obliterate() . 然后,您可以从中激发自己编写find()size() (如果您不保持数据元素的大小),甚至是obliterate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM