0

I have a problem which i need to check if a given list is daisy chained. Daisy chained list is list with strings in which the last letter of first element equals first letter of the next element. And the output is if there is that kind of elements in the list print them to console. The list size cant be less than 4 and more than 25 words. Maybe my problem is that i cant iterate through the list. I know the logic for the task but im not familiar with the list and it dont have operator[] so i dont know how to realize it. Here is my code:

#include <iostream>
#include <list>
#include <string>
using namespace std;

int main()
{
    int n;
    cin >> n;

    if (n < 4 || n > 25)
    {
        cout << "N cant be less than 4 and more than 25" << endl;
        return 1;
    }

    list<string> li;
    string line;

    for (int i = 0; i < n; i++)
    {
         cin >> line;
         li.push_back(line);
    }

    /*for (list<string>::iterator it = li.begin(); it != li.end(); it++)
    {
        cout << *it << " ";
    }*/

    for (list<string>::iterator it = li.begin(); it != li.end(); it++)
    {
        if ()
    }

    return 0;
 }
user7460099
  • 53
  • 1
  • 2
  • 9
  • `*it` gives you the `string` and `it->front()` gives you the first character and so on. Someone should have told you how to do that, if not [read up](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1). – nwp Feb 19 '17 at 10:19
  • Also, you can increment the iterator *inside* the loop to get the next element in the list (you need an extra check for `end` though). – Some programmer dude Feb 19 '17 at 10:27
  • Its not working there is no member funtions back and front – user7460099 Feb 19 '17 at 10:28
  • This is what i typed `if (it->back() == (it++)->front())` – user7460099 Feb 19 '17 at 10:28
  • 1
    Oops, better read on post-increment operator, you're dereferencing the same string. – stefaanv Feb 19 '17 at 10:32
  • [`front`](http://en.cppreference.com/w/cpp/string/basic_string/front) and [`back`](http://en.cppreference.com/w/cpp/string/basic_string/back) were introduced in C++11. Which compiler are you using? Maybe you need to explicitly enable C++11? And if it's not possible, then there are other ways to access the first and last characters from a `std::string` object. – Some programmer dude Feb 19 '17 at 10:43
  • This is not working too `if ((*it)->back() == ((*it)++)->front())` – user7460099 Feb 19 '17 at 10:43
  • Try doing e.g. `std::string first = *it++; std::string second = *it;` And use the two string objects. – Some programmer dude Feb 19 '17 at 10:47

1 Answers1

1
  • *it will give you the string. In order to access a char at a specific position inside a string do : char tmp = (*it)[index];

  • Your logic will be something like:

    1. ++it to move iterator to next string in the list
    2. store the first character of this string in some temp variable
    3. --it to move the iterator back to original iterator inside the loop.
    4. access last element of this iterator now and compare this with the one previously stored in step 2.
  • inside your loop, you need an additional check after step 1. above that is: if(it == li.end()) break; in order to avoid trying to access invalid memory.

Clifford
  • 88,407
  • 13
  • 85
  • 165
ibrar arshad
  • 223
  • 2
  • 7