Representation and solution of a chained linear table

Source: Internet
Author: User

# Include
 
  
Using namespace std; struct LNode {// node class double p; // coefficient item int e; // index item LNode * next ;};
 
Class LsList {// One-dimensional polynomial chained linear table private: LNode head; // The size_t size of the header node; // The number of nodes double mx; // The variable value public of The unary polynomial: LNode & GetHead () {return head;} size_t & GetSize () {return size;} size_t GetSize () const {return size;} LsList () {// Initialize an empty linear table size = 0; head. next = 0; mx = 0;} // initialize the coefficient and index LsList (double ps [], int es [], int n) in the linear table of a polynomial) // ps indicates the coefficient item array, es indicates the index item array, and n indicates the number of The unary polynomials {size = 0; head. next = 0; mx = 0; int I = 0; LNode * p = & head; for (; I
 
  
E = es [I]; p1-> p = ps [I]; p-> next = p1; p = p-> next; size ++ ;} p-> next = 0;} // copy the constructor LsList (LsList & list2) {size = 0; head. next = 0; mx = 0; LNode * p = & head; LNode * p1 = list2.GetHead (). next; while (p1! = 0) {LNode * p2 = (LNode *) malloc (sizeof (LNode); p2-> e = p1-> e; p2-> p = p1-> p; p-> next = p2; p1 = p1-> next; p = p-> next; size ++;} p-> next = 0 ;} // The Value assignment operator overload LsList & operator = (LsList & list2) {clear (); LNode * p = & head; LNode * p1 = list2.GetHead (). next; while (p1! = 0) {LNode * p2 = (LNode *) malloc (sizeof (LNode); p2-> e = p1-> e; p2-> p = p1-> p; p-> next = p2; p1 = p1-> next; p = p-> next; size ++;} p-> next = 0; return * this ;} // set the variable value Xvoid SetX (double d) {mx = d;} // clear a linear table void clear () {LNode * p = head. next; while (p! = 0) {LNode * p2 = p-> next; free (p); p = p2;} size = 0; head. next = 0; mx = 0;} // print the void PrintLsList () {if (mx = 0) {cout <"set the variable x value first" <
  
   
P <"*" <
   <<"^"<
    
E; p = p-> next; if (p! = 0 & p-> p> 0) cout <"+" ;}}// calculate the result of the polynomial double GetResult () {double sum = 0; if (mx = 0) {cout <"set variable x value first" <
    
     
P) * pow (mx, p-> e); p = p-> next;} return sum;} // delete a data bool DeleElem (int e) in the polynomial) {LNode * p1 = & head; LNode * p = p1-> next; while (p! = 0) {if (p-> e = e) {p1-> next = p-> next; free (p); size --; return true ;} p1 = p1-> next; p = p1-> next;} return false;} // Add a data void AddElem (double pi, int ei) to the polynomial) {LNode * p = head. next; if (p = 0) {LNode * p2 = (LNode *) malloc (sizeof (LNode); p2-> e = ei; p2-> p = pi; head. next = p2; size ++; p2-> next = 0; return;} if (p-> e> ei) {LNode * p2 = (LNode *) malloc (sizeof (LNode); p2-> e = ei; p2-> p = pi; head. next = p2; p2-> next = p; size ++; r Eturn;} while (p! = 0) {if (p-> e = ei) {if (p-> p + pi) = 0) {DeleElem (ei ); // Delete return;} else {p-> p + = pi; return ;}} if (p-> e
     
      
Next! = 0 & p-> next-> e> ei) | (p-> e
      
        Next = 0) {LNode * p2 = (LNode *) malloc (sizeof (LNode); p2-> e = ei; p2-> p = pi; p2-> next = p-> next; p-> next = p2; size ++; return;} p = p-> next ;}} // complete the polynomial addition and destroy the polynomial list2void AddList (LsList & list2) {int s1 = size; int n = 0; size = 0; LNode * p1 = head. next; LNode * p = & head; LNode * p2 = list2.GetHead (). next; while (p1! = 0 & p2! = 0) {if (p1-> e <p2-> e) {p-> next = p1; p1 = p1-> next; p = p-> next; size ++;} else {if (p1-> e> p2-> e) {p-> next = p2; p2 = p2-> next; p = p-> next; size ++;} else {double db = (p1-> p) + (p2-> p); if (db! = 0) {p1-> p = db; p-> next = p1; p = p-> next; size ++; p1 = p1-> next; LNode * p3 = p2-> next; free (p2); p2 = p3; size ++;} if (db = 0) {LNode * pa = p1-> next; LNode * pb = p2-> next; free (p1); free (p2); p1 = pa; p2 = pb; n ++ ;}}}} if (p1 = 0) {p-> next = p2; size + = list2.GetSize ()-size-n;} else {p-> next = p1; size + = s1-size-n;} list2.GetHead (). next = 0;} // completes the polynomial subtraction operation and destroys the polynomial list2void Sub (LsList & list2) {LNode * p = list2.GetHead (). Next; while (p! = 0) {p-> p =-(p-> p); p = p-> next;} AddList (list2);} // completes the multiplication of polynomials, and destroy the polynomial list2void Mul (LsList & list2) {LNode * p = list2.GetHead (). next; LsList ili = * this; while (p! = 0) {LsList li = ili; for (LNode * p1 = li. GetHead (). next; p1! = 0; p1 = p1-> next) {p1-> p = (p1-> p) * (p-> p ); p1-> e = p1-> e + p-> e;} AddList (li); p = p-> next ;}/// complete the phase division operation of polynomials, and destroy the polynomial list2 // void Devi ()//{//////}};
      
     
    
   <<"^"<
  
 

Int main () {/** Test 1 * // create an empty linear table LsList list1; // Add the item information list1.AddElem (1, 2) to the empty linear table ); list1.AddElem (1.5, 3); list1.AddElem (2, 4); // set the value of list1.SetX (3) of the variable x of the polynomial; cout <

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.