Не работает deleteNodeByIndex Двусвязный список Java
Столкнулся с проблемой - не могу реализовать удаление элемента из списка. Удаление с головы и хвоста реализовал, но удаление где-то с середины работает через раз public class LinkedListTabulatedFunction implements TabulatedFunction {
public class FunctionNode {
private FunctionPoint point;
private FunctionNode Prev, Next;
public FunctionNode(FunctionPoint x) {
this.point = x;
}
public FunctionNode() {
point = null;
Prev = null;
Next = null;
}
}
private int lgth, indx, counter = 0;
private FunctionNode Main = new FunctionNode(), Head, Tail, Cur;
{
Main.Next = Main;
Main.Prev = Main;
Head = Main;
Tail = Main;
Cur = Main;
}
public LinkedListTabulatedFunction(double leftX, double rightX, int pointsCount) {
if (pointsCount < 2 || leftX >= rightX)
throw new IllegalArgumentException("Exception: Incorrect borders or count of points");
Head = new FunctionNode();
Main.Next = Head;
Cur = Head;
Head.Prev = Main;
double step = (rightX - leftX) / (pointsCount - 1);
double X = leftX;
for (int i = 0; i < pointsCount; i++) {
Cur.point = new FunctionPoint(X, 0);
Cur.Next = new FunctionNode();
Cur.Next.Prev = Cur;
Cur = Cur.Next;
indx++;
X += step;
}
lgth = pointsCount;
Tail = Cur.Prev;
Tail.Next = Main;
Main.Prev = Tail;
}
public LinkedListTabulatedFunction(double leftX, double rightX, double values[]) {
if (values.length < 2 || leftX >= rightX)
throw new IllegalArgumentException("Exception: Incorrect borders or count of points");
Head = new FunctionNode();
Main.Next = Head;
Cur = Head;
Head.Prev = null;
double step = (rightX - leftX) / (values.length - 1);
double X = leftX;
for (int i = 0; i < values.length; i++) {
Cur.point = new FunctionPoint(X, values[i]);
Cur.Next = new FunctionNode();
Cur.Next.Prev = Cur;
Cur = Cur.Next;
indx++;
X += step;
}
lgth = values.length;
Tail = Cur.Prev;
Tail.Next = Main;
}
FunctionNode getNodeByIndex(int index) {
if (index < 0 || index > lgth)
throw new FunctionPointIndexOutOfBoundsException("getNodeByIndex exception: IndexOutOfBounds");
int fromTail = lgth - index - 1;
int fromHead = index;
int fromCurrent = Math.abs(indx - index);
if (fromTail < fromHead) {
if (fromTail < fromCurrent) {
Cur = Tail;
indx = lgth - 1;
}
} else {
if (fromHead < fromCurrent) {
Cur = Head;
indx = 0;
}
}
if (index < indx) {
while (indx != index) {
Cur = Cur.Prev;
indx--;
}
} else {
while (indx != index) {
Cur = Cur.Next;
indx++;
}
}
return Cur;
}
FunctionNode addNodeToTail() {
Tail.Next.Prev = Tail;
Tail.Next.Next = Main;
Tail = Tail.Next;
lgth++;
Main.Prev = Tail;
return Tail;
}
FunctionNode addNodeByIndex(int index) {
if (index < 0 || index > lgth)
throw new FunctionPointIndexOutOfBoundsException("addNodeByIndex exception: IndexOutOfBounds");
if (index == lgth)
return addNodeToTail();
getNodeByIndex(index);
FunctionNode tmp = new FunctionNode();
tmp.Next = Cur;
tmp.Prev = Cur.Prev;
Cur.Prev.Next = tmp;
Cur.Prev = tmp;
Cur = tmp;
lgth++;
return Cur;
}
FunctionNode deleteNodeByIndex(int index) {
if (index < 0 || index >= lgth)
throw new FunctionPointIndexOutOfBoundsException("deleteNodeByIndex exception: IndexOutOfBounds");
FunctionNode tmp = new FunctionNode();
if (index == lgth - 1) {
tmp = Tail;
Tail = Tail.Prev;
} else if (index == 0) {
tmp = Head;
Head = Head.Next;
} else {
getNodeByIndex(index);
tmp = Cur;
Cur.Prev.Next = Cur.Next;
Cur.Next.Prev = Cur.Prev;
Cur = Cur.Prev;
}
indx--;
lgth--;
return tmp;
}
@Override
public double getLeftDomainBorder() {
return Head.point.getX();
}
@Override
public double getRightDomainBorder() {
return Tail.point.getX();
}
@Override
public int getPointsCount() {
return lgth;
}
@Override
public double getFunctionValue(double x) {
if (x >= getLeftDomainBorder() && x <= getRightDomainBorder())
return ((x - getLeftDomainBorder()) * (Tail.point.getY() - Head.point.getY())) / (getRightDomainBorder() - getLeftDomainBorder()) + Head.point.getY();
return Double.NaN;
}
@Override
public void setPoint(int index, FunctionPoint point) throws FunctionPointIndexOutOfBoundsException, InappropriateFunctionPointException {
if (index < 0 || index > lgth - 1)
throw new FunctionPointIndexOutOfBoundsException("setPoint exception: Out of Index Bounds");
if (getLeftDomainBorder() > point.getX() || getRightDomainBorder() < point.getX()) {
throw new InappropriateFunctionPointException("setPoint exception: Out of X Bounds");
}
FunctionNode node = getNodeByIndex(index);
node.point = new FunctionPoint(point);
}
@Override
public double getPointX(int index) throws FunctionPointIndexOutOfBoundsException {
if (index > lgth - 1 || index < 0)
throw new FunctionPointIndexOutOfBoundsException("getPointX exception: Out of Bounds");
return getNodeByIndex(index).point.getX();
}
@Override
public void setPointX(int index, double x) throws InappropriateFunctionPointException {
if (index > lgth - 1 || index < 0)
throw new FunctionPointIndexOutOfBoundsException("setPointX exception: Out of Bounds");
if (getLeftDomainBorder() > x || getRightDomainBorder() < x)
throw new InappropriateFunctionPointException("setPointX exception: Out of X Bounds");
FunctionNode node = getNodeByIndex(index);
node.point.setX(x);
}
@Override
public double getPointY(int index) throws FunctionPointIndexOutOfBoundsException {
if (index > lgth - 1 || index < 0)
throw new FunctionPointIndexOutOfBoundsException("getPointY exception: Out of Bounds");
return getNodeByIndex(index).point.getY();
}
@Override
public void setPointY(int index, double y) throws FunctionPointIndexOutOfBoundsException {
if (index > lgth - 1 || index < 0)
throw new FunctionPointIndexOutOfBoundsException("setPointY exception: Out of Bounds");
getNodeByIndex(index).point.setY(y);
}
@Override
public void deletePoint(int index) throws FunctionPointIndexOutOfBoundsException, IllegalStateException {
if (lgth < 3)
throw new IllegalStateException("Exception: Length < 3");
if (index > lgth - 1 || index < 0)
throw new FunctionPointIndexOutOfBoundsException("deletePoint exception: Out of Bounds");
deleteNodeByIndex(index);
}
@Override
public void addPoint(FunctionPoint point) throws InappropriateFunctionPointException {
if (point.getX() < getLeftDomainBorder()) {
FunctionNode Node = new FunctionNode(point);
Node.Next = Head;
Node.Prev = null;
Head = Node;
lgth++;
} else if (getRightDomainBorder() < point.getX()) {
Tail.Next = new FunctionNode(point);
addNodeByIndex(lgth);
} else {
for (Cur = Head, indx = 0; Cur.point.getX() <= point.getX(); Cur = Cur.Next, indx++) {
if (Cur.point.getX() == point.getX())
throw new InappropriateFunctionPointException("addPoint exception: Similar X");
}
addNodeByIndex(indx).point = point;
}
}
}