數(shù)據(jù)結(jié)構(gòu).都穿、(Data structures. All wear,)_第1頁(yè)
數(shù)據(jù)結(jié)構(gòu).都穿、(Data structures. All wear,)_第2頁(yè)
數(shù)據(jù)結(jié)構(gòu).都穿、(Data structures. All wear,)_第3頁(yè)
數(shù)據(jù)結(jié)構(gòu).都穿、(Data structures. All wear,)_第4頁(yè)
數(shù)據(jù)結(jié)構(gòu).都穿、(Data structures. All wear,)_第5頁(yè)
已閱讀5頁(yè),還剩33頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、數(shù)據(jù)結(jié)構(gòu).都穿、(Data structures. All wear,)# include Using namespace STD.Typedef struct linknode Float data;Int power;Struct linknode * next;/ / create a linear tableLinknode * CreateList () Head linknode * and * p, * s * q; / / head is the head pointer that needs to be returned after the linear table is e

2、stablished. The p pointer points to the last node of the linear table, and s represents the newly created nodeFloat newdata; / / newdata is the coefficient part to join the new nodeInt newpower; / / newpower is part of the index to add new nodesInt z = 1; / / integer z represents the determination o

3、f the loopThe head = new linknode; / / create a new specification and let the head pointer point to itThe head - next = NULL; / / the next pointer to the initialization header is empty/ / p = head; / / initializes the p pointer and the head pointer to the same nodeCout please enter nodes one by one:

4、 0 0 as the end mark endl;While (z) Cout please enter two data, the first part is the coefficient part, the second part is the index part newdata newpower;If (newdata = = 0 & newpower = = 0) Z = 0;Else / / sort insertP = the head;Q = p;While (p - next! = NULL & p - next - power newpower) Q = p;P = p

5、 - next;If (p- next = = NULL) / / the last insert in the listS = new linknode;S - data = newdata;S - power = newpower;S - next = NULL;P - next = s;The continue;If (p - next - power = = newpower) P - next - data = p - next - data + newdata;If (p- next - power next;S = new linknode;S - data = newdata;

6、S - power = newpower;S - next = p;Q - next = s;/ *S = new linknode; / / the input data is valid and a new node is appliedS - data = newdata;S - power = newpower;S - next = NULL;P - next = s; / / after the first modification, head - next points to the first nodeP = s;* /Return the head;/ / display th

7、e linear tableVoid showList (linknode * a) Linknode * p = a;Bool bl = true; / / when the bl value is true, it represents the output of the first dataIf (p - next = = NULL | | p = = NULL) Cout linear table is empty next! = NULL) If (p - next - data = = 0) P = p - next;The continue;Else if (p- next -

8、data 0) / / when the coefficient portion corresponding to the operation element is positiveIf (bl = = true) / / when the data to be output is the first dataIf (p - next - power = = 0) cout next - data; / / when the exponent part is 0The else cout next - data * X next - power; Bl = false;Else / / whe

9、n the data to be output is not the first dataIf (p - next - power = = 0) cout + next - data; / / when the exponent part is 0The else cout + next - data * X next - power; Else / / when the coefficient portion corresponding to the operation element is negativeIf (p - next - power = = 0) cout next - da

10、ta; / / when the exponent part is 0The else cout next - data * X next - power; Bl = false;P = p - next;Cout endl;/ / remove node elementsVoid DelList (float data, int power, linknode * head) Linknode * p * q; / / P pointer points to the node to be deleted, and q points to the previous node of the de

11、leted nodeIf (head = = NULL) Cout next = = NULL) Cout next; / / p pointer points to the address of the first nodeWhile (p = NULL &! (p - data = = data & p - power = = power) Q = p;P = p - next;If (p! = NULL) / / p! = NULL, which indicates that the node has been found to be deletedQ - next = p - next

12、;The delete p;The else The return;/ / find the element of the node, and there is an index corresponding to the indexFloat SearchList (int power, linknode * head) / / return type is the index of the corresponding coefficientLinknode * p; / / p pointer is used to traverse the entire linear table to fi

13、nd the corresponding nodeIf (head = = NULL) Cout next = = NULL) Cout list is empty! next;While (p! = NULL & p - power! = power) P = p - next;If (p! = NULL) The return p - data;The else Return 0;/ / find the node element, and the presence returns true, otherwise falseBool SearchList (float data, int

14、power, linknode * head) Linknode * p; / / p pointer is used to traverse the entire linear table to find the corresponding nodeIf (head = = NULL) Cout next = = NULL) Cout list is empty! next;While (p = NULL &! (p - power = = power & p - data = = data) P = p - next;If (p! = NULL) Return true;The else

15、Return false;/ / insert node elements to insert data into new linked lists.Void incorporation (float data, int power, linknode * head) / *Linknode * s, * p;P = the head;While (p - next! = NULL) P = p - next;S = new linknode;P - next = s;S - data = data;S - power = power;S - next = NULL;* /Linknode *

16、 p * q * s;P = the head;Q = p;While (p - next! = NULL & p - next - power power) Q = p;P = p - next;If (p- next = = NULL) / / the last insert in the listS = new linknode;S - data = data;S - power = power;S - next = NULL;P - next = s;The return;If (p - next - power = = power) P - next - data = p - nex

17、t - data + data;The return;If (p- next - power next;S = new linknode;S - data = data;S - power = power;S - next = p;Q - next = s;The return;/ / addition operationLinknode * add (linknode * head1, linknode * head2) If (head1 = = NULL | | head1 - next = = NULL) return head2;If (head2 = = NULL | | head

18、2 - next = = NULL) return head1;Float data, temp; / / data represents the newly inserted coefficient, temp represents the state valueInt power; / / power represents the newly inserted indexNewList linknode * and * (p1, * (p2);NewList = new linknode;NewList - next = NULL; / / the next pointer to the

19、first node is blankP1 = head1 - next;The p2 = head2 - next;While (p1! = NULL & p2! = NULL) Power = p1 - power;Data = p1 - data;Temp = SearchList (power, head2);If (temp = = 0) / / fail to find items with the same indexInsList (data, power, newList);DelList (data, power, head1); / / delete the itemP1

20、 = head1 - next; else / / find items with the same indexData = temp + p1 - data;If (data = = 0) / / when the value added is 0, you do not need to add value to the polynomialData = p1 - data;DelList (data, power,Head1);DelList (temp, power, head2);P1 = head1 - next;The p2 = head2 - next; else InsList

21、 (data, power, newList); / / add new dataData = p1 - data;DelList (data, power, head1);DelList (temp, power, head2);P1 = head1 - next;The p2 = head2 - next;If (head1 - next = = NULL) The p2 = head2 - next;While (p2! = NULL) Data = p2 - data;Power = p2 - power;InsList (data, power, newList);DelList (

22、data, power, head2);The p2 = head2 - next;If (head2 - next = = NULL) P1 = head1 - next;While (p1! = NULL) Data = p1 - data;Power = p1 - power;InsList (data, power, newList);DelList (data, power, head1);P1 = head1 - next;Return newList;/ / subtraction operationLinknode * sub (linknode * head1, linkno

23、de * head2) If (head1 = = NULL | | head2 = = NULL | | head1 - next = = NULL | | head2 - next = = NULL)Cout, which is not a polynomial, is not a polynomial.Return NULL;Float data, temp; / / data represents the newly inserted coefficient, temp represents the state valueInt power; / / power represents

24、the newly inserted indexNewList linknode * and * (p1, * (p2);NewList = new linknode;NewList - next = NULL; / / the next pointer to the first node is blankP1 = head1 - next;The p2 = head2 - next;While (p1! = NULL & p2! = NULL) Power = p1 - power;Data = p1 - data;Temp = SearchList (power, head2);If (t

25、emp = = 0) / / fail to find items with the same indexInsList (data, power, newList);DelList (data, power, head1); / / delete the itemP1 = head1 - next; else / / find items with the same indexData = p1 - data - temp;If (data = = 0) / / when the value added is 0, you do not need to add value to the po

26、lynomialData = p1 - data; / / data means that when the two Numbers want to decrease to 0, the coefficients of the elements that polynomial 1 needs to be removedDelList (data, power, head1);DelList (temp, power, head2);P1 = head1 - next;The p2 = head2 - next; else InsList (data, power, newList); / /

27、add new dataData = p1 - data;DelList (data, power, head1);DelList (temp, power, head2);P1 = head1 - next;The p2 = head2 - next;If (head1 - next = = NULL) The p2 = head2 - next;While (p2! = NULL) Data = p2 - data;Power = p2 - power;InsList (- data, power, newList);DelList (data, power, head2);The p2

28、= head2 - next;If (head2 - next = = NULL) P1 = head1 - next;While (p1! = NULL) Data = p1 - data;Power = p1 - power;InsList (data, power, newList);DelList (data, power, head1);P1 = head1 - next;Return newList;/ / a coefficient is data, which is the result of multiplying the data of power by a polynom

29、ialLinknode * oneMul (float data, int power, linknode * head) Float newdata; / / newdata represents the coefficients of the terms of the polynomials that are performed each timeInt newpower; / / newpower represents the index part of the term of the polynomial in each operationNewList linknode * and

30、* p1.NewList = new linknode;NewList - next = NULL; / / the next pointer to the first node is blankP1 = head - next;While (p1! = NULL) Newdata = p1 - data;Newpower = p1 - power;InsList (data * newdata, power + newpower, newList);P1 = p1 - next;Return newList;/ / multiplicationA void mul (linknode * h

31、ead1, linknode * head2) Float data; / / data is the coefficient part of the data operation of polynomial 1Int power; / / power is an exponential part of the data operation for polynomial 1NewList linknode * and * (p1, * (p2);NewList = NULL;P1 = head1 - next;The p2 = head2 - next;If (p1 = = NULL | |

32、| p2 = = NULL | | head1 = = NULL | | | head2 = = NULL) cout the polynomial input is wrong and cannot be multiplied by the polynomial While (p1! = NULL) Data = p1 - data;Power = p1 - power;Linknode * midList;MidList = oneMul (data, power, head2);NewList = add (midList, newList);P1 = p1 - next;ShowLis

33、t (newList);/ / clone polynomialLinknode * cloneList (linknode * head) If (head = = NULL | | head - next = = NULL) return the head;Float data;Int power;NewList linknode * = new linknode;NewList - next = NULL;Linknode * p = head - next;While (p! = NULL) Data = p - data;Power = p - power;InsList (data

34、, power, newList);P = p - next;Return newList;/ / division operation.How many terms do I haveInt getListLength (linknode * head) If (head = = NULL | | head - next = = NULL) return 0;Int length = 0;Linknode * p = head - next;While (p! = NULL) Length+;P = p - next;Return length;/ / find out the maximu

35、m number of indices in a polynomialInt getMaxPower (linknode * head) If (head = = NULL | | head - next = = NULL) Cout polynomial is empty, and there is no exponential maximum of data next;MaxPower = p - power;While (p! = NULL) If (p - power maxPower) maxPower = p - power;P = p - next;Return maxPower

36、;/ * void division (linknode * head1, linknode * head2) / / head1 is a list pointer of divisor polynomials, and head2 is a list pointer of the list of divisor polynomialsIf (head1 = = NULL | | head2 = = NULL | | head1 - next = = NULL | | head2 - next = = NULL) Cout the input of polynomials is wrong

37、and cannot be done. Please re-enter! endl;Int head1_length = getListLength (head1), head2_length = getListLength (head2);If (head1_length head2_length) The two polynomials are the same as the two polynomials.Cout next = NULL;Linknode * p = cloneList (head1); / / p indicates a copy of the divisor pol

38、ynomial that operates, because it involves deleting the data, so you need to copy it firstFor (int I = 0; I next!/ / until the dividend is traversedDelList (SearchList (maxPower2, head2), maxPower2, q); / / delete the element of the maximum exponent of the dividend to find the element of the second

39、largest indexMaxPower2 = getMaxPower (q); / / find the number of elements of the second indexData1 = data * SearchList (maxPower2, head2); The coefficient value of the element of the / / / sub-index is multiplied by the quotient of the quotient, to verify whether the value can be found in the diviso

40、r, and the finding indicates that the quotient value is validPower1 = power + maxPower2; / / same as aboveTemp = SearchList (data1, power1 head1); / / find the corresponding value in the divisor and find that the quotient is validIf (temp = = = = = true) / / indicates that the same value can be foun

41、d in the divisor/ / continue to maximizeTemp1 = true;The continue;Else / / indicates that you cant find the same value in the divisor, and you can go straight back to the second index of the divisorDelList (SearchList (maxPower1 head1), maxPower1, p); / / delete the value of the largest indexTemp1 =

42、 false;Break;If (temp1 = = = true) / / indicates that this part is valid and is deposited into the merchant listInsList (data, power, newList);Else / / temp1 is equal to false, indicating that there is no valid value in the divisor, that is, that part of the business is invalidThe continue;ShowList

43、(newList); * /Void div (linknode * head1, linknode * head2) If (head1 = = NULL | | head2 = = NULL | | head1 - next = = NULL | | head2 - next = = NULL) Cout the input of polynomials is wrong and cannot be done. Please re-enter! next = NULL;Linknode * p = cloneList (head1); / / p is the remainder queueLinknode * mid = new

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論