Changing whitespace and replacing "Walker" with "W", as a long variable name is merely clutter in a short piece of code...
Code:
void printList() {
Node *W;
W = pHead;
while (W != NULL) {
if (W->power != 0) {
if (W->coeff > 0) {
if (W == pHead) cout << W->coeff << "x" << "^" << W->power;
else if (W->power > 1) cout << "+" << W->coeff << "x" << "^" << W->power;
else cout << "+" << W->coeff << "x";
} else {
if (W->power > 1) cout << W->coeff << "x" << "^" << W->power;
else cout << "+" << W->coeff << "x";
}
} else {
if (W->coeff > 0) cout << "+" << W->coeff;
else cout << W->coeff;
}
W = W->next;
}
}
};
There's the extra close brace, which might be a copy/paste slip.
There's a question... although x^0 is unity (when x is not zero), is this really part of the requirement, to drop "x^0" from the expression? In which case, you should also test for "W == pHead" in the second section, to also avoid the spurious prefixing of a "+" sign.
Also, the "+" is wrong in the fifth "cout" line.
From your description, I assume that "power" is unsigned. Even so, I would be tempted to use "W->power" != 1", just so negative powers would not be dropped. It will suffice, whether the field is signed or unsigned.
Anyhow, my pet hate is the current trend of using a code layout that completely obscures everything.
An alternative version would be...
Code:
void printList() {
Node *W;
char *p = ""; // don't need infix "+" at the start
for (W = pHead; W != NULL; W = W->next) {
if (W->coeff > 0) cout << p;
p = "+"; // from now on, may need to force an infix "+"
cout << W->coeff;
if (W->power != 0) {
cout << "x";
if (W->power != 1) cout << W->power;
}
}
}
_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports."
Kamran Nazeer