delete repeated value

Write program to delete each node, if has repeated value

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
#include <iostream>
#include <list>

using namespace std;

void add2List(list<int> &L)
{
    int val;
    cout << "Adding values to list :\n";
    cout << ">>";
    while(cin >> val)
    {
        cout << ">>";
        L.push_back(val);
    }
}

void removeFromList(list<int> &L)
{

}

void printList(list<int> L)
{
    while(L.size())
    {
        cout << L.front() << " ";
        L.pop_front();
    }
}

int main()
{
    list<int> L;

    add2List(L);

    removeFromList(L);

    printList(L);

    return 0;
}


I don't know how to remove values from list with this #include <list>

Please any one can complete the rest of the code of the removeFromList function

sorry for my bad english
Last edited on
http://www.cplusplus.com/reference/stl/list/erase/

This accepts an iterator for the position you want erased.
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
void removeFromList(list<int> &L)
{
    list<int>::iterator root, afterRoot, bf_afterRoot;
    root = L.begin();

    while(root != L.end())
    {
        afterRoot = ++root;
        bf_afterRoot = root;
        while(afterRoot != L.end())
        {
            if(*afterRoot == *root)
            {
                L.erase(afterRoot);
                bf_afterRoot = afterRoot;
            }
            else
            {
                bf_afterRoot = afterRoot;
                afterRoot = ++afterRoot;
            }
        }
    root = ++root;
    }
}


where is my wrong?
afterRoot = ++root; increments root
root = ++root; increments root again.

I suggest moving line 9 before line 8, and then removing line 23 and see if that gives you your intended results.

A simpler way (I believe you're looking for duplicate values?) is to use a basic for loop (if you have C++11 features (auto), it's even easier) inside of another for loop.
1
2
3
4
5
6
7
8
   // First iterator
   for (list<int>::iterator i = L.begin(); i != L.end(); i ++)
      // Second iterator to compare against
      for (list<int>::iterator j = i + 1; j != L.end(); j ++)
         // Check to see if the values are equal
         if (*i == *j)
            // If they are, erase it
            i = L.erase(i);


I didn't have all of your code, and don't feel like typing it all up just to test it, but that should work. You can add a break in there if you know there is only going to be one duplicate.

Edit: I just stumbled across this (didn't know about it) and it looks like it would make your job much easier...
http://www.cplusplus.com/reference/stl/list/unique/
Last edited on
@Volatile Pulse

Thank yooooou soooooooooooo much

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
#include <iostream>
#include <list>

using namespace std;

void add2List(list<int> &L)
{
    int val;

    cout << "Adding values to list :\n";
    cout << ">>";

    while(cin >> val)
    {
        cout << ">>";
        L.push_back(val);
    }
}

void printList(list<int> L)
{
    while(L.size())
    {
        cout << L.front() << " ";
        L.pop_front();
    }
}

int main()
{
    list<int> L;

    add2List(L);

    L.sort();
    L.unique();

    printList(L);

    return 0;
}


done...........
Topic archived. No new replies allowed.