Beispiel #1
0
int primeCount(forward_list<int> lst){
    if(lst.empty()){ //base case
        return 0; //empty list has no (prime) numbers
    }
    if(isPrime(lst.front())){ //check if first item is prime
        cout << lst.front() << endl;
        lst.pop_front(); //can do this bc lst is passed by value
        return 1 + primeCount(lst); //recursive call
    }
    else{ //first number isnt prime
        lst.pop_front(); //dont output, just delete it
        return primeCount(lst); //dont add 1 to primecount
    }
}
Beispiel #2
0
void printLots(forward_list<T> L, forward_list<int> P){
    int temp = 0;
    while(!P.empty()){
        int pos = P.front() - temp;
        temp = pos;
        for(; pos > 0; --pos){
            L.pop_front();
        }
        cout << L.front() << " ";
        P.pop_front();
    }
    //keep L.pop_front() until you get to the right position
    
    return;
}
Beispiel #3
0
void printLots(forward_list<T> L, forward_list<int> P)
{
    int num_elem = 0;
    
    for (typename forward_list<T>::iterator count = L.begin() ; count != L.end() ; ++count)
    {
        num_elem = num_elem + 1;
    }
    
    while(!P.empty())
    {
        int pos = P.front();
        
        if(pos >= num_elem)
        {
            cout << "Position out of bounds!" << endl;
            return;
        }
        
        typename forward_list<T>::iterator start = L.begin();
        
        for(int i = 0 ; i < pos ; i++)
        {
            ++start;
        }
        
        cout << *start << ", ";
        
        P.pop_front();
    }
    
    cout << endl;
}
Beispiel #4
0
int primeCount(forward_list<int> lst)
{
    int total;
    
    if(lst.empty())
    {
        return 0;
    }
    
    else
    {
        bool prime = isPrime(lst.front());
        lst.pop_front();
        
        if(prime == true)
        {
            total = 1 + primeCount(lst);
        }
        
        if(prime == false)
        {
            total = primeCount(lst);
        }
    }
    
    return total;
}
Beispiel #5
0
forward_list<T> listCopy(forward_list<T> L, forward_list<T> &P){
    //concatenate L - in reverse - to the back of P
    forward_list<T> temp;
    while(!L.empty()){
        temp.push_front(L.front());
        L.pop_front();
    }
    
    P.reverse();
    while(!P.empty()){
        temp.push_front(P.front());
        P.pop_front();
    }
    
    P = temp;
    return P;   
}
Beispiel #6
0
int primeCount(forward_list<int> lst)
{
    if(lst.empty()) //If list's size is 0, return 0;
    {
        return 0;
    }
    else if(isPrime(lst.front())) //If the number is prime, pop the front, add one, call the function again.
    {
        lst.pop_front();
        return 1 + primeCount(lst);
    }
    else //If it isn't prime, pop the front and call the function again.
    {
        lst.pop_front();
        return primeCount(lst);
    }
    
}
Beispiel #7
0
int primeCount(forward_list<int> lst){
	if(lst.empty()){
		return 0;
	}else{ 
		bool prime = isPrime(lst.front());
		lst.pop_front();
		if(prime){
			return 1 + primeCount(lst);
		}else{
			return primeCount(lst);
		}
	}
}
Beispiel #8
0
int primeCount( forward_list<int> lst ){
    
    static int counter = 0;
    if( lst.empty() ) { return 0; }// If lst is empty, then counter = 0.
    else
    {
        if ( isPrime( lst.front() ) ){ ++counter;}
}
    
    lst.pop_front();
    primeCount(lst); 
    
    return counter;
} // Function that returns number of Primes in a forward_list