Reduce elements repeated of queue





1
Date Submitted Thu. Dec. 20th, 2007 2:59 PM
Revision 1 of 1
Helper eldaniel
Tags "CPlusPlus" | "Queue"
Comments 0 comments
Reduce elements repeated of queue ...

#include <iostream>
#include <queue>

using namespace std;

void reducirCola(queue <int> &c);

int main(){
    queue <int> c;

    c.push(1);
    c.push(1);
    c.push(1);
    c.push(2);
    c.push(2);
    c.push(2);
    c.push(3);
    c.push(3);
    c.push(3);

    reducirCola(c);

    while (!c.empty()){
        cout << c.front() << endl; c.pop();
    }

    return 0;
}

void reducirCola(queue <int> &c){
    queue <int> q;
    int x, y, z;

    if (!c.empty()){
        x = c.front(); c.pop();
        q.push(x);

        while (!c.empty()){
            y = c.front(); c.pop();

            if (x != y){
                q.push(y);
                x = y;
            }
        }
    }

    while (!q.empty()){
        z = q.front(); q.pop();
        c.push(z);
    }
}
 

Daniel Carrasco

daniel.to.md

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Scripter i_kenneth

Votes Down