public PolynomList(List<PolynomNode> list) { if (list.Count > 0) { bool sorted = false; PolynomNode pos = null; while (!sorted) { sorted = false; for (int i = 0; i < list.Count - 1; i++) { if (list[i].Power < list[i + 1].Power) { pos = list[i + 1]; list[i + 1] = list[i]; list[i] = pos; sorted = true; } } } _First = list[0]; pos = list[0]; for (int i = 1; i < list.Count; i++) { if (pos.Value != 0) { pos.ChildNode = list[i]; pos = pos.ChildNode; } } } }
public PolynomList(List<int> values) { int count = 0; while ((count < values.Count) && (values[count] == 0)) count++; if (count < values.Count) { int power = values.Count - count - 1; _First = new PolynomNode(values[count], power); PolynomNode pos = _First; for (int i = count + 1; i < values.Count; i++) { power--; if (values[i] != 0) { pos.ChildNode = new PolynomNode(values[i], power); pos = pos.ChildNode; } } } }
public static PolynomList operator +(PolynomList a, PolynomList b) { PolynomList summ = new PolynomList(); PolynomNode sx = new PolynomNode(0, -1); summ.First = sx; int value = 0; PolynomNode ax = a.First as PolynomNode, bx = b.First as PolynomNode; //добавляем пока не кончится один из списков while ((ax != null) && (bx != null)) { if (ax.Power == bx.Power) { value = ax.Value + bx.Value; if (value != 0) { sx.ChildNode = new PolynomNode(value, ax.Power); sx = sx.ChildNode; } ax = ax.ChildNode; bx = bx.ChildNode; } else { if (ax.Power > bx.Power) { sx.ChildNode = new PolynomNode(ax.Value, ax.Power); ax = ax.ChildNode; } else { sx.ChildNode = new PolynomNode(bx.Value, bx.Power); bx = bx.ChildNode; } sx = sx.ChildNode; } } //добавляем конец a while (ax != null) { sx.ChildNode = new PolynomNode(ax.Value, ax.Power); sx = sx.ChildNode; ax = ax.ChildNode; } //добавляем конец b while (bx != null) { sx.ChildNode = new PolynomNode(bx.Value, bx.Power); sx = sx.ChildNode; bx = bx.ChildNode; } //выбрасываем вспомогательный summ.First = (summ.First as PolynomNode).ChildNode; return summ; }
public void Insert(IPolynomNode inode, int value) { PolynomNode node = inode as PolynomNode; if (node.Power != 0) { if (node.ChildNode == null) { node.ChildNode = new PolynomNode(value, node.Power - 1); } else { if (node.ChildNode.Power == node.Power - 1) throw new Exception("Нет свободной степени для вставки"); else { PolynomNode ins = new PolynomNode(value, node.Power - 1); ins.ChildNode = node.ChildNode; node.ChildNode = ins; } } } }
public void Dispose() { _First = null; }
public void AddToBegin(int value) { if (value != 0) { int power = 0; if (_First != null) power = _First.Power; PolynomNode node = new PolynomNode(value, power); node.ChildNode = _First; _First = node; } }