0

I'm looking for a help to understand linked list. I have such a task: aclass 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:
    • 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).
  3. A push_back(DNA* newDNA) method that adds a node to the end of the list
  4. A find(int id) method that returns a DNA* if a DNA object with id exists in the list; otherwise it returns NULL
  5. An obliterate(int id) method that deletes the DNA entry with an accession number id and removes the corresponding node
  6. An int size() method that returns the number of elements in the list

First of all, I try to do push_back(DNA* newDNA) method. Any help, please?

Thank you.

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;
    }
8
  • So many errors, its hard to know where to start, plus you haven't shown the code in DNA.h in your question, so it's even harder to help. Commented Sep 7, 2015 at 5:19
  • 1
    First point, why the confusion between DNA and DNANode? Use one or the other, not both (the assignment says DNANode) Commented Sep 7, 2015 at 5:20
  • Second point, why getHeadPtr and setHeadPtr? The assignment doesn't ask for them, they are wrong. Think about the person using your object. The waht to add items to the end of the list, they want to find items using an id, they want to obliterate items. Do they want to set the head pointer, or get the head pointer? It makes no sense to the users of your class, so it shouldn't be there. Commented Sep 7, 2015 at 5:22
  • Third point, what is DNANode object doing? That just looks like a compiler error. Commented Sep 7, 2015 at 5:23
  • Fourth point, none of you method implementations push_back, find, obliterate and size are defined in the DNAList class. It should be int DNAList::size() { return 0; } etc Commented Sep 7, 2015 at 5:24

1 Answer 1

1

The simplest way of having a linked list of DNA*, would be to use the standard <list> container and use list<DNA*>. And to avoid memory leaks, even 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():

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.