// Move the nth disk of the first tower public void MoveDiskFromFirstTowerToLast(int n) { if (first == null || n == 0) { return; } // Pop the disks that's smaller than n in the 1st stack List <int> firstDisks = PopDisksFromtheStackAboveDiskN(first, n); // Get disk N from the 1st stack int diskN = first.pop(); // Put the disks back into the 1st stack PushDisksBackTotheStack(first, firstDisks); if (second != null) { // Insert diskN to the 2nd stack List <int> secDisks = InsertDiskIntoAStack(second, diskN, false); // Put the disks back into the 2nd stack PushDisksBackTotheStack(second, secDisks); } if (third == null) { third = new StackOfDisks(); } // Insert diskN to the 3rd stack List <int> thdDisks = InsertDiskIntoAStack(third, diskN, true); // Put the disks back into the 3rd stack PushDisksBackTotheStack(third, thdDisks); }
public List <int> InsertDiskIntoAStack(StackOfDisks s, int diamterOfDisk, bool putdown) { List <int> disks = new List <int>(); while (true) { int d = s.pop(); if (d < diamterOfDisk) { disks.Add(d); } else { s.push(d); // Put the bigger disk back to stack if (putdown) { s.push(diamterOfDisk); // put the diskN to the stack } break; } } return(disks); }