Program Help. Constructors, String, Linked List

closed account (ShvpDjzh)
Help with voidPrint()
Last edited on
closed account (SECMoG1T)
Hi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
   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; 

             
Last edited on
closed account (SECMoG1T)
Oh sorry I dint see this legal input as (n,m) where (0<=n< m). n am puzzled right now.
closed account (ShvpDjzh)
Thank You Andy but the void print function must stay like that for the program, instructed by the instructor.


How would you insert char values in new nodes?
closed account (ShvpDjzh)
@andy1992 its very fustrating
closed account (SECMoG1T)
Okay tell me what are int begin, int end i'll help , are they indexes are they values? i need a clue on that
closed account (ShvpDjzh)
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?
closed account (ShvpDjzh)
@andy1992, first wouldn't we need to do the this function first:

1
2
3
4
DNA(string dna) 
{

}


Where it would initialize the string to char array and store them in the Nodes. I'm confused on doing that.
closed account (SECMoG1T)

okay its pretty simple as you have your struct as node hence all it's members are public

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/// example
  struct Node
      { 
          char acid;
           Node *next; 
       } 
     

 DNA::DNA(string dna)
 {
    if (dna.size ()==0){cout<<"empty string" ;}/// exit
  
   strand-> acid=dna [0];
   strand-> next= nullptr; 
   ++length; 
   /// if string size is greater than one continue adding

   int index=1;
   Node *back=strand-> next;

   while(index <dna.size ())
       {
          back=new Node; 
          back-> acid=dna [index]; ++length; 
          back-> next= nullptr; 
          back=back-> next;//// moves back to the last pointer
          ++index; 
        }
 }
Last edited on
closed account (ShvpDjzh)
Ok that makes sense. Now the next step would be initializing the dna[index] with begin and end correct? and that would be within the print function.

Now a question for the example you provided should we include a cin to enter a string or no?



closed account (SECMoG1T)
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
          }
   }
Last edited on
closed account (SECMoG1T)
Am more casual method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void print(int begin,  int end)
 {
     Node* start;
     Node* myend; 
     Node* temp=strand; 

    Int i=1;
    while (i <begin)
            {
               temp=temp-> next;++i; 
            }
      
          start=temp;  temp=strand; i=1;
   
      while (i <end)
            {
                temp=temp-> next; ++i; 
             }

          myend=temp;
       while (start!=myend)
            {
                cout <<start-> acid; 
                start=start-> next;
             }
    
  }
Last edited on
closed account (ShvpDjzh)
@andy1992, ok I am going to try to work with what you gave for the print function. Are we able to chat on here privately?
closed account (SECMoG1T)
try them hope they work btw this is a public forum .
Last edited on
closed account (ShvpDjzh)
No it does not work, so for the

1
2
3
4
5
6

DNA(string dna)
{
 for (int i = 0; i < dna.length(); i++)
     add2Last(dna[i]);
}


But now we would have to create an add2Last()

That confuses me. @andy1992
closed account (SECMoG1T)
add2last () // is a function you seem to have defined

You add a single base to the strand
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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; 
     }
}
 


What is the error on print function
Last edited on
Topic archived. No new replies allowed.