Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CSC 2101 Data Structures And Algorithms
- // Assignment 1: Adding Polynomials
- // 1221417 Zaid Huda
- #include "cmath"
- class Node {
- public:
- int coef; // coefficient of x
- int exp; // degree of x
- Node *next; // pointer to next Node
- };
- class List {
- private:
- Node *head; // head of List
- public:
- List(){ head = NULL; } // head initilized to NULL (empty list)
- ~List(){
- clear(); // clear the list
- delete head; // delete head
- }
- Node *first(){
- return head; // return head as first Node (or NULL for empty list)
- }
- void insert(int coef, int exp){
- Node *newNode = new Node; // temporary Node with new info
- newNode->coef = coef; // set new coef to argument coef
- newNode->exp = exp; // set new exp to argument exp
- newNode->next = NULL; // set next to NULL
- bool added = false; // temporary boolean to check whether node added in or at the end of the list
- if (head == NULL) head = newNode; // if empty list, set head to new Node
- else { // iterate non empty list
- Node *currNode = head; // set current Node to head
- Node *prevNode = head; // set previous Node to head
- bool first = true;
- while(currNode){ // check if current Node is last
- if(currNode->exp < exp){ // check if current Node exp less than argument exp, if true
- if(first) // if first Node
- head = newNode; // set head to new Node
- else
- prevNode->next = newNode; // set new Node after previous Node
- newNode->next = currNode; // set current Node after new Node
- added = true; // set added to true
- break; // break the iteration
- }
- first = false;
- prevNode = currNode; // set previous Node to current Node before continuing the loop
- currNode = currNode->next; // iterate the list
- }
- if(!added){
- prevNode->next = newNode; // if not added, add new Node at the end of list
- }
- }
- }
- void addNode(int coef, int exp){ // add a 'node' with coef and exp
- Node *currNode = head; // set head as current Node
- bool newNode = true; // temporary to check 'node' is unique
- while(currNode){ // iterate non empty list
- if(currNode->exp == exp){ // check if nodes has same exp
- currNode->coef += coef; // add coef of same exp
- newNode = false; // set new as false ('node' is added to list)
- break;
- }
- currNode = currNode->next; // iterate the list
- }
- if(newNode) insert(coef, exp); // if it is new 'node', insert to list
- }
- void addPolynomials(List that){ // add that to this list
- Node *currNode = that.first(); // get first Node to start iteration
- while(currNode){ // iterate that list
- addNode(currNode->coef, currNode->exp); // add every Node to this
- currNode = currNode->next;
- }
- }
- void display(){
- Node *currNode = head;
- bool first = true;
- while(currNode){
- int coef = currNode->coef, exp = currNode->exp;
- if(coef != 1){
- if (first) std::cout << coef;
- else std::cout << std::abs(coef);
- }
- if(exp != 1 && exp != 0) std::cout << "x^" << exp;
- else if(exp == 1) std::cout << 'x';
- if(coef == 1 && exp == 0) std::cout << "1";
- if(currNode->next){
- if(currNode->next->coef < 0) std::cout << " - ";
- else std::cout << " + ";
- }
- first = false;
- currNode = currNode->next;
- }
- }
- void clear(){
- Node *currNode = head; // set head as current Node
- while(currNode){ // iterating the list
- head = currNode->next; // set head to next Node
- delete currNode; // delete current Node
- currNode = head; // set current Node as head
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement