Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

想問一題有關c++中link list的問題(寫多項式)

如題,如何用single link list 來寫出兩個可供讀者輸入的兩個一元多項式,最後再做相加,然後降羃排列出來,請問各位大大如何用c語言寫出來,順便解釋一下步驟意義,麻煩ㄌ,謝謝!

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    #include <iostream.h>

    struct node

    {

    float coef;//係數

    float power;//x的次方數

    struct node*next;//指向下一個node

    };

    typedef struct node*NODEPTR;

    void insert(NODEPTR & Polynomial, float coef, float power);

    void print(NODEPTR Polynomial);//印出list內容

    void merge(NODEPTR & Merge, NODEPTR No_change);

    void free_memory(NODEPTR & Polynomial);//釋放記憶體

    void insert(NODEPTR & Polynomial, float coef, float power)

    {

    NODEPTRrear= NULL;

    NODEPTRfront= NULL;

    NODEPTRnew_node= NULL;

    new_node = new node;

    new_node->coef = coef;

    new_node->power = power;

    new_node->next = NULL;

    if(Polynomial==NULL)

    {

    Polynomial = new_node;

    return;

    }

    if(Polynomial->power < new_node->power)

    {

    new_node->next = Polynomial;

    Polynomial = new_node;

    return;

    }

    if(Polynomial->power == new_node->power)

    {

    Polynomial->coef += new_node->coef;

    return;

    }

    for(front=Polynomial, rear=front->next; rear!=NULL && rear->power>new_node->power; front=front->next, rear=rear->next);

    if(rear==NULL)

    {

    front->next = new_node;

    return;

    }

    if(rear->power == new_node->power)

    {

    rear->coef += new_node->coef;

    return;

    }

    else

    {

    new_node->next = rear;

    front->next = new_node;

    return;

    }

    }

    void print(NODEPTR Polynomial)

    {

    NODEPTRfront= NULL;

    if(Polynomial==NULL)

    {

    cout<<"多項式是空的";

    }

    else

    {

    cout<<Polynomial->coef<<"x^"<<Polynomial->power;//第一項

    for(front=Polynomial->next; front!=NULL; front=front->next)

    {

    if(front->coef>0)//係數為"正",印多項式時印出 "+" 號

    cout<<"+"<<front->coef<<"x^"<<front->power;

    else

    if(front->coef==0)//係數為0,不印

    continue;

    else

    cout<<front->coef<<"x^"<<front->power;

    }

    }

    }

    void merge(NODEPTR & Merge, NODEPTR No_change)

    {

    NODEPTR index= No_change;

    for(index; index!=NULL; index=index->next)

    insert(Merge, index->coef, index->power);

    }

    void free_memory(NODEPTR & Polynomial)

    {

    NODEPTRrear= NULL;

    NODEPTRfront= NULL;

    if(Polynomial!=NULL)

    {

    front = Polynomial;

    rear= front->next;

    for (front, rear; rear!=NULL; front=rear, rear=front->next)

    delete front;

    delete front;

    Polynomial = NULL;

    }

    }

    2006-05-11 22:13:10 補充:

    int main()

    {

    NODEPTRPolynomial_1= NULL;

    NODEPTRPolynomial_2= NULL;

    intnode_number_1=0;

    intnode_number_2=0;

    floatpower;

    floatcoef;

    2006-05-11 22:13:30 補充:

    cout<<"請輸入第一個一元多項式(係數輸入0,次方數輸入任意數時,即可完成輸入):"<<endl;

    do

    {

    cout<<"係數: ";

    cin>>coef;

    cout<<"x的次方數: ";

    cin>>power;

    if(coef!=0)

    {

    insert(Polynomial_1, coef, power);

    node_number_1++;

    }

    }while(coef!=0);

    2006-05-11 22:13:44 補充:

    cout<<"\n請輸入第二個一元多項式(係數輸入0,次方數輸入任意數時,即可完成輸入):"<<endl;

    do

    {

    cout<<"係數: ";

    cin>>coef;

    cout<<"x的次方數: ";

    cin>>power;

    if(coef!=0)

    {

    insert(Polynomial_2, coef, power);

    node_number_2++;

    }

    }while(coef!=0);

    2006-05-11 22:14:51 補充:

    //print list

    cout<<"\n第一個多項式為: ";

    print(Polynomial_1);

    cout<<"\n第二個多項式為: ";

    print(Polynomial_2);

    cout<<"\n相加結果為: ";

    if(node_number_1 < node_number_2)

    {

    merge(Polynomial_2, Polynomial_1);

    print(Polynomial_2);

    }

    2006-05-11 22:15:15 補充:

    else

    {

    merge(Polynomial_1, Polynomial_2);

    print(Polynomial_1);

    }

    free_memory(Polynomial_1);

    free_memory(Polynomial_2);

    return 0;

    }//end of main

    2006-05-11 22:16:07 補充:

    輸出結果:

    請輸入第一個一元多項式(係數輸入0,次方數輸入任意數時,即可完成輸入):

    係數: 6

    x的次方數: 5

    係數: 2

    x的次方數: 7

    係數: -5

    x的次方數: 3

    係數: 8

    x的次方數: 6

    係數: -3

    x的次方數: 2

    係數: 0

    x的次方數: 1

    請輸入第二個一元多項式(係數輸入0,次方數輸入任意數時,即可完成輸入):

    係數: 3

    x的次方數: 6

    係數: 2

    x的次方數: 4

    係數: -4

    x的次方數: 8

    係數: -6

    x的次方數: 7

    係數: 0

    x的次方數: 1

    2006-05-11 22:17:14 補充:

    第一個多項式為: 2x^7+8x^6+6x^5-5x^3-3x^2

    第二個多項式為: -4x^8-6x^7+3x^6+2x^4

    相加結果為: -4x^8-4x^7+11x^6+6x^5+2x^4-5x^3-3x^2

    執行檔下載:

    http://www.badongo.com/file/635449

    右上角倒數讀秒完後,出現「click here to download」點擊下載

    Source(s): 自己寫的
Still have questions? Get your answers by asking now.