Linked List Substrings

Hi having trouble with this homework assignment and hopefully a fresh pair of eyes can see where I went wrong in this assignment. So the assignment is that given a string to store it in a linked list and give all possible substrings that start with A and end with B.
For Example:
Enter a string: CABAAXBYA
Substring 1: AB
Substring 2: ABAAXB
Substring 3: AAXB
Substring 4: AXB
Total 4 substrings



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
33
34
35
36
37
38
39
40
41
42
43
44
45
void LinkedList::substring()
{
    counter = 0;
    Node * temp;
    Node * aPtr = first;
    Node * bPtr = first;

    while (aPtr->next != NULL || bPtr->next != NULL)
    {
        while(aPtr->data != 'A')
        {
            aPtr = aPtr->next;
        }

        while(bPtr->data != 'B')
            bPtr = bPtr->next;

        if(aPtr->data == 'A' && bPtr->data == 'B')
        {
            temp = aPtr;
            while(aPtr->next != bPtr->next)
            {
                cout << aPtr->data;
                aPtr = aPtr->next;
            }
            cout << bPtr->data << endl;
        }

        bPtr = bPtr->next;

        while(bPtr->data != 'B')
        {
            bPtr = bPtr->next;
        }

        while(temp->next != bPtr->next)
        {
            cout << temp->data;
            temp = temp->next;
        }
        cout << bPtr->data << endl;

    }
    cout << "Total " << counter << " substrings";
}
I don't quite understand. Does each node hold just one character of the string?

If so, I'd expect something like:
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
int LinkedList::substring() const
{
    int counter = 0;
    Node* p = first;
    Node* begin = nullptr;
    Node* end = nullptr;

    do
    {
        // find starting A
        while (p && p->data != 'A')
            p = p->next;
        begin = p;

        // find ending B
        while (p && p->data != 'B')
            p = p->next;
        end = p;

        if (begin && end)
            ++counter;
    }
    while (begin && end);

    return counter;
}
Last edited on
@kbw
That code does not work for what I need to do, it is suppose to display each substring you're just counting how many substrings there are and yes each node holds just one character of the string.
Last edited on
Topic archived. No new replies allowed.