detect screen width in functions

According to codex, wp_is_mobile(); is a boolean function that returns true if the user is visiting the website on a mobile device, so what you need is:

if ( wp_is_mobile() ) {
    // Run this only for mobile visitors
    add_filter( 'wp_nav_menu_items', 'B_function', 10, 2 ); function B_function(/* add B */ );
} else {
    //If we are not on mobile, then run this filter
    add_action( 'wp_head', 'A_function' ); function A_function(/* add A */);
}