If CP Frederik were to die before he became King, CP Mary would become known as Princess Mary and assuming they had no children Prince Joachim then would become Crown Prince and Princess Alexsandra would become Crown Princess in Mary's place.
If Frederik died and he and Mary had children, then CP Mary would still become Princess Mary and their first born child would be come Crown Prince/Princess. Also if this child has not reach the age of majorty when QMII dies, then Prince Joachim would become regent until the child reached adulthood.
Of course if Frederik comes to the thrown and dies before Mary, Mary would become a Dowager Queen Or as they are more commonly known Queen Mother (assuming she had a child) if they did have a child the first born would become King/Queen but if they didn't and assuming Joachim was still alive he would become King and Alexsandra Queen.