Листинг списков
class shiplist { public:
struct ship //элемент списка { int x,y; ship* nextright; ship* nextdown;
ship(int _x, int _y, ship *nr = NULL, ship *nd = NULL)// конструктор с передающ значениями { x = _x; y = _y; nextright = nr; nextdown = nd; } };
int shipCount; ship* first; // Первый элемент ship* firstdown; //Верхний элемент ship* lastright; //Последний элемент в списке корабля ship* lastdown;// Последний элемент в списке кораблей
shiplist() // Конструктор списка
{ shipCount = 0; first = firstdown = lastright = lastdown = NULL; }
void addshipFirst(int _x, int _y) //добавление первого корабля { ship* newship = new ship(_x, _y);
first = newship; lastright = newship; lastdown = newship; shipCount++;
}
void addshipRight(int _x, int _y) //добавление корабля вправо(ячейки) { ship* newship = new ship(_x, _y);
lastright->nextright = newship; lastright = newship; shipCount++;
} void addshipDown(int _x, int _y) //добавление первой ячейки следующего корабля { ship* newship = new ship(_x, _y);
lastdown->nextdown = newship; lastdown = newship; lastright = newship; shipCount++; }
bool delShip (int _x, int _y) //удаление ячейки { ship* prev = NULL, *current = first, *lastdown = first, *prevup = NULL; while (current) //пока не 0 { while (current->nextdown) //пока снизу не ноль { while(current->nextright) //пока справа не ноль { if ((current->x == _x) && (current->y == _y)) //если значения совпали { if (current == first) //если первый { if(current->nextright) //если справа что-то есть { current->nextright->nextdown = current->nextdown; first = current->nextright; lastdown = first; delete current; shipCount--; current = first; return true; } else { first = current->nextdown; lastdown = first; delete current; shipCount--; current = first; return true; }
}
else if ((prev == 0) && (prevup!= 0) && (current->nextright!= 0)) //если слева пусто и сверху есть клетка и справа не пусто { prevup->nextdown = current->nextright; current->nextright->nextdown = current->nextdown; lastdown = current->nextright; delete current; shipCount--; current = lastdown; return true; }
else if (prev) //если предыдущий не равен 0 { prev->nextright = current->nextright; delete current; shipCount--; current = prev->nextright; return true; }
} else //если значения не совпали идём дальше { prev = current; current = current->nextright; } } if ((current->x == _x) && (current->y == _y)) //если справа 0 { if(prev) //если слева не пусто { prev->nextright = 0; delete current; shipCount--; current = prev; return true; }
else if ((prev == 0) && (prevup!= 0) && (current->nextright == 0) && (current->nextdown!=0)) //если слева пусто и сверху есть клетка и справа пусто и снизу не пусто { prevup->nextdown = current->nextdown; lastdown = current->nextdown; delete current; shipCount--; current = lastdown; return true; }
}
prevup = lastdown; current = lastdown->nextdown; lastdown = current; prev = 0;
} delete current; shipCount--; return true;
} return false; } }
|