You must use Term Class and Linked List Input file: 1 2 - 2 1 1 0 (Coefficient E
ID: 3611647 • Letter: Y
Question
You must use Term Class and Linked List Input file: 1 2 - 2 1 1 0 (Coefficient Exponent Coefficient Exponent Coefficient Exponent) Output : Term class Linked List Example : 2x + 1 Now you have already had the class declaration for a linked list You should read each pair of coefficient and exponent from an input file whose format is as follows Input 1 2 - 1 2 -2 1 1 0 Output x ^ 2 - 2x + 1 What do you need for creating a list? A head pointer Point to the first element A loop Terminated when the input file is ended A tail pointer Always point to the last element A new element should be connected after this element Pseudo codeExplanation / Answer
#include#includeusing namespace std;class Term;class Term{private:int coeff,exp;public:Term *next;Term(int a=0,int b=0){coeff=a;exp=b;}int getCoffecient(){return coeff;}int getExponenet(){return exp;}};Term *head=NULL,*tail=NULL;void insert(int c,int e){if(head == NULL){head = new Term(c,e);head->next = NULL;tail = head;}else{tail -> next = new Term(c,e);tail = tail->next;tail->next =NULL;}}int main(){int c,e;char file[100];coutfile;ifstream myfile (file);if (myfile.is_open()){while (! myfile.eof() ){myfile>>c;if(myfile.eof())break;myfile>>e;insert(c,e); }myfile.close();}else coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.