struct Node
{
char acid;
Node *next;
}
DNA(string dna); // initalizes the strand to the string
/// can work if you may use a loop and insert it's char values in new nodes added to strand.since your
/// nodes take a single char each.
void print (int begin, int end) const; // I have no idea if it will work because a linked list isn't indexed
/*You might check this one out*/
void print (const Node *&mystrand) ///passes by reference
{
Node *temp=mystrand; ///copy mystrand into a temporary pointer.
/// this will apply only if you strand ends with a nullptr value on the last pointer.
while (temp!=nullptr)
{
cout <<temp-> acid;
temp=temp-> next; /// moves temp to the next node;
}
}
// do something similar here
Dna substr (int begin, int end) const;
It wasn't given but in the print function it shows
1 2 3 4 5
void print(int begin, int end) const;
// prints the strand from begin to end
//the first position is 0
//output gattaca.print(1,3) is att)
So would that make them indexes?
Where (1,3) is inputted with gattaca.print(1,3) it would print out att. So I was guessing it was (begin, end) perhaps. But wouldn't we have to format the string into a char array then initialize the char array into print with begin and end?
Hi note that above is just a constructor , you'll have to make a function that add a single node for
efficiency , bearing in mind that the user might not have the intial dna as a strand and will add base after base
Okay if your print function must take int begin, int end then you can use pointer arithmetics though I don't know if they will work fine for linked lists
1 2 3 4 5 6 7 8 9 10 11
void print (int begin, int end) /// am not sure on these coz am not sure how the nodes are in memory
{ /// if they are contiguous and if pointer arithmetic apply to 'em
Node *temp=strand+begin; /// just try
Node *myend=strand+end;
while (temp!=myend)
{
cout <<temp-> acid;
temp=temp-> next;/// move to next node
}
}
void add2last (char b)
{
///if strand is empty set b to to its acid
if (length==0)
{
strand-> base=b; ++length;
strand-> next=nullptr;
}
else
{
Node *temp=strand-> next;
while (temp!=nullptr) /// loop exit when next pointer is nullptr i.e. end of strand
temp=temp-> next;
Node *end=new Node;
end-> acid=b;
end-> next=nullptr;
temp=end; ++length;
}
}