M W
M W

Reputation: 67

for loop for simple linked list c++

I'm trying to do a loop for this part:

    insert_front(&list,name[0]);
    insert_front(&list,name[1]);                 
    insert_front(&list,name[2]);

but, i can't figure out which should be used as a limiter, which should stop the loop for going and how it will change the name[] perimeters.

Here's the whole code:

#include "list.h"
#include "string"
using namespace std;
int main(){
    cout<<"What is your name \n";
    string name;
    getline(cin,name);

    Node *list;

    list = new_list();                          
    insert_front(&list,name[0]);
    insert_front(&list,name[1]);                  
    insert_front(&list,name[2]);
    }                          
    print_list(list);                                      
    delete_list(&list);
        print_list(list);
        return 0;

}

Upvotes: 0

Views: 3840

Answers (4)

Jonathan Seng
Jonathan Seng

Reputation: 1219

The Standard Template Library, which strings are part of, provide some nice efficient and safe iterators for dealing with items you might want to loop over.

for(string::iterator iter = name.begin(); iter < name.end(); iter++) {
    insert_front(&list, *iter);
}

string::begin shows this same example.

string::begin() returns an object that when dereferenced with operator* gives the value where its at. operator++() increments to the next position. string::end() gives another object after the end of the string so that the operator==() after the operator++() stops the loop.

Now, the important part here is that you aren't using indices. Many STL containers let you copy in values using the iterators. You could even skip the for loop to use an stl list and do something like the following:

std::list list (name.begin(), name.end());

Done. No manual for loop or for_each() call at all. You can even use name.begin() + 1 to skip the first character. See the list constructor for this.

As pointed out in a comment, to achieve the insert_front() reversing effect, this should be:

std::list list (name.rbegin(), name.rend());

string::rebgin() and string::rend() iterate in reverse order. To be simpler, this is coming after the use of string::begin() and string::end() to introduce the reversal separately.

Obviously, you are using a different list and will need some looping to continue using it. But, you might just decide to leave the string as a string and pass the relevant iterators to a print function and avoid using a list at all. This might allow you to make code generic enough to not care whether this is from a string or list.

Upvotes: 1

paddy
paddy

Reputation: 63471

That's not really "the whole code" as you say. What elements does your list store? Looks to be a single character per node. So it appears you are you trying to make a list of characters to represent the person's name in reverse order.

That being the case:

for( size_t i = 0; i < name.length(); i++ ) {
    insert_front(&list,name[i]);
}

Upvotes: 1

Edward Strange
Edward Strange

Reputation: 40859

Iterator version (C++03):

for (std::string::iterator iter = name.begin(); iter != name.end(); ++iter)
    insert_front(&list, *iter);

Foreach version (C++11):

for ( char c : name)
    insert_front(&list, c);

Index version (C++03):

for (size_t i = 0; i < name.size(); ++i)
    insert_front(&list, name[i]);

Foreach version (C++03 with Boost):

std::for_each(name.begin(), name.end(), boost::bind(insert_front, &list, _1));

I'm not so sure on the C++11 foreach version.

There's probably also a reasonable way to use std::copy or std::transform if your hand-coded list has iterators.

Upvotes: 6

Aesthete
Aesthete

Reputation: 18848

I think you are trying to do:

for(int i=0; i<name.size(); i++)
{
      insert_front(&list,name[i]);
}

And this will store each character of the name in the list.

Upvotes: 0

Related Questions