• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            獨立思考_潛心修行

            AllenNewOK

            常用鏈接

            統(tǒng)計

            最新評論

            多項式的規(guī)范化_數(shù)據(jù)結(jié)構(gòu)_C語言實現(xiàn)

            多項式的規(guī)范化,采用單鏈表,使用C語言實現(xiàn),gcc調(diào)試通過。

              1 //該程序是為了將無序的、不規(guī)范的多項式進(jìn)行規(guī)范化而寫的。
              2 #include<stdio.h>
              3 #include<stdlib.h>
              4 #define N 8  //指明多項式數(shù)據(jù)項的數(shù)目
              5 
              6 int GetLength();  //獲得單鏈表的長度
              7 void Print();  //打印出單鏈表的節(jié)點數(shù)據(jù)
              8 
              9 typedef struct multinomialnode  //定義存儲多項式數(shù)據(jù)項的節(jié)點的結(jié)構(gòu)體
             10 {
             11     int coefficient,power;  //定義系數(shù)和冪
             12     struct multinomialnode *next;
             13 }node;
             14 
             15 node *Create(int num)  //創(chuàng)建存儲多項式的鏈表
             16 {
             17     int i;
             18     node *head,*pointer,*tmp;
             19     
             20     head=(node*)malloc(sizeof(node));
             21     if(head!=NULL)    pointer=head;
             22     
             23     printf("請依次輸入要處理的多項式元素的系數(shù)和冪:\n");
             24     for(i=0;i<num;i++)
             25     {
             26         printf("請輸入第 %d 個元素的系數(shù)和冪:\n",i+1);
             27         tmp=(node*)malloc(sizeof(node));
             28         if(tmp!=NULL)
             29         {
             30             scanf("%d%d",&tmp->coefficient,&tmp->power);
             31             tmp->next=NULL;
             32             pointer->next=tmp;
             33             pointer=tmp;            
             34         }
             35     }
             36     return(head);
             37 }
             38 
             39 node *Standard(node *head)  //對多項式進(jìn)行規(guī)范化的過程
             40 {
             41     int i;
             42     node *pointer,*pre,*cur,*tmp,*q;
             43     
             44     pointer=head->next;  //代表用于比較及合并相同冪的節(jié)點,也用于條件判斷,控制循環(huán)
             45     pre=pointer;  //代表被比較的節(jié)點的上一個節(jié)點的指針,用于鏈接節(jié)點的操作,從而構(gòu)造新的鏈表
             46     cur=pointer->next;  //代表被比較的節(jié)點,也用于條件判斷,控制循環(huán)
             47 
             48     while(pointer->next!=NULL)  //合并無序多項式中具有相同冪的節(jié)點,并將被合并后的節(jié)點刪除
             49     {
             50         while(cur!=NULL)
             51         {
             52             if(pointer->power==cur->power)  //相等則合并,同時刪除被合并過的節(jié)點
             53             {
             54                 pointer->coefficient+=cur->coefficient;  //合并具有相同冪的項的系數(shù)
             55                 q=cur;
             56                 cur=cur->next;
             57                 pre->next=cur;
             58                 free(q);  //釋放內(nèi)存
             59             }
             60             else  //不等則指向被比較的節(jié)點及其上一節(jié)點的指針均后移
             61             {
             62                 cur=cur->next;
             63                 pre=pre->next;
             64             }
             65         }
             66         pointer=pointer->next;  //后移
             67         pre=pointer;  //重新初始化
             68         cur=pointer->next;  //重新初始化
             69     }
             70     
             71     Print(head);  //打印出上面while以后構(gòu)造成的多項式
             72 
             73     for(i=0;i<GetLength(head);i++)  //將上一步while完成以后得到多項式進(jìn)一步規(guī)范化,使之按數(shù)據(jù)項的冪由大到小依次排列
             74     {
             75         pre=head;  //代表指向當(dāng)前節(jié)點的指針的上一指針,用于交換節(jié)點的操作
             76         cur=head->next;  //代表指向當(dāng)前節(jié)點的指針,用于比較
             77         tmp=cur->next;  //代表指向當(dāng)前節(jié)點的下一節(jié)點的指針,用于比較和條件判斷
             78     
             79         while(tmp!=NULL)
             80         {
             81             if(cur->power<tmp->power)  //如果當(dāng)前數(shù)據(jù)項的冪小于其后緊鄰的數(shù)據(jù)項的冪,則交換兩個節(jié)點在鏈表中的位置,然后改變指針使重新指向
             82             {
             83                 pre->next=tmp;
             84                 cur->next=tmp->next;
             85                 tmp->next=cur;
             86                 
             87                 pre=tmp;
             88                 tmp=cur->next;
             89             }
             90             else  //如果條件相反的話,直接后移這三個指針
             91             {
             92                 pre=pre->next;
             93                 cur=cur->next;
             94                 tmp=tmp->next;
             95             }
             96         }
             97     }
             98 
             99     return(head);
            100 }
            101 
            102 int GetLength(node *head)  //獲得單鏈表的長度
            103 {
            104     int i=0;
            105     node *pointer;
            106     pointer=head->next;
            107 
            108     while(pointer!=NULL)
            109     {
            110         i++;
            111         pointer=pointer->next;
            112     }
            113     return i;
            114 }
            115 
            116 void Print(node *head)  //打印出單鏈表的節(jié)點數(shù)據(jù)
            117 {
            118     int i=0;
            119     node *pointer;
            120     pointer=head->next;
            121     
            122     printf("\n新的多項式系數(shù)和冪表示如下:\n");
            123     while(pointer!=NULL)
            124     {
            125         i++;
            126         printf("第 %d 個數(shù)據(jù)元素的系數(shù)為:%d,冪為:%d\n",i,pointer->coefficient,pointer->power);
            127         pointer=pointer->next;
            128     }
            129 }
            130 
            131 int main()
            132 {
            133     node *multinomial;
            134     multinomial=Create(N);
            135     Print(multinomial);
            136 
            137     multinomial=Standard(multinomial);
            138     Print(multinomial);
            139 
            140     return 0;
            141 }
            142 



            調(diào)試環(huán)境:Ubuntu Desktop 8.04.4    VI 7.1.138    GCC 4.2.4
            QQ:81064483
            E-mail:AllenNewOK@126.com

            復(fù)習(xí)之用,不足之處,煩請高手們指教。< ^_^ >

            posted on 2010-09-17 10:44 AllenNewOK 閱讀(1238) 評論(0)  編輯 收藏 引用

            精品久久久久久久中文字幕| 欧美久久亚洲精品| 99精品久久精品| 久久影视综合亚洲| 亚洲国产精品18久久久久久| 2020最新久久久视精品爱| 亚洲国产精品综合久久网络| 97久久国产综合精品女不卡| 久久久久国产一级毛片高清版| 伊人久久五月天| 久久天堂电影网| 久久狠狠爱亚洲综合影院| 蜜桃麻豆www久久| 亚洲午夜久久久久妓女影院 | 狠狠综合久久综合中文88| 久久亚洲sm情趣捆绑调教| 国产精品成人久久久久久久| 亚洲AV无码久久精品狠狠爱浪潮| 国产午夜精品理论片久久| 久久精品国产亚洲av麻豆小说 | 国内精品久久国产大陆| 久久亚洲精品国产亚洲老地址 | 偷窥少妇久久久久久久久| 久久精品一区二区国产| 无码人妻精品一区二区三区久久久 | 久久久久国色AV免费观看| 国产亚洲精品自在久久| 久久久久av无码免费网| 亚洲日本va午夜中文字幕久久 | 久久夜色精品国产欧美乱| 伊人情人综合成人久久网小说 | 开心久久婷婷综合中文字幕| 91久久精品电影| 99久久综合国产精品二区| 久久久国产精品福利免费| 欧美日韩中文字幕久久伊人| 久久这里只有精品久久| 久久综合丁香激情久久| 99久久99久久精品国产片果冻| 777久久精品一区二区三区无码| 色综合合久久天天综合绕视看|