Shortcode leaves no space for other elements?

I think your problem is that your shortcode echos it’s output rather than returning it.

So in your shortcode function, remove any direct output (that is stuff between ?>.....<?php and any echos and rather gather your output in a variable and return that:

function my_shortcode_cb($atts) {
    ....
    $output = ....
    $output .= ....
    return $output;
}

More detail:

So instead of

while ($a != $b) {
    echo "<ul>";
    if ($c == $d) {
        foreach ($e as $k => $v) {
            ?>
            <li class="item-<?php echo $k; ?>"><?php echo $v; ?></li>
            <?php
        }
    }
    echo "</ul>";
}

you do:

$output="";
while ($a != $b) {
    $output .= "<ul>"
    if ($c == $d) {
        foreach ($e as $k => $v) {
            $output .= "<li class=\"item-".$k."\">".$v."</li>";
        }
    }
    $output .= "</ul>";
}
return $output;

Or, if you wanna be cheap ;-), you can do this:

ob_start();
while ($a != $b) {
    echo "<ul>";
    if ($c == $d) {
        foreach ($e as $k => $v) {
            ?>
            <li class="item-<?php echo $k; ?>"><?php echo $v; ?></li>
            <?php
        }
    }
    echo "</ul>";
}
return ob_get_clean();

BTW: If your shortcode callback is really really long, then chances are that it’s got lots of potential for refactoring. And if you’d do that, chances are that you’d assemble your output from strings returned by function calls, anyways.