Advertisement
zaidhuda

Polynomials List with Addition

Mar 27th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. // CSC 2101 Data Structures And Algorithms
  2. // Assignment 1: Adding Polynomials
  3. // 1221417 Zaid Huda  
  4.  
  5. #include "cmath"
  6.  
  7. class Node {
  8.   public:
  9.     int coef;   // coefficient of x
  10.     int exp;    // degree of x
  11.     Node *next; // pointer to next Node
  12. };
  13.  
  14. class List {
  15.   private:
  16.     Node *head; // head of List
  17.  
  18.   public:
  19.     List(){ head = NULL; } // head initilized to NULL (empty list)
  20.  
  21.     ~List(){
  22.       clear();      // clear the list
  23.       delete head;  // delete head
  24.     }
  25.  
  26.     Node *first(){
  27.       return head; // return head as first Node (or NULL for empty list)
  28.     }
  29.  
  30.     void insert(int coef, int exp){
  31.       Node *newNode = new Node;   // temporary Node with new info
  32.       newNode->coef = coef;       // set new coef to argument coef
  33.       newNode->exp = exp;         // set new exp to argument exp
  34.       newNode->next = NULL;       // set next to NULL
  35.       bool added = false;         // temporary boolean to check whether node added in or at the end of the list
  36.  
  37.       if (head == NULL) head = newNode; // if empty list, set head to new Node
  38.       else {                            // iterate non empty list
  39.         Node *currNode = head;     // set current Node to head
  40.         Node *prevNode = head;     // set previous Node to head
  41.         bool first = true;
  42.         while(currNode){           // check if current Node is last
  43.           if(currNode->exp < exp){ // check if current Node exp less than argument exp, if true
  44.             if(first)              // if first Node
  45.               head = newNode;      // set head to new Node
  46.             else
  47.               prevNode->next = newNode; // set new Node after previous Node
  48.             newNode->next = currNode;   // set current Node after new Node
  49.             added = true;               // set added to true
  50.             break;                      // break the iteration
  51.           }
  52.           first = false;
  53.           prevNode = currNode;       // set previous Node to current Node before continuing the loop
  54.           currNode = currNode->next; // iterate the list
  55.         }
  56.         if(!added){
  57.           prevNode->next = newNode; // if not added, add new Node at the end of list
  58.         }
  59.       }
  60.     }
  61.  
  62.     void addNode(int coef, int exp){  // add a 'node' with coef and exp
  63.       Node *currNode = head;      // set head as current Node
  64.       bool newNode = true;        // temporary to check 'node' is unique
  65.       while(currNode){            // iterate non empty list
  66.         if(currNode->exp == exp){ // check if nodes has same exp
  67.           currNode->coef += coef; // add coef of same exp
  68.           newNode = false;        // set new as false ('node' is added to list)
  69.           break;
  70.         }
  71.         currNode = currNode->next; // iterate the list
  72.       }
  73.       if(newNode) insert(coef, exp); // if it is new 'node', insert to list
  74.     }
  75.  
  76.     void addPolynomials(List that){             // add that to this list
  77.       Node *currNode = that.first();            // get first Node to start iteration
  78.       while(currNode){                          // iterate that list
  79.         addNode(currNode->coef, currNode->exp); // add every Node to this
  80.         currNode = currNode->next;
  81.       }
  82.     }
  83.  
  84.     void display(){
  85.       Node *currNode = head;
  86.       bool first = true;
  87.       while(currNode){
  88.         int coef = currNode->coef, exp = currNode->exp;
  89.         if(coef != 1){
  90.           if (first)  std::cout << coef;
  91.           else        std::cout << std::abs(coef);
  92.         }
  93.         if(exp != 1 && exp != 0) std::cout << "x^" << exp;
  94.         else if(exp == 1) std::cout << 'x';
  95.         if(coef == 1 && exp == 0) std::cout << "1";
  96.         if(currNode->next){
  97.           if(currNode->next->coef < 0) std::cout << " - ";
  98.           else std::cout << " + ";
  99.         }
  100.         first = false;
  101.         currNode = currNode->next;
  102.       }
  103.     }
  104.  
  105.     void clear(){
  106.       Node *currNode = head;   // set head as current Node
  107.       while(currNode){         // iterating the list
  108.         head = currNode->next; // set head to next Node
  109.         delete currNode;       // delete current Node
  110.         currNode = head;       // set current Node as head
  111.       }
  112.     }
  113. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement