I've made an algorithm solving Hanoi Tower puzzles, for n disks and m pegs.It uses lists as pegs, each list's element contains disks array - {0, 0, 0} means the peg is empty, {1, 2, 0} means the peg contains 1 and 2 disks, {3, 0, 0} means the peg contains the biggest disk only.The algorithm is iterative (and has to be), to solve for 3 pegs it repeats the following steps:Move smallest disk to the right is number of disks is odd, move it to the left if evenMake only one possible move left, without toughing the smallest diskVery simple and cool algorithm. If pegs > 3, I repeat the above (pegs - 2) times - divide and conquer, until the last peg is filled.My problem is that the algorithm is very ugly. As someone said: If you need more than 3 levels of indentation, you're screwed anyway, and should fix your program..I've been thinking on how can I optimize my code for a couple of days, and I do not know what more I could possibly do.This is the whole program (console output is not in English though, but variables are, so it should be clear enough): http://ideone.com/Jwt4nNThis is the working algorithm on ideone: http://ideone.com/woEg9oAnd I'm pasting it here:void hanoi(rod **head, rod **tail){    time_start();    rod *temp, *temp2, *head_t = *head;    int flag = 0, div = 0;    for(int i = 0; i < rods - 2; i++)    {        while((*tail)->disks[n - 1] != n)        {            temp = *head, flag = 0;            if(div % 2 == 0) // move smallest disk to the right if odd, or left if even            {                while(temp != (*tail)->next)                {                    if(flag == 1)                    {                        unshift(1, &temp->disks);                        break;                    }                    if(temp->disks[0] == 1)                    {                        pop(&temp->disks);                        flag = 1;                    }                    if(n % 2 == 0)                    {                        temp = (temp->next == (*tail)->next) ? *head : temp->next;                    }                    else                    {                        temp = (temp->prev == (*head)->prev) ? *tail : temp->prev;                    }                }            }            else // make first possible move            {                while(temp != (*tail)->next && flag == 0)                {                    temp2 = *head;                    while(temp2 != (*tail)->next)                    {                        if((temp->disks[0] < temp2->disks[0] || temp2->disks[0] == 0) && temp->disks[0] != 1 && temp->disks[0] != 0 && temp->num != temp2->num)                        {                            int t = temp->disks[0];                            pop(&temp->disks);                            unshift(t, &temp2->disks);                            flag = 1;                            break;                        }                        temp2 = temp2->next;                    }                    temp = temp->next;                }            }            div++, moves++;        }        // divide and conquer        if(i < rods - 3)        {            if((*head)->num <= i + 1)                *head = (*head)->next;            if((*tail)->num <= i + 3)                *tail = (*tail)->next;        }    }    *head = head_t;    time_stop();}/** * Input: {1, 2, 3, 4, 0}, el = 5 * Output: {5, 1, 2, 3, 4} *  * Input array ALWAYS contains 0's at the end */void unshift(int el, int **arr){    for(int i = (int)n - 1; i > 0; i--)    {        (*arr)[i] = (*arr)[i - 1];    }    (*arr)[0] = el;}/** * Input: {1, 2, 3, 4} * Output: {2, 3, 4, 0} */void pop(int **arr){    for(int i = 0; i < (int)n - 1; i++)    {        (*arr)[i] = (*arr)[i + 1];    }       (*arr)[n - 1] = 0;}ROD is the name of the list structure. Head points to the first element, tail to the 3rd (at the beginning). If pegs > 3, head and tail move to the right repeatedly (pegs - 3) times.If anything would be unclear, please write this in the comment, and I'll try to explain it best as I can.Any suggestions would be welcomed.EDIT: You say it does not compile.  I am using Visual Studio 2012, and it does compile for me - .cpp extension, C++ file, and I guess the compiler is also for C++.
Hanoi Towers solver
c++;c;algorithm