Sunday, March 29, 2009

[Cpp-Programming] i need help in C++

Alright.. i kinda solved the question, but i cant solve the error.
please let me know if you can help.

-The question is:
Create the attached linked list using stuct nodes. Use the linked
list to print out as a polynomial... -3x^7 + 4x^5 + 7x^3 - x^2 + 9.

-If you would like to see a diagram for it go to:
http://bb.cos.edu/courses/1/09-S-CSCI002-22439/content/_286958_1/linked_list.pdf


Here is what i did.

#include <iostream>
#include <string>
using namespace std;


struct Node{
string coeff;
int power;
Node *nextPtr;
};
Node* inputPolynomial( void );
void deletePolynomial( Node * );
double evaluatePolynomial(Node *, double x);
void printPolynomial( Node * );


void addNodeAfter (Node *, string, int);


int main(void){

int size = 7;
Node *dhead = NULL;
Node *cur = NULL;

dhead = new Node();
cur = dhead;

cur ->coeff = "-3";
cur->power = 7;
cur->nextPtr = new Node();

cur = cur->nextPtr;

cur->coeff = "4x";
cur->power = 5;
cur->nextPtr = new Node();

cur = cur->nextPtr;

cur->coeff = "7";
cur->power = 3;
cur->nextPtr = new Node();

cur = cur->nextPtr;

cur->coeff = "-1";
cur->power = 2;
cur->nextPtr = new Node();

cur = cur->nextPtr;

addNodeAfter(dhead, "9" , 0);
printPolynomial(dhead);

return(0);
}

void printPolynomail(Node *passNode){
Node *tempCur = passNode;

while (tempCur->nextPtr != passNode){

cout << tempCur->coeff << endl;
tempCur = tempCur->nextPtr;
}
cout << tempCur->coeff << endl;

}

void deletePolynomial(Node *passNode){
passNode->nextPtr = (passNode->nextPtr)->nextPtr;
}

void addNodeAfter (Node*passNode, string coeff, int power){

Node *temp = passNode->nextPtr;

passNode->nextPtr = new Node();
(passNode->nextPtr)->coeff = coeff;
(passNode->nextPtr)->power = power;
(passNode->nextPtr)->nextPtr = temp;
//(passNode->nextPtr)->prevPtr = passNode;

//temp->prevPtr = passNode->nextPtr;


}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to Cpp-Programming@googlegroups.com
To unsubscribe from this group, send email to Cpp-Programming+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Cpp-Programming?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

<< Home