import java.util.Scanner;/** univariate polynomials with integer coefficients represented as
* circular linked lists with head nodes */public class CircularPolynomial
{
// top-level class
private static class PolyNode
{
// data members
int coeff; // coefficient
int exp; // exponent
PolyNode next; // pointer to next node // constructors
PolyNode(int theCoeff, int theExp, PolyNode theNext)
{
coeff = theCoeff;
exp = theExp;
next = theNext;
} PolyNode(int theCoeff, int theExp)
{
coeff = theCoeff;
exp = theExp;
} PolyNode() {}
}
// data members of CircularPolynomial
PolyNode headNode;
static PolyNode lastNode;
// constructors
/** create the zero polynomial */
public CircularPolynomial()
{
headNode = new PolyNode(0, -1);
headNode.next = headNode;
} /** @return polynomial degree */
public int degree()
{
if (headNode.next == headNode) // zero polynomial
return 0;
else // nonzero polynomial
return headNode.next.exp;
}
/** add a new term to the end of the list */
private void append(int theCoeff, int theExp)
{
lastNode.next = new PolyNode(theCoeff, theExp);
lastNode = lastNode.next;
} /** make a clone */
public Object clone()
{
// w will be the clone
CircularPolynomial w = new CircularPolynomial();
lastNode = w.headNode; // use currentNode to march through the polynomial this
PolyNode currentNode = headNode.next;
while (currentNode != headNode)
{
append(currentNode.coeff, currentNode.exp);
currentNode = currentNode.next;
}
// close circular list
lastNode.next = w.headNode; return w;
}
}这是用链表实现的Polynomial,我只给了部分方法,其中的clone()方法看不懂,大家帮忙看看
* circular linked lists with head nodes */public class CircularPolynomial
{
// top-level class
private static class PolyNode
{
// data members
int coeff; // coefficient
int exp; // exponent
PolyNode next; // pointer to next node // constructors
PolyNode(int theCoeff, int theExp, PolyNode theNext)
{
coeff = theCoeff;
exp = theExp;
next = theNext;
} PolyNode(int theCoeff, int theExp)
{
coeff = theCoeff;
exp = theExp;
} PolyNode() {}
}
// data members of CircularPolynomial
PolyNode headNode;
static PolyNode lastNode;
// constructors
/** create the zero polynomial */
public CircularPolynomial()
{
headNode = new PolyNode(0, -1);
headNode.next = headNode;
} /** @return polynomial degree */
public int degree()
{
if (headNode.next == headNode) // zero polynomial
return 0;
else // nonzero polynomial
return headNode.next.exp;
}
/** add a new term to the end of the list */
private void append(int theCoeff, int theExp)
{
lastNode.next = new PolyNode(theCoeff, theExp);
lastNode = lastNode.next;
} /** make a clone */
public Object clone()
{
// w will be the clone
CircularPolynomial w = new CircularPolynomial();
lastNode = w.headNode; // use currentNode to march through the polynomial this
PolyNode currentNode = headNode.next;
while (currentNode != headNode)
{
append(currentNode.coeff, currentNode.exp);
currentNode = currentNode.next;
}
// close circular list
lastNode.next = w.headNode; return w;
}
}这是用链表实现的Polynomial,我只给了部分方法,其中的clone()方法看不懂,大家帮忙看看
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货